Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How can I Update the Width and Height of a Pane(or Scene) automatically ?

  1. #1
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default How can I Update the Width and Height of a Pane(or Scene) automatically ?

    Hello everybody.

    I've been working on a program to solve the following exercise:


    (Geometry: find the bounding rectangle)

    Write a program that enables the user
    to add and remove points in a
    two-dimensional plane dynamically, as shown
    in Figure.

    A minimum bounding rectangle is updated as the
    points are added and removed.

    Assume that the radius of each point is 10 pixels.


    I've created a Java JavaFX program that solves this problem.

    That program is working well.

    There is a Pane to present the points (circles).

    That Pane is placed inside a BorderPane, at the center.

    But I would like to make the extra mile, even though the exercise does not require me to do it.

    I would like to improve this program, so that when a Circle is added, the Width and the Height of the Pane are increased.


    I was thinking of making the Width:


    Width = maxX + 40;


    and the Height:

    Height = maxY + 40;

    That would mean that I could always add a new Circle at the Right (Below) the circle most to the Right (Most Lower).

    This is the code of my program:



     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.EndofChapter15.Exerc15dot17;
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
     
     
     
    /*
     
     
    (Geometry: find the bounding rectangle)
     
    Write a program that enables the user
    to add and remove points in a
    two-dimensional plane dynamically, as shown
    in Figure.
     
    A minimum bounding rectangle is updated as the
    points are added and removed.
     
    Assume that the radius of each point is 10 pixels.
     
     
    */
     
     
     
     
    import java.util.ArrayList;
    import javafx.collections.ObservableList;
    import javafx.event.Event;
    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.control.Label;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseButton;
    import javafx.scene.paint.Color;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Line;
    import javafx.stage.Stage;
     
     
     
    public class GetBoundingRectangle extends Application {
     
     
        private ArrayList <Circle> allCircles = new ArrayList<>();
     
     
        private Circle newCircle;
     
     
     
        private ArrayList <Line> boundRectangleLines;
     
     
     
        private ObservableList <Node> listNodes;
     
        /*
        Create the pane for the points
        */
     
        private Pane pointsPane = new Pane();
     
     
     
     
        @Override
        public void start(Stage primaryStage) throws Exception {
     
     
            double initialSceneW;
     
            double initialSceneH;
     
            initialSceneW = 400;
     
            initialSceneH = 320;
     
            /*
            Create the Border pane
            */
     
            BorderPane mainPane = new BorderPane();
     
     
            /*
            Create the label with instructions
            */
     
            Label lblInstructions = new Label("INSTRUCTIONS\n" + "Add: Left Click\n" + "Remove: Right Click");
     
     
     
            /*
            Create the handler
            */
     
            pointsPane.setOnMouseClicked( e -> {
     
     
                int i;
     
                int indexClickedNode;
     
                double getX;
     
                double getY;
     
     
                double newCircleX;
     
                double newCircleY;
     
     
     
                double incrementXY;
     
                double circleRadius;
     
     
     
     
                /*
                Variables to update the
                Width and Height of the
                PointsPane
                */
     
                double incrementWH;
     
                incrementWH = 40;
     
                double newPointsPaneW;
     
                double newPointsPaneH;
     
     
                boolean nodeIsCircle;
     
                boolean pointsPaneUpdated;
     
                pointsPaneUpdated = false;
     
     
                indexClickedNode = -1;
     
     
                getX = e.getX();
     
                getY = e.getY();
     
                incrementXY = 5;
     
                circleRadius = 10;
     
     
                String nodeName;
     
                String circle;
     
                circle = "Circle";
     
     
     
     
     
                double currentNodeListCircleX;
     
                currentNodeListCircleX = 0;
     
                double currentNodeListCircleY;
     
                currentNodeListCircleY = 0;
     
                Node currentNodeList;
     
                Circle currentNodeListCircle;
     
     
                double[][] minMax;
     
     
                //listNodes = pane.getChildren();
     
                /*
                Display all the nodes
                */
     
                System.out.println("");
     
                System.out.println("Displaying all the Nodes:" );
     
                for(i = 0; i < listNodes.size(); i++) {
     
                    System.out.println( listNodes.get(i) );
     
                } 
     
     
     
     
                System.out.println( "Button: " + e.getButton() );
     
                System.out.println("Event To String: " + e.toString() );
     
                nodeName = e.getTarget() + "";
     
     
     
                System.out.println( "Event Target: " + e.getTarget() );
     
     
                nodeIsCircle = nodeName.matches(circle + ".*");
     
                System.out.println( "The Clicked Node is a Circle: " + nodeIsCircle );
     
     
     
     
                if(  e.getButton() == MouseButton.PRIMARY )  {
     
     
                    if(nodeIsCircle == false) {
     
     
                        if( getX <= circleRadius  + incrementXY ) {
     
                            newCircleX = circleRadius + incrementXY;
     
                        } else {
     
                            newCircleX = getX;
     
                        } //end of if else if( getX <= circleRadius )
     
     
                        if( getY <= circleRadius + incrementXY ) {
     
                            newCircleY = circleRadius + incrementXY;
     
                        } else {
     
                            newCircleY = getY;
     
                        } //end of if else if( getY <= circleRadius )
     
     
     
                        newCircle = new Circle( newCircleX, newCircleY,  circleRadius);
     
                        newCircle.setFill( Color.WHITE );
     
                        newCircle.setStroke( Color.BLACK );
     
     
                        allCircles.add(newCircle);
     
                        //listNodes.add( newCircle );
     
                        pointsPaneUpdated = true;
     
     
                        System.out.println( "Added New Circle x: " + getX + " y: " + getY + " and radius: " + circleRadius );
     
     
                    } //end of if(nodeIsCircle == false)
     
     
                } else if( e.getButton() == MouseButton.SECONDARY ) {
     
     
                    if(nodeIsCircle) {
     
     
                        for(i = 0; i < listNodes.size(); i++) {
     
     
                            currentNodeList = listNodes.get(i);
     
     
                            if( currentNodeList instanceof Circle ) {
     
                                currentNodeListCircle = (Circle) currentNodeList;
     
     
                                if(  currentNodeListCircle.contains(getX, getY) ) {
     
                                    indexClickedNode = i;
     
                                }
     
                            } //end of if( currentNodeList instanceof Circle )
     
     
                        } //end of for loop for(j = 0; j < listNodes.size(); j++)
     
     
     
                        System.out.println( "The index for the clicked Circle is " + indexClickedNode );
     
     
                        if(indexClickedNode >= 0) {
     
                            System.out.println("Removing the circle " + nodeName );
     
     
                            //listNodes.remove( indexClickedNode );
     
                            allCircles.remove( indexClickedNode );
     
                            pointsPaneUpdated = true;
     
     
                        } //end of if(indexClickedNode >= 0)
     
     
     
                    } //end of if(nodeIsCircle)
     
     
                } //end of if else if if(  e.getButton() == MouseButton.PRIMARY )
     
     
     
                if(allCircles.size() > 0 && pointsPaneUpdated ) {
     
                    minMax = getMinAndMaxXY(allCircles);
     
     
                    /*
                    Remove all the nodes
                    */
     
                    listNodes.clear();
     
     
                    /*
                    Add all the Circles
                    */
     
                    for(i = 0; i < allCircles.size(); i++) {
     
                        listNodes.add( allCircles.get(i) );
     
                    }
     
     
                    /*
                    Add all the
                    lines in the Array List
                    of Lines
                    */
     
                    boundRectangleLines = getBoundingLines(minMax);
     
     
                    for(i = 0; i < boundRectangleLines.size(); i++) {
     
                        listNodes.add( boundRectangleLines.get(i) );
     
                    }
     
     
     
                    newPointsPaneW = minMax[1][0] + incrementWH;
     
                    newPointsPaneH = minMax[1][1] + incrementWH;
     
     
                    //pointsPane.setMinSize(newPointsPaneW, newPointsPaneH);
     
                    mainPane.setMinSize(newPointsPaneW, newPointsPaneH);
     
     
                } else if( allCircles.size() == 0 && pointsPaneUpdated ) {
     
                    listNodes.clear();
     
                } //end of if else if(allCircles.size() > 0 ) 
     
     
     
            } ); //end of pane.setOnMouseClicked( e -> {} );
     
     
     
     
            pointsPane.getChildren().clear();
     
     
     
            listNodes = pointsPane.getChildren();
     
     
     
            mainPane.getChildren().clear();
     
     
            mainPane.setLeft(lblInstructions);
     
     
            mainPane.setCenter(pointsPane);
     
     
            BorderPane.setAlignment( lblInstructions , Pos.TOP_CENTER);
     
     
     
            /*
            Create a scene and
            place it in the stage
            */
     
            Scene scene = new Scene(mainPane, initialSceneW, initialSceneH);
     
     
            primaryStage.setResizable(true);
     
            primaryStage.setTitle("Display Points in Pane");
     
            primaryStage.setScene(scene);
     
            primaryStage.show();
     
     
     
        } //end of the method public void start(Stage primaryStage)
     
     
     
     
     
     
        /*
        The main method is only needed for the IDE with
        limited JavaFX support.
        Not needed for running from the
        command line
        */
     
        public static void main(String[] args) {
     
     
            try {
     
                Application.launch(args);
     
            } catch(Exception ex1) {
     
                System.out.println("Exception in the main in the " +
                        "class GetBoundingRectangle");
     
                System.out.println( ex1.toString() );
     
            }
     
     
     
        } //end of main
     
     
     
        /*
        Method to get the
        MinX and MaxX
        and
        MinY and MaxY
        from an
        Array List of circles
        */
     
        public double[][] getMinAndMaxXY(ArrayList <Circle> circles) {
     
            int i;
     
            double circleRadius;
     
     
            double currentX;
     
            double currentY;
     
     
     
            double minX, maxX;
     
            double minY, maxY;
     
     
            ArrayList<Double> allX;
     
            ArrayList<Double> allY;
     
     
            allX = new ArrayList<>();
     
            allY = new ArrayList<>();
     
     
            double[][] minMax;
     
            minMax = new double[2][2];
     
     
     
            if( circles.size() > 0) {
     
     
                circleRadius = circles.get(0).getRadius();
     
     
                for(i = 0; i < circles.size(); i++) {
     
                    currentX = circles.get(i).getCenterX();
     
                    currentY = circles.get(i).getCenterY();
     
                    allX.add(currentX);
     
                    allY.add(currentY);
     
     
                }
     
     
                minX = java.util.Collections.min(allX);
     
                minY = java.util.Collections.min(allY);
     
                maxX = java.util.Collections.max(allX);
     
                maxY = java.util.Collections.max(allY);
     
     
                minMax[0][0] = minX - circleRadius;
     
                minMax[0][1] = minY - circleRadius;
     
                minMax[1][0] = maxX + circleRadius;
     
                minMax[1][1] = maxY + circleRadius;
     
     
            } //end of if( circles.size() >= 0)
     
     
            return minMax;
     
     
        } //end of the method public double [][] getMinAndMaxXY(ArrayList <Circle> circles)
     
     
        /*
        Method to get tha
        4 Lines that define the
        bounding rectangle
        */
     
        public ArrayList<Line> getBoundingLines( double[][] minMaxXY ) {
     
     
            int i;
     
            double minX, maxX;
     
            double minY, maxY;
     
            double incrementXY;
     
            Line newLine;
     
     
            incrementXY = 2;
     
     
            ArrayList<Line> allBoundRectangleLines = new ArrayList<>();
     
     
            if( minMaxXY.length == 2 ) {
     
     
                minX = minMaxXY[0][0] - incrementXY;
     
                minY = minMaxXY[0][1] - incrementXY;
     
                maxX = minMaxXY[1][0] + incrementXY;
     
                maxY = minMaxXY[1][1] + incrementXY;
     
     
                /*
                Line 1 - Between (minX, minY) and (maxX, minY)
                */
     
                newLine = new Line(minX, minY, maxX, minY);
     
     
                allBoundRectangleLines.add(newLine);
     
     
                /*
                Line 2 - Between (maxX, minY) and (maxX, maxY)
                */
     
                newLine = new Line(maxX, minY, maxX, maxY);
     
                allBoundRectangleLines.add(newLine);
     
     
                /*
                Line 3 - Between (minX, maxY) and (maxX, maxY)
                */
     
                newLine = new Line(minX, maxY, maxX, maxY);
     
                allBoundRectangleLines.add(newLine);
     
     
     
                /*
                Line 4 - Between (minX, minY) and (minX, maxY)
                */
     
     
                newLine = new Line(minX, minY, minX, maxY);
     
                allBoundRectangleLines.add(newLine);
     
     
     
     
            } //end of if(allCircles.size() > 0 )
     
     
     
            for(i = 0; i < allBoundRectangleLines.size(); i++ ) {
     
     
                allBoundRectangleLines.get(i).setStroke( Color.BLACK );
     
                allBoundRectangleLines.get(i).setStrokeWidth( 2 );
     
            }
     
     
            return allBoundRectangleLines;
     
     
        } //end of the method public ArrayList<Line> getBoundingLines( double[][] minMaxXY )
     
     
     
    } //end of the class GetBoundingRectangle



    Thank you,

    Rogério

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I Update the Width and Height of a Pane(or Scene) automatically ?

    What have you tried?

    What makes so many empty lines in the code? All the extra lines makes the code harder to read and understand.
    Can you remove extra lines before posting?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: How can I Update the Width and Height of a Pane(or Scene) automatically ?

    Hello Norm.

    I'm going to describe what I' ve tried.

    I've made the Stage resizable.


    primaryStage.setResizable(true);


    And inside the if that updates the Obsevable list of the pointsPane, I've tried to use the setMinSize method for the pointsPane and also for the mainPane (Border Pane);


    if(allCircles.size() > 0 && pointsPaneUpdated ) {



    //pointsPane.setMinSize(newPointsPaneW, newPointsPaneH);

    mainPane.setMinSize(newPointsPaneW, newPointsPaneH);



    }


    As for the empty lines, I use a lot of empty lines in my programs.

    I place an empty line between each variable.

    And I also place empty lines between different "SubSections" of the code inside a certain method.

    But I also use lots of comments to help understand the code.

    For example:


    /*
    Create the handler
    */

    pointsPane.setOnMouseClicked( e -> {
    } ); //end of pane.setOnMouseClicked( e -> {} );


    if(allCircles.size() > 0 && pointsPaneUpdated ) {
    } //end of if else if(allCircles.size() > 0 )


    Do you want me to remove all the empty lines in the programs I create ?

    Thank you,

    Rogério

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I Update the Width and Height of a Pane(or Scene) automatically ?

    Don't remove all of the blank lines. Some blank line are good. Too many make it hard to read. If a group of variables are used for specific purpose they can be listed together without intervening blank lines. For example the variable for the x and y locations of objects. Same with assignment statements that are setting up a container.

    Another item: the declaration of a variable and an assignment of a value can be done in a single statement:
    double someVariable = 123.4; // declare and assign in one statement.

    Another item: variables that are constants should be declared final and their names should start with a capital letter.

    Re your question:
    Look at the top level JavaFX container - the Stage. Look at the API doc for Stage to see how to change its size and location.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (October 16th, 2024)

  6. #5
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: How can I Update the Width and Height of a Pane(or Scene) automatically ?

    Hello Norm.

    Thank you for the remarks.

    I'm already trying to group variables but sometimes I have lots of variables in the same program, and it gets messy.

    As for the constants, When I use a constant, I give it a name entirely with Capital Letters.

    However, there is a situation that happens.

    I only make a variable to be a constant if I'm 100% sure that that variable won't need to be changed later in the execution of the program I'm creating.

    If I'm not sure, I prefer to make it a variable and I keep it that way, even if the final version of my program does not change that same variable after declaration and assignment.

    For the message to look at the Stage, that is correct.

    In this case, there was another problem.

    The Stage uses a BorderPane.

    In that BorderPane, in the Left there is a Label.

    I've placed the Label as a private data field of the Class.

    That way, I can get the lblInstructions.getWidth() and lblInstructions.getHeight().

    By adding this to the MaxX and MaxY I get

    newPointsPaneW = maxX + circleRadius + incrementWH (40) + lblInstructions.getWidth();

    Then I use the:

    primaryStage.setWidth(newPointsPaneW);

    primaryStage.setHeight(newPointsPaneH);

    I've tested it and it works, the Stage is updated (increasing or decreasing as long as I Add or Remove Circles and there is at least one Circle in the pointsPane).

    Thank you again

    Rogério

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I Update the Width and Height of a Pane(or Scene) automatically ?

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (October 17th, 2024)

Similar Threads

  1. Replies: 2
    Last Post: July 17th, 2014, 06:20 AM
  2. Getting the width and height of a frame without borders
    By NathanWick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 24th, 2013, 09:45 PM
  3. Why width of dropdown is decreasing automatically on every select?
    By ayush1989 in forum Other Programming Languages
    Replies: 0
    Last Post: February 1st, 2013, 01:39 AM
  4. JButton Width & Height
    By Howdy_McGee in forum Java Theory & Questions
    Replies: 4
    Last Post: May 22nd, 2010, 07:12 AM
  5. Reading the size (height and width) of word documents
    By pradeep_das in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 7th, 2010, 09:58 AM