0

I'm trying to combine 3 features into a single JavaFX class. My first feature displays "WELCOME TO JAVA" around in a circle. The second displays a 10x10 matrix of random 1's and 0's. The third displays a smiley face. They should be displayed one after the other in a single pane. I have the first and third features but the matrix one is throwing me off. Although everything is supposed to be in a single pane to display on the same GUI window (per professor), I don't see how else I could've done the matrix other than creating the gridPane. It displays fine without the size constraints, but then it takes up the entire screen and my other 2 features aren't visible. When I add the constraints, it gets small and the numbers aren't visible. I'm not sure how I can fix this. Can someone please help?

        Pane pane = new Pane();

        // Create a circle and set its properties
        Circle circle = new Circle();
        circle.setCenterX(100);
        circle.setCenterY(100);
        circle.setRadius(50);
        circle.setStroke(null); 
        circle.setFill(null);
        pane.getChildren().add(circle); // Add circle to the pane

        //Display WELCOME TO JAVA with the text forming a circle
        int i = 0;
        String phrase = "WELCOME TO JAVA ";
        double degree = 360 / phrase.length();
        for (double degrees = 0; i < phrase.length(); i++, degrees += degree) {
            double pointX = circle.getCenterX() + circle.getRadius() *
                Math.cos(Math.toRadians(degrees));
            double pointY = circle.getCenterY() + circle.getRadius() *
                Math.sin(Math.toRadians(degrees));
            Text letter = new Text(pointX, pointY, phrase.charAt(i) + "");
            letter.setFill(Color.BLACK);
            letter.setFont(Font.font("Times New Roman", FontWeight.BOLD, 20));
            letter.setRotate(degrees + 90);
            pane.getChildren().add(letter); }

        //Create a 10x10 matrix of 1s and 0s
        GridPane pane2 = new GridPane();
        pane2.setHgap(1);
        pane2.setVgap(1);

        Button[][] matrix;

        int length = 10;
        int width = 10;

        ArrayList<TextField> textFields = new ArrayList<>();
        for (int y = 0; y < length; y++) {
            ColumnConstraints colConst = new ColumnConstraints();
            colConst.setPercentWidth(10);
            pane2.getColumnConstraints().add(colConst);
            for (int x = 0; x < width; x++) {
                RowConstraints rowConst = new RowConstraints();
                rowConst.setPercentHeight(10);
                pane2.getRowConstraints().add(rowConst);
                Random rand = new Random();
                int random1 = rand.nextInt(2);
                TextField textf = new TextField();
                textf.setText("" + random1);
                textf.setPrefSize(15, 15);
                pane2.setRowIndex(textf,  x);
                pane2.setColumnIndex(textf,  y);
                pane2.getChildren().add(textf);
            }}

                //Create a smiley face
                Circle circle2 = new Circle();
                circle2.setCenterX(600.0f);
                circle2.setCenterY(100.0f);
                circle2.setRadius(50.0f);
                circle2.setStroke(Color.BLACK);
                circle2.setFill(null);

                pane.getChildren().add(circle2);

                Circle leftInnerEye = new Circle();
                    leftInnerEye.setCenterX(580.0f);
                    leftInnerEye.setCenterY(85.0f);
                    leftInnerEye.setRadius(5);
                    leftInnerEye.setStroke(Color.BLACK);
                    pane.getChildren().add(leftInnerEye);

                Ellipse leftOutterEye = new Ellipse();
                    leftOutterEye.setCenterX(580.0f);
                    leftOutterEye.setCenterY(85.0f);
                    leftOutterEye.setRadiusX(11.0f);
                    leftOutterEye.setRadiusY(8.0f);
                    leftOutterEye.setStroke(Color.BLACK);
                    leftOutterEye.setFill(null);
                    pane.getChildren().add(leftOutterEye);

                Circle rightEye = new Circle();
                    rightEye.setCenterX(620.0f);
                    rightEye.setCenterY(85.0f);
                    rightEye.setRadius(5);
                    rightEye.setStroke(Color.BLACK);
                    pane.getChildren().add(rightEye);

                Ellipse rightOutterEye = new Ellipse();
                    rightOutterEye.setCenterX(620.0f);
                    rightOutterEye.setCenterY(85.0f);
                    rightOutterEye.setRadiusX(11.0f);
                    rightOutterEye.setRadiusY(8.0f);
                    rightOutterEye.setStroke(Color.BLACK);
                    rightOutterEye.setFill(null);
                    pane.getChildren().add(rightOutterEye);

                Polygon nose = new Polygon();
                    nose.getPoints().setAll(
                            600d, 90d,
                            588d, 115d,
                            612d, 115d );
                    nose.setStroke(Color.BLACK);
                    nose.setFill(null);
                    pane.getChildren().add(nose);

                Arc mouth = new Arc(600, 115, 30, 16, 180, 180);
                    mouth.setFill(null);
                    mouth.setType(ArcType.OPEN);
                    mouth.setStroke(Color.BLACK);
                    pane.getChildren().add(mouth);

            HBox hbox = new HBox(pane, pane2);
            hbox.autosize();
            hbox.setAlignment(Pos.BASELINE_LEFT);
            hbox.setPadding(new Insets(20));

        // Create a scene and place it in the stage
        Scene scene = new Scene(hbox, 1000, 500);
        primaryStage.setTitle("Laura's Chapter 14"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
        } 

    catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
  • I just heard back from the professor that the instructions were incorrect. It's not supposed to be a single pane. It's supposed to be separate ones. So the only thing I need help with is properly resizing the matrix pane. – Laura Bingelyte Oct 04 '15 at 01:57
  • Hi, you might consider editing your question to reflect your new requirements so we can help you better. You might also want to condense the question as much as possible as it's pretty broad. What is the essential thing about your code that is either giving you trouble or that you don't understand? – eeeeeean Oct 04 '15 at 02:28

1 Answers1

0

Create a pane for each feature and then add the features to either a VBox or HBox, that will root pane of the scene. That way you can have a GridPane for your second feature. There are different layouts available in JavaFX and they behave differently. Take a look at this documentation and its sub documentations.

hotzst
  • 7,238
  • 9
  • 41
  • 64