Hey everyone! I have to write a program that generates random shapes in a window, but when the window is resized the program can still remember the shapes and redraw the same ones.
My code so far draws the random shapes, but when I resize the window, the shapes change into new ones. Any help!?

This is what I have as my Picture class:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;


public class Picture extends JPanel {

//chooses at random
private static final Random randomShape = new Random();

public void paintComponent (Graphics g){
    super.paintComponent( g );
    int width = getWidth();
    int height = getHeight();

    int shape;
    int color;

    int x; //x coordinate
    int y; //y coordinate

    int shapeWidth;
    int shapeHeight;

 for( int i = 0; i < 10; i++ ){
    //chooses the coordinates and size of the shapes
     shape = ChoosesShape1();          
     color = ChoosesColor1();

     x = ChoosesCoordinates1( height );
     y = ChoosesCoordinates1( width );

    shapeHeight = ChoosesSize1( height );
    shapeWidth = ChoosesSize1( width );

  //switch statement for size
        switch (shape){

    case 1:
        g.fillOval(x, y, shapeWidth, shapeHeight);
        break;

    case 2:
        g.fillRect(x, y, shapeWidth, shapeHeight);
        break;

    }

//The are my switch statements for the shape colors

    switch(color){

    case 1:
        g.setColor( Color.GRAY);
        break;

    case 2:
        g.setColor( Color.PINK);
        break;

    case 3:
        g.setColor( Color.LIGHT_GRAY);
        break;

    case 4:
        g.setColor( Color.DARK_GRAY);
        break;

    case 5:
        g.setColor( Color.BLUE);
        break;

    case 6:
        g.setColor( Color.YELLOW);
        break;

    case 7:
        g.setColor( Color.MAGENTA);
        break;

    case 8:
        g.setColor( Color.RED);
        break;

    case 9:
        g.setColor( Color.GREEN);
        break;

    case 10:
        g.setColor( Color.BLACK);
        break;
    }


        }
     }




public static int ChoosesShape1(){
    int theShape = 1 + randomShape.nextInt(2);
    return theShape;

     }


 public static int ChoosesColor1(){
    int theColor = 1 + randomShape.nextInt(10);
    return theColor;
     }


 public static int ChoosesCoordinates1( int origin ){
     int coordinate = randomShape.nextInt( origin + 1 );
     return coordinate;
    }


 public static int ChoosesSize1(int dimension){
     int side = randomShape.nextInt( dimension / 2 );
     return side;    
     }

}

I also have a Window, and an Application class.

but when I resize the window, the shapes change into new ones.

  • talking about resize from container to call repaint(from methods implemented in API), thats correct

  • create an array of Object, inside paintComponent loop inside this array, instead fo creating a new set of Obejcts, because paintComponent is/can be (automatically) called from all mouse, key events and linked methods implemented in APIs (e.g. resize)

  • override getPreferredSize for JPanel for getWidth/Height();

None of that really makes sense to me...I am so confused.

Also the rest of my directions are:

"The different shapes form a class hierarchy with the Shape class at the apex. This class is abstract since we should not be able to instantiate just a Shape, but rather one of its sub-classes. The sub-classes include: FilledOval, FilledRectangle, Line, OutlineOval and OutlineRectangle. In addition, there is a sub-class for Arcs, which adds a starting and an extent angle as instance fields. Two additional subclasses are added – FilledArc and OutlineArc. Each of the sub-classes implements a method (draw) which draws the appropriate shape on a Graphics object.
The class Picture is responsible for the random generation of shapes and for keeping track of them through an appropriate List object. The actual generation of the random Shape takes place in a static method of the Shape class called randomFactory() (this idiom is known as a factory). The decision about the Color of the Shape is further deferred to the static method pickColour()."

just a thought: you can easily minimize some of that code.

less code => easier to read => easier to maintain.

switch(color){
    case 1:
        g.setColor( Color.GRAY);
        break;
    case 2:
        g.setColor( Color.PINK);
        break;
    case 3:
        g.setColor( Color.LIGHT_GRAY);
        break;
    case 4:
        g.setColor( Color.DARK_GRAY);
        break;
    case 5:
        g.setColor( Color.BLUE);
        break;
    case 6:
        g.setColor( Color.YELLOW);
        break;
    case 7:
        g.setColor( Color.MAGENTA);
        break;
    case 8:
        g.setColor( Color.RED);
        break;
    case 9:
        g.setColor( Color.GREEN);
        break;
    case 10:
        g.setColor( Color.BLACK);
        break;
    }

is actually the same as:

Color[] colors = {Color.GRAY, Color.PINK, Color.LIGHT_GRAY, Color.DARK_GRAY, Color.BLUE, Color.YELLOW, Color.MAGENTA, Color.RED, Color.GREEN, Color.BLACK};
if ( color > 0 && color < 11){
  g.setColor(colors[color-1]);
}

JComponents API has implemented repaint() as recrusive method, container is (very extensive) repainted from a few events, notifiers,

  • create final arrays of Object (once time e.g. as local variable) outside of paintComponent (each of events created a new painting, refreshed by super.paintComponent( g )), inside paintComponent loop in the prepared arrays of Objects

  • override getPreferredSize for JPanel is about correct value for getWidth/Height();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.