Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How can I add shapes automatically in paintComponent?  (Read 707 times)
0 Members and 1 Guest are viewing this topic.
Offline crystalclear506

Junior Newbie





« Posted 2011-09-01 00:51:46 »

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 Huh
Offline loom_weaver

JGO Coder


Medals: 16



« Reply #1 - Posted 2011-09-01 01:16:30 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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():
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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.
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #2 - Posted 2011-09-01 02:37:16 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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:
1  
2  
3  
4  
5  
6  
7  
8  
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    for (int i = 0; i < entities.size(); i++)
    {
        entities.get(i).draw(g);
    }
}

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.155 seconds with 20 queries.