As a beginner, whenever I want to add graphical shapes inside the frame I do something like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, 10, 10);
g.fillOval(10,10,10,10);
}
How do I draw an unlimited number of shapes automatically inside the frame? If I follow the way I did above I only have a limited number of shapes(Rect, Oval and nothing else).
I'm looking for something different, for example, whenever a method addStuff(x, y)
has called, it draws "Stuff" automatically at the coordinate x and y without having to edit anything inside paintComponent
manually again.
I used to do this with the acm package and it was easy. Just like the code below.
for (int i = 0; i < NCIRCLES; i++) {
double r = rgen.nextDouble(MIN_RADIUS, MAX_RADIUS);
double x = rgen.nextDouble(0, getWidth() - 2 * r);
double y = rgen.nextDouble(0, getHeight() - 2 * r);
GOval circle = new GOval(x, y, 2 * r, 2 * r);
circle.setFilled(true);
circle.setColor(rgen.nextColor());
add(circle);
}
As you can see above, I can add as many circles as I want, I know it can take pages to explain this but I just want a brief instruction on how to create something similar to the code above without relying on acm package.