mardi 7 novembre 2017

Random generated shapes, polygon class no set coordinates

I am using Java to create an application that randomly generates a circle, square, rectangle, and triangle.

Currently most everything has been going along well. However, in my drawTriangle method (drawTri), I cannot find a way to set the triangles coordinates to a different position when the key is pressed. It continues to "randomly generate" the triangle in the exact same spot. In the rectangle and circles classes there exists the setX and setY. I cannot find anything like that for the polygon class.

Also, how can I randomly generate a square and not a rectangle. How can I randomly generate numbers that would make it in the shape of a square?

I have them update each time a key is pressed and this us used using keyevents.

Below is the relevant code minus the main class.

/**
 * 
 * 
 * Holds 4 types of drawable shapes, squares, rectangles, circles, and triangles
 * Key Events
 * registed to S-square; R-rectangle, T-triangle, C-circle
 * 
 * Mouse Events, user clicks one of 4 random shapes are drawn
 * 
 * Shapes are randomly generated color, location, and size
 * 
 */

public class RandomPane extends BorderPane {
   Pane pane;
   Circle circle;
   Rectangle sqr;
   Polygon tri;
   Rectangle rect;

    Random rnd;
    Random rnd1;    


    public RandomPane() {
        rnd = new Random();
        rnd1 = new Random();
        buildUI();
        customizeUI();
        registerEvents();
    }



    public void buildUI() {
        tri = new Polygon(rnd1.nextInt(590), rnd1.nextInt(590),
                            rnd1.nextInt(590), rnd1.nextInt(590),
                              rnd1.nextInt(590), rnd1.nextInt(590) );
        rect = new Rectangle(rnd1.nextInt(300) + 10, rnd1.nextInt(300) + 10,
                                rnd1.nextInt(300) + 10, rnd1.nextInt(300) + 10);
        circle = new Circle(rnd1.nextInt(590), rnd1.nextInt(590), rnd1.nextInt(590) );
        sqr = new Rectangle(rnd1.nextInt(300) + 10, rnd1.nextInt(300) + 10,
                                rnd1.nextInt(300) + 10, rnd1.nextInt(300) + 10);



    }




    public void registerEvents() {

       // key events
        setOnKeyReleased( e -> {
            switch ( e.getCode() ) {
                case C: drawCircle();  break;
                case S: drawSqr();     break;
                case R: drawRect();    break;
                case T: drawTri();     break;
            }
        });





    }

    public void drawSqr() {

        sqr.setFill(randomRgb() );
        sqr.setFill(randomRgb() );
        sqr.setX(rnd1.nextInt(590) + 1);
        sqr.setY(rnd1.nextInt(590) + 1);
        sqr.setWidth(rnd1.nextInt(300) + 10);
        sqr.setHeight(rnd1.nextInt(300) + 10);

        pane = new Pane(sqr);
        setCenter(pane);
    }// end square method

    public void drawCircle() {

        circle.setCenterX(rnd1.nextInt(590) + 1);
        circle.setCenterY(rnd1.nextInt(590) + 1);
        circle.setRadius(rnd1.nextInt(590) + 1);
        circle.setFill(randomRgb() );

        pane = new Pane(circle);
        setCenter(pane);
    }// end circle method

    public void drawRect() {

        rect.setFill(randomRgb() );
        rect.setX(rnd1.nextInt(590) + 1);
        rect.setY(rnd1.nextInt(590) + 1);
        rect.setWidth(rnd1.nextInt(300) + 10);
        rect.setHeight(rnd1.nextInt(300) + 10);

        pane = new Pane(rect);
        setCenter(pane);
    }// end rectangle method

    double min = 0;
    double max = 590;   // double values for random generated triangle
    Random dub = new Random();
    double randomValue = min + (max - min) * dub.nextDouble();

    public void drawTri() {

        tri.setFill(randomRgb() );

        // I tried here below to get points and change them but nothing happens 

        tri.getPoints().addAll(new Double[]{ randomValue, randomValue,
                                           randomValue, randomValue, 
                                        randomValue, randomValue });

        pane = new Pane(tri);
        setCenter(pane);
    }// end triangle method








       private Color randomRgb() {
       return Color.rgb(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255) );
   }// end random color gen

}// end class




Aucun commentaire:

Enregistrer un commentaire