Lets say I have a JPanel and an object called ball inside of it, instead of creating gets so the JPanel can draw the ball image I would like the ball object to draw itself on the JPanel.

Is it possible?

Thanks :D.

By draw itself do you mean using a method inside its own class to draw on the JPanel in another class?

By draw itself do you mean using a method inside its own class to draw on the JPanel in another class?

Yes xD, I wanted to pass a Graphics object to the JPanel using a method inside a Ball oject, but I saw that Graphics is abstract.

Edited 5 Years Ago by Kuroshi: n/a

Inside the Ball class, import your java.awt.Graphics2D. Then create a method that takes a Graphics2D/Graphics paramater. Then just write the code to draw your ball in that method.

import java.awt.Graphics2D;
public void drawBall(Graphics2D g){
g.fillOval(...);
}

Then, in your paintComponent/paint method on your JPanel, call the method using the paints Graphics object. i.e. ball.drawBall(g2d);

Edited 5 Years Ago by Akill10: n/a

Inside the Ball class, import your java.awt.Graphics2D. Then create a method that takes a Graphics2D/Graphics paramater. Then just write the code to draw your ball in that method.

import java.awt.Graphics2D;
public void drawBall(Graphics2D g){
g.fillOval(...);
}

Then, in your paintComponent/paint method on your JPanel, call the method using the paints Graphics object. i.e. ball.drawBall(g2d);

Thank you xD

This question has already been answered. Start a new discussion instead.