Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Class to draw a circle to Jframe  RSS feed

 
Greenhorn
Posts: 15
Eclipse IDE Slackware Windows
  • Mark post as helpful
  • send pies
  • 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: 2930
19
  • Mark post as helpful
  • send pies
  • 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: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 9122
172
  • Mark post as helpful
  • send pies
  • 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: 59715
187
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!