Chaps, I wonder if you can give me a hand with this. Here's the brief:
"Create a program that draws 10 random filled shapes in random colors, position and sizes. Method paintcomponent should contain a loop that iterates 10 times. In each iteration, the loop should determine whether to draw a filled rectangle or an oval, create a random color and choose coordinates and dimension at random. The coordinates should be chosen based on the panel's width and height. Lenghts of sides should be limited to half the width or height of the window. "
Right, so I managed to do that, code's here:
//ShapesTest.java
import javax.swing.JFrame;
public class ShapesTest{
public static void main( String[] args){
Shapes panel = new Shapes();
JFrame application = new JFrame();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.add( panel );
application.setSize( 600, 600 );
application.setVisible( true );
}
}
AND:
/*
p263 draw 10 random filled shapes in random colours, positions and sides
*/
//Shapes.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
public class Shapes extends JPanel{
private static final Random randomShape = new Random();
public void paintComponent( Graphics g ){
super.paintComponent( g );
int width = getWidth();
int height = getHeight();
int shape;//holding the shape
int colour;//holding the colour
int x;//holding x coordinate
int y;//holding y coordinate
int shapeWidth;
int shapeHeight;
for( int i = 0; i < 10; i++ ){
//determine the coordinates and size of shapes to draw
shape = decideShape();
colour = decideColour();
x = decideCoordinates( width );
y = decideCoordinates( height );
shapeWidth = decideSize( width );
shapeHeight = decideSize( height );
//switch statement for colour
switch( colour ){
case 1:
g.setColor( Color.BLACK );
break;
case 2:
g.setColor( Color.BLUE );
break;
case 3:
g.setColor( Color.CYAN );
break;
case 4:
g.setColor( Color.DARK_GRAY );
break;
case 5:
g.setColor( Color.GRAY );
break;
case 6:
g.setColor( Color.GREEN );
break;
case 7:
g.setColor( Color.LIGHT_GRAY );
break;
case 8:
g.setColor( Color.MAGENTA );
break;
case 9:
g.setColor( Color.ORANGE );
break;
case 10:
g.setColor( Color.PINK );
break;
case 11:
g.setColor( Color.RED );
break;
case 12:
g.setColor( Color.WHITE );
break;
case 13:
g.setColor( Color.YELLOW );
break;
}//colour switch
//switch statement for shape
switch( shape ){
case 1:
g.fillOval( x, y, shapeWidth, shapeHeight );
break;
case 2:
g.fillRect( x, y, shapeWidth, shapeHeight );
break;
}//shape switch
}//end of for loop
}//end of paintComponent method
//choose randomly the shape, 1 for oval, 2 for rectangle
public static int decideShape(){
int theShape = 1 + randomShape.nextInt(2);
// System.out.printf("Shape value returned is %d\n", theShape);
return theShape;
}
//choose randomly the colour among the 13 color objects
public static int decideColour(){
int theColour = 1 + randomShape.nextInt(13);//choose from the 13 predefined objects
//System.out.printf("Colour value returned is %d\n", theColour);
return theColour;
}
//choose random coordinates, x and y depending on the parameter passed to it
public static int decideCoordinates( int origin ){
int coordinate = randomShape.nextInt( origin + 1 );
//System.out.printf("Coordinate value returned is %d\n", coordinate);
return coordinate;
}
//choose random side lenghts
public static int decideSize(int dimension){
int side = randomShape.nextInt( dimension / 2 );
//System.out.printf("Coordinate value returned is %d\n", side);
return side;
}
}//end of class Shapes
So the problem I am having here is that after having run the program everytime I resize the java windows the shapes get drawn again, whereas I'd like them to stay as they are. Is that possible?
One more thing: the brief says to create a color, whereas I didn't really do that, but reused the color classes. Now, if I ought to create a colour, how would I go about it? I know there is this constructor Color(int r, int g, int b)
but how could I use it in my program rather than reusing the 13 color classes?
any suggestion?
thanks