• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class to draw a circle to Jframe

 
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
David Schmidt
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic