****BOXESPANEL****
import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
public class BoxesPanel extends JPanel
{
private final int MAX_SIDE = 50, MAX_SHADE = 75;
private final int MAX_X = 750, MAX_Y = 300;
private Random generator;
public BoxesPanel ()
{
generator = new Random();
setBackground (Color.black);
setPreferredSize (new Dimension(800, 350));
}
public void paintComponent(Graphics page)
{
super.paintComponent (page);
int x, y;
int widthRect, heightRect, widthOval, heightOval, circleDiameter, squareSize;
int howManyRect, howManySquare, howManyOval, howManyCircle;
howManyRect = generator.nextInt(MAX_SHADE) + 1;
howManySquare = generator.nextInt(MAX_SHADE) + 1;
howManyCircle = generator.nextInt(MAX_SHADE) + 1;
howManyOval = generator.nextInt(MAX_SHADE) + 1;
for (int count = 0; count < howManyRect; count++)
{
x = generator.nextInt(MAX_X) + 1;
y = generator.nextInt(MAX_Y) + 1;
widthRect = generator.nextInt(MAX_SIDE) + 1;
heightRect = generator.nextInt(MAX_SIDE) + 1;
page.setColor (Color.red);
page.fillRect (x, y, widthRect, heightRect);
}
for (int count = 0; count < howManyOval; count++)
{
x = generator.nextInt(MAX_X) + 1;
y = generator.nextInt(MAX_Y) + 1;
widthOval = generator.nextInt(MAX_SIDE) + 1;
heightOval = generator.nextInt(MAX_SIDE) + 1;
page.setColor (Color.blue);
page.fillOval(x, y, widthOval, heightOval);
}
for (int count = 0; count < howManyCircle; count++)
{
x = generator.nextInt(MAX_X) + 1;
y = generator.nextInt(MAX_Y) + 1;
circleDiameter = generator.nextInt(MAX_SIDE) + 1;
page.setColor (Color.yellow);
page.fillOval(x, y, circleDiameter, circleDiameter);
}
for (int count = 0; count < howManySquare; count++)
{
x = generator.nextInt(MAX_X) + 1;
y = generator.nextInt(MAX_Y) + 1;
squareSize = generator.nextInt(MAX_SIDE) + 1;
page.setColor (Color.green);
page.fillRect (x, y, squareSize, squareSize);
}
}
}
*****BOXES******
import javax.swing.JFrame;
public class Boxes
{
//-----------------------------------------------------------------
// Creates the main frame of the program.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Boxes");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
BoxesPanel panel = new BoxesPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}