Input in javafx

Asked

Viewed 252 times

1

I have the following program that draws an illusion of Pogendorff but I need that before he runs ask the number of rectangles and by chance of lines. That is, the N_BARS be put by input provided for the execution of the program.I leave the code:

import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.shape.*;
import javafx.embed.swing.*;
import javafx.application.*;
import javafx.scene.text.*;
import java.util.*;
import javafx.scene.paint.Color;

public class Ilusao  {  
 private Pane pane;
private void start(Stage primaryStage) 
{
    primaryStage.setOnCloseRequest(
        e -> Platform.runLater( () -> {Platform.exit(); System.exit(0);} ) );

    // WRITE YOUR CODE HERE
    // TODO    

    // https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Pane.html
    this.pane = new Pane();
    this.pane.setPrefSize(900, 600);
    primaryStage.setScene(new Scene(this.pane, Color.WHITE));
    primaryStage.show();

    Label label1 = new Label("Name:");
    TextField textField = new TextField ();
    HBox hb = new HBox();
    hb.getChildren().addAll(label1, textField);
    hb.setSpacing(10);

    this.drawIlusion();
} // END start
/**
 * Add shape to pane 
 */
public void addShape(Shape shape)
{
    Platform.runLater(() -> this.pane.getChildren().add(shape));
}

/** execute this method to start the program
 * executing the code in method start(Stage primaryStage) 
 */
public static void start()
{
    Ilusao drawingApp = new Ilusao();
    drawingApp.launch();
}

public void launch()
{
    // Initialises JavaFX:
    new JFXPanel();
    // Makes sure JavaFX doesn't exit when first window is closed:
    Platform.setImplicitExit(false);
    // Runs initialisation on the JavaFX thread:
    Platform.runLater(() -> start(new Stage()));
}

public Ilusao() 
{
    super();
}

private void drawIlusion()
{      
    final int N_BARS = 14;
    for(int i =50; i <=N_BARS*50;i+=100)
    {
        Rectangle rectangles = new Rectangle(i,50,30,300);
        rectangles.setStrokeWidth(0.5);
        rectangles.setStroke(Color.BLACK);
        rectangles.setFill(Color.YELLOW);
        pane.getChildren().add(rectangles);
    }
    int k =50;
    for(int j = 0;j<=N_BARS*50;j+=100)
    {
        Line lowerLine = new Line (j,350,k,267);
        lowerLine.setStrokeWidth(0.5);
        lowerLine.setStroke(Color.BLACK);
        pane.getChildren().add(lowerLine);
        k+=100;
    }
    int m = 150;
    for(int l = 80;l<=N_BARS*50;l+=100)
    {

        Line upperLine = new Line(l,218,m,100); 
        upperLine.setStrokeWidth(0.5);
        upperLine.setStroke(Color.BLACK);
        pane.getChildren().add(upperLine);

        m+=100;
    }

    // Line testLine = new Line(0,350,180,50);
    // testLine.setStrokeWidth(0.5);
    // testLine.setStroke(Color.BLACK);
    // pane.getChildren().add(testLine);
    // END class World

   }
}

1 answer

1

You can make N_BARS class attribute, can be changed from any method of Ilusao before the method drawIlusion be called to draw the elements. Another solution is to parameterize the method drawIlusion to take as argument the number that will replace N_BARS, any of the options will work.

The reading part of the input can be done using the class TextInputDialog:

TextInputDialog dialog = new TextInputDialog();
dialog.setHeaderText("Nº de Barras");
dialog.setContentText("Digite o número de barras a serem criadas:");

// para só aceitar números.
dialog.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
   if(!newValue.matches("\\d*"))
      dialog.getEditor().setText(newValue.replaceAll("\\D+", ""));
});

Optional<String> value = dialog.showAndWait();
if(value.isPresent()){
   try {
       int bars =  Integer.parseInt(value.get());
       this.drawIlusion(bars);
   } catch(NumberFormatException ex){
      // Seja legal e trate as exceções. :) 
   }
}

Staying:

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.shape.*;
import java.util.*;
import javafx.scene.paint.Color;

public class Ilusao extends Application {

    private Pane pane;

    @Override
    public void start(Stage primaryStage) {

        this.pane = new Pane();
        this.pane.setPrefSize(900, 600);
        primaryStage.setScene(new Scene(this.pane, Color.WHITE));

        Label label1 = new Label("Name:");
        TextField textField = new TextField();
        HBox hb = new HBox();
        hb.getChildren().addAll(label1, textField);
        hb.setSpacing(10);

        TextInputDialog dialog = new TextInputDialog();
        dialog.setHeaderText("Nº de Barras");
        dialog.setContentText("Digite o número de barras a serem criadas:");

        dialog.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
            if(!newValue.matches("\\d*"))
                dialog.getEditor().setText(newValue.replaceAll("\\D+", ""));
        });

        Optional<String> value = dialog.showAndWait();
        if(value.isPresent()){
            try {
                int bars =  Integer.parseInt(value.get());
                this.drawIlusion(bars);
            } catch(NumberFormatException ex){
                // Seja legal e trate as exceções. :) 
            }
        }
        primaryStage.show();
    }

    private void drawIlusion(int bars){
        for (int i = 50; i <= bars * 50; i += 100) {
            Rectangle rectangles = new Rectangle(i, 50, 30, 300);
            rectangles.setStrokeWidth(0.5);
            rectangles.setStroke(Color.BLACK);
            rectangles.setFill(Color.YELLOW);
            pane.getChildren().add(rectangles);
        }
        int k = 50;
        for (int j = 0; j <= bars * 50; j += 100) {
            Line lowerLine = new Line(j, 350, k, 267);
            lowerLine.setStrokeWidth(0.5);
            lowerLine.setStroke(Color.BLACK);
            pane.getChildren().add(lowerLine);
            k += 100;
        }
        int m = 150;
        for (int l = 80; l <= bars * 50; l += 100) {

            Line upperLine = new Line(l, 218, m, 100);
            upperLine.setStrokeWidth(0.5);
            upperLine.setStroke(Color.BLACK);
            pane.getChildren().add(upperLine);

            m += 100;
        }
    }
}
  • Good, I tested your code and the number entered does not match the number of bars that appears? I can’t identify the error.

  • @Phil tested your previous code and it also doesn’t display the correct number. Isn’t your code’s logic problem? The only change I made was to let the value of N_BARS was specified via input.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.