Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class to draw a circle to Jframe  RSS feed

 
David Schmidt
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.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • 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.
 
Mohamed Sanaulla
Bartender
Posts: 3169
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
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • 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:
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • 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!