Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Graphics - passing instance of Point2D to 'paint (Graphics g)' method  RSS feed

 
Hugh Roarty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've got a bit of a problem with java graphics. I'm sure it's very basic but its got me stumped.

I want to use a 'for loop' to pass line co-ordinates to the paint method. I do not want the loop inside my paint method; it must pass co-ordinates to the paint method from another part of my programme.

Here is a simplified version of my code, it looks like it only displays the last line to be drawn. I was expecting it to display a new line for every iteration of my loop in my display() method. Can anyone help?



 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hugh Roarty wrote:I want to use a 'for loop' to pass line co-ordinates to the paint method. I do not want the loop inside my paint method; it must pass co-ordinates to the paint method from another part of my programme.


Well, right away you have a problem because that isn't how things work. The paint method is what draws the GUI, so it needs to know everything which is supposed to show up on the screen. And you don't call the paint method, so you can't pass it any parameters.

So I don't know why you want to use the for-loop to pass line coordinates to the paint method, but you're going to have to do things differently. You're going to have to adjust your code so that the for-loop puts the line coordinates into some data structure (maybe a list of line coordinates), and so that the paint method goes through that data structure and displays whatever is in it.
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Custom painting is done by overriding paintComponent(...), not paint() and don't forget to invoke super.paintComponent(...) as the first statement.

Move the display code into the paintComponent(...) method. of course you will need to modify the loop to paint each line.
 
Hugh Roarty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great advice guys. Paul, thats a good work around which I think would work but I'll go with the paintComponent method as suggested by Rob - this works for me, thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!