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
}
}
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
@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.– Renan Gomes