How can I add shapes automatically in paintComponent?

Normally, When I want to draw something I do this:

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(3, 7, 10, 10);
g.fillOval(10,10,10,10);
}

But 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.

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, I can add as many circles as I want with the code above. But now I’m struck with doing the same thing without using acm package. I’m really new in Java game programming. Anyone have any idea ???

It looks like to me that the acm package adds an extra layer on top that allows you to instantiate all your shapes.

However, some code needs to be called that can iterate through the acm shapes and actually draw them onscreen.

The Java equivalent of that drawing code would be a loop that calls the Graphics.draw* methods. The nearest corresponding Java classes that mimics acm functionality would be the java.awt.Shape hierarchy but it isn’t an exact match (see below).

For your example you could create your own “shape” hierarchy and implement how they can draw themselves to a graphics context (Graphics or Graphics2D). Then instantiate your shapes into a list and iterate over them and invoke their draw() method.


abstract class MyShape {
    public void draw(Graphics g);
}

class MyCircle extends MyShape {
    private int x;
    private int y;
    private int radius;
    private Color color;

    public MyCircle(int x, int y, int radius, Color color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.color = color;
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.drawOval(x-radius, y-radius, radius*2, radius*2);
    }
}

And in paint():


public void paint(Graphics g) {
    List<MyShape> objs = new List<MyShape>();

    // instantiate the shapes you want to paint
    objs.add(new MyCircle(200, 300, 20, Color.red));
    ...

    // at the end, actually draw your shapes
    for (MyShape s : objs) {
        s.draw(g);
    }
}

The other possibility is to use java.awt.Shape and call Graphics2D.draw(Shape). However the latter is aimed towards drawing complex shapes/polygons with smooth curves at any resolution but that might be overkill for your needs.

I guess overall that with Java you’re working 1 level of abstraction lower than what you’re used to. Closer to the metal so to speak.

Generally you just let every object handle its own drawing, and you don’t worry about what it’s doing.


public class MyEntity
{
    protected int x, y, width, height;
    protected Color color;

    public void draw(Graphics g)
    {
        g.setColor(color);
        g.fillOval(x,y,width,height);
    }
}
public class OtherEntity extends MyEntity
{
    public void draw(Graphics g)
    {
        g.setColor(color);
        g.fillRect(x,y,width,height);
    }
}
//etc.

Then just:


public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    for (int i = 0; i < entities.size(); i++)
    {
        entities.get(i).draw(g);
    }
}