Issue
I'm working on a homework problem where I am making a cylinder using Java and Java FX. The cylinder is to re-size based on the size of the window.
I start by creating an ellipse at the top, two vertical lines, and two arc's at the bottom (one dashed). I have bound them to the pane, so they change as the window gets re-sized.
When I try to run it, the program compiles fine (in Intelli-J), the new Java window show up, but the program seems to hang there. I can't access the window, just see it in the program bar on my Mac.
For some reason, when I add text individually to the pane before I add all with my shapes, it works fine?
Code is below. Any help would be greatly appreciated!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class CylinderResize extends Application {
@Override
public void start(Stage primaryStage) {
//Create Pane
Pane pane = new Pane();
Scene scene = new Scene(pane);
//Create Elipse for top
Ellipse ellipse = new Ellipse();
//Make the Center X property half of the pane width
ellipse.centerXProperty().bind(pane.widthProperty().divide(2));
//Height starts 1/3 the way down
ellipse.centerYProperty().bind(pane.heightProperty().divide(3));
//X Radius is 1/4 the width property and y radius is 1/8 WIDTH property
ellipse.radiusXProperty().bind(pane.widthProperty().divide(4));
ellipse.radiusYProperty().bind(pane.heightProperty().divide(8));
ellipse.setStroke(Color.BLACK);
ellipse.setFill(Color.WHITE);
//Create Solid arch for bottom
Arc solidArc = new Arc();
solidArc.centerXProperty().bind(pane.widthProperty().divide(2));
solidArc.centerYProperty().bind(pane.heightProperty().multiply(2).divide(3));
solidArc.radiusXProperty().bind(pane.widthProperty().divide(4));
solidArc.radiusYProperty().bind(pane.heightProperty().divide(8));
solidArc.setStartAngle(180);
solidArc.setLength(180);
solidArc.setType(ArcType.OPEN);
solidArc.setStroke(Color.BLACK);
solidArc.setFill(Color.WHITE);
//Create dashed line for bottom
Arc dashedArc = new Arc();
dashedArc.centerXProperty().bind(pane.widthProperty().divide(2));
dashedArc.centerYProperty().bind(pane.heightProperty().multiply(2).divide(3));
dashedArc.radiusXProperty().bind(pane.widthProperty().divide(4));
dashedArc.radiusYProperty().bind(pane.heightProperty().divide(8));
dashedArc.setStartAngle(0);
dashedArc.setLength(180);
dashedArc.setType(ArcType.OPEN);
dashedArc.setFill(Color.WHITE);
dashedArc.setStroke(Color.BLACK);
dashedArc.getStrokeDashArray().addAll(6.0, 21.0);
//Create Vertical Lines for the sides
Line leftLine = new Line();
leftLine.startXProperty().bind(ellipse.centerXProperty().subtract(ellipse.radiusXProperty()));
leftLine.startYProperty().bind(ellipse.centerYProperty());
leftLine.endXProperty().bind(solidArc.centerXProperty().subtract(solidArc.radiusXProperty()));
leftLine.endYProperty().bind(solidArc.centerYProperty());
leftLine.setStroke(Color.BLACK);
Line rightLine = new Line();
rightLine.startXProperty().bind(ellipse.centerXProperty().add(ellipse.radiusXProperty()));
rightLine.startYProperty().bind(ellipse.centerYProperty());
rightLine.endXProperty().bind(solidArc.centerXProperty().add(solidArc.radiusXProperty()));
rightLine.endYProperty().bind(solidArc.centerYProperty());
rightLine.setStroke(Color.BLACK);
//Test with a text box
Text text = new Text(25, 25, "WHY IS THIS REQUIRED?????");
//Add the objects to the pane
pane.getChildren().add(text); //WHEN I TAKE THIS LINE OUT, THE PROGRAM JUST HANGS.........
pane.getChildren().addAll(ellipse, solidArc, dashedArc, leftLine, rightLine);
//Set Up Stage
primaryStage.setTitle("Ellipse that scales");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Solution
Its not that your program is hanging but it is the stage is not sizing to anything you can fix that by setting a height and width like so
primaryStage.setWidth(200);
primaryStage.setHeight(200);
The reason the text was fixing it was because it was making a base size that you can visibly see when you remove it you can no longer see it because its so small
Also well done on the project I think its cool
Answered By - Matt