public class SplatPanel extends JPanel
{
private Circle circle1, circle2, circle3, circle4, circle5;
private Rectangle rec1, rec2, rec3;
// Constructor: Creates five Circle objects.
public SplatPanel()
{
circle1 = new Circle (30, Color.red, 70, 35);
circle2 = new Circle (50, Color.green, 30, 20);
circle3 = new Circle (100, Color.cyan, 60, 85);
circle4 = new Circle (45, Color.yellow, 200, 100);
circle5 = new Circle (60, Color.blue, 350, 200);
// (positionX, positionY, color, width, length)
rec1 = new Rectangle (200, 40, Color.GRAY, 90, 70);
rec2 = new Rectangle (150, 250, Color.red, 10, 10);
rec3 = new Rectangle (40, 200, Color.pink, 50, 300);
setPreferredSize (new Dimension(500, 400));
setBackground (Color.black);
}
// Draws this panel by requesting that each circle draw itself.
public void paintComponent (Graphics page)
{
super.paintComponent(page);
circle1.draw(page);
circle2.draw(page);
circle3.draw(page);
circle4.draw(page);
circle5.draw(page);
rec1.draw(page);
rec2.draw(page);
rec3.draw(page);
}
The objective is to make the circles randomize, how would I go about to randomize the circles so when I run the "Splat" program the circles will randomize in different places and sizes? I have a Rectangle class, a Circle class, a Splat class and then the SplatPanel class. I'm only assuming I would be using the randomizer in the the SplatPanel class
The problem is you are using hard coded values for the
Circles
. Instead you can randomize the values using theRandom
classYou could also use a
List<Circle>
to add the circles to and just loop through them in thepaintComponent
method (which is pretty common).Here's a full running example