0

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.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Kuroshi
0

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

0

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 by Kuroshi: n/a

0

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 by Akill10: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.