Thanks
here is my code:
import javax.swing.JPanel; import java.util.Random; import java.awt.*; public class NervousShapesPanel extends JPanel{ Shape[] shapes; Random r; int width, height; public NervousShapesPanel(){ shapes = new Shape[50]; r = new Random(); height = 200; width = 200; setPreferredSize(new Dimension(width, height)); } public void paintComponent(Graphics g){ int shape = r.nextInt(2); int radius = r.nextInt(11) + 10; int w = r.nextInt(11) + 10; int h = r.nextInt(11) + 10; int cX = r.nextInt(width-radius); int cY = r.nextInt(height-radius); int rX = r.nextInt(width-w); int rY = r.nextInt(height-h); Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); //initialize the shape for(int i = 0; i < shapes.length; i++){ shapes[i] = (shape == 0) ? new Circle(cX, cY, c, radius) : new Rectangle(rX, rY, c, h, w); shapes[i].draw(g); } } }
the shape class, as well as it's subclasses Circle and Rectangle were made by me. They both extend Shape, so they're both exactly similar except that circle has a radius variable, and rectangle has a width and height variable. Because they're nearly identical, I'll just show you circle:
import java.awt.*; public class Circle extends Shape{ private int radius; public Circle(int x, int y, Color color, int radius){ super(x, y, color); this.radius=radius; } @Override public int getHeight() { return radius; } @Override public int getWidth() { return radius; } @Override public void draw(Graphics g) { g.setColor(color); g.fillOval(x, y, radius, radius); } }
thanks in advance!