Forums Login/signup

Class to draw a circle to Jframe

+Pie Number of slices to send: Send
I am still learning about classes and Object oriented programming. I created a "Ball" class to help me learn.
This class draws a ball to a JFrame.

To use it, I use the code:



where window is a JFrame.

This works very well, the problem is that when I create more than one ball, it draws the second ball and removes the
first ball.

This is the class:



Code for main class:





Could someone kindly show me how I can modify this class to be able to draw more than one ball simultaneously?
Thank you in advance.
+Pie Number of slices to send: Send
First of all custom painting should be done in the paintComponent() method, not the paint() method.

how I can modify this class to be able to draw more than one ball simultaneously?


One way is to keep a List of Balls to paint. Check out the DrawOnComponent example from Custom Painting Approaches.
+Pie Number of slices to send: Send
 

Davste Ste wrote:
This class draws a ball to a JFrame.


You cannot draw on a JFrame. You are drawing on the MyComponent. You could have as well used JPanel as it already extends JComponent.

You have to somehow store the circles drawn previously. You can use an ArrayList for that. And in the paint(Graphics g) method of the MyComponent, you can iterate through the list and re-draw all the circles in the list.

Update: As I was composing this reply, there was already a solutions posted
+Pie Number of slices to send: Send
haha, and our tutor keeps telling us "You can't have a resizable array". Anyway, I don't want to use arrayList for now, just normal arrays would be enough. I managed to make it draw multiple balls like this, but I'm not too happy with it to be honest.

Ball Class



Main Class


+Pie Number of slices to send: Send
I don't like this line


in VB, there was a command "with" that allowed you to do something like this:
with ballArray[i]
g.fillOval(.getBallX(),.getBallY(), .getBallHeight(), .getBallWidth());
End with


I also don't like the fact that I have to use the instance of the class, (such as redBall) to be able to call the method
"drawMultipleBalls"



and the fact that I didn't find a way to pass parameters(such as the ball array) directly to paintComponent() to avoid doing this:


Any ideas? ^^ Thanks
+Pie Number of slices to send: Send
You should make a 'view' of your balls, and then make it your GUIs responsibility to draw the view, not the other way around.

Example:
+Pie Number of slices to send: Send
As an alternative to having an inner Component class, will this work? . . . and now we are using polymorphism, you can change the for-each loop to for (DrawableShape d : shapes) { d.drawShape(g); } and draw squares, too

reply
reply
This thread has been viewed 15130 times.
Similar Threads
Custom JComponent Problem
First applet
Newbie help
can not move the paddle in this program
Ball to Pumpkin?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jul 11, 2025 14:40:43.
close