Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 1185 times)
0 Members and 1 Guest are viewing this topic.
Offline crystalclear506

Junior Newbie





« Posted 2011-08-31 22: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: 17



« Reply #1 - Posted 2011-08-31 23: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2011-09-01 00: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.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!