passing parameters to paint(graphics)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bouncydeath
    New Member
    • Feb 2009
    • 1

    passing parameters to paint(graphics)

    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:

    Code:
    public class Main {
    
        
        public static void main(String[] args) {
            
            Creature dude = new Creature(20, 40);
            MakeWorld world = new MakeWorld();
        }
    }
    and:

    Code:
        import java.awt.*;
        import javax.swing.*;
    
    public class MakeWorld {
    
         MakeWorld(){
    
            JFrame frame = new JFrame("title");
    
            frame.getContentPane().add(new MyComponent());
            frame.getContentPane().setBackground(Color.WHITE);
    
            frame.setSize(Globals.FRAME_WIDTH, Globals.FRAME_HEIGHT);
            frame.setVisible(true);
    
        }
    
    
        class MyComponent extends JComponent
        {
            @Override
            public void paint(Graphics g)
            {
     
               Graphics2D g2d = (Graphics2D)g;
    
                g2d.fillOval(10, 20, 2, 2);
    
            }
        }
    So, I am able to draw an oval. How could I make it work so I could do something like:

    g2d.fillOval(du de.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
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Why don't you pass a Graphics(2D) object to your Dudes and let them draw on the Graphics object.

    kind regards,

    Jos

    Comment

    Working...