Input in javaFx

1

I have the following program that draws an illusion of Pogendorff but I need it before it executes the number of rectangles and lines. That is, the N_BARS be put in input before executing 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

   }
}
    
asked by anonymous 12.12.2017 / 21:29

1 answer

1

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

The part of reading the entry can be made using the class N_BARS :

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. :) 
   }
}

Getting:

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;
        }
    }
}
    
13.12.2017 / 12:49