By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

passing parameters to paint(graphics)

P: 1
I think my question ultimately stems from the fact that I don't fully understand classes: how they work together, when to make them, etc.

So I have a class called Creature. These guys have properties that will determine how they are drawn, but right now they just contain an (x,y) for where they are to be drawn. I would just like to draw a circle where each is located. Easy enough, I suppose, but I'm not getting it.

Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.  
  4.     public static void main(String[] args) {
  5.  
  6.         Creature dude = new Creature(20, 40);
  7.         MakeWorld world = new MakeWorld();
  8.     }
  9. }
and:

Expand|Select|Wrap|Line Numbers
  1.     import java.awt.*;
  2.     import javax.swing.*;
  3.  
  4. public class MakeWorld {
  5.  
  6.      MakeWorld(){
  7.  
  8.         JFrame frame = new JFrame("title");
  9.  
  10.         frame.getContentPane().add(new MyComponent());
  11.         frame.getContentPane().setBackground(Color.WHITE);
  12.  
  13.         frame.setSize(Globals.FRAME_WIDTH, Globals.FRAME_HEIGHT);
  14.         frame.setVisible(true);
  15.  
  16.     }
  17.  
  18.  
  19.     class MyComponent extends JComponent
  20.     {
  21.         @Override
  22.         public void paint(Graphics g)
  23.         {
  24.  
  25.            Graphics2D g2d = (Graphics2D)g;
  26.  
  27.             g2d.fillOval(10, 20, 2, 2);
  28.  
  29.         }
  30.     }
So, I am able to draw an oval. How could I make it work so I could do something like:

g2d.fillOval(dude.x, dude.y, 2, 2)?

It doesn't seem like I can add any parameters to paint(); so I can't even reference "dude".

Is there a way I could make a function like:

dude.draw() that I could call outside of the paint() function?

Thanks,

bd
Feb 16 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 10K+
P: 11,448
Why don't you pass a Graphics(2D) object to your Dudes and let them draw on the Graphics object.

kind regards,

Jos
Feb 17 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.