• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java2D Dinosaur? paint vs. repaint

 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to develop a graphical front end to an application, and I think that Java2D seems ideal for me, because i just want to use vector (not bitmapped) graphics + fonts and be able to print out what I see in the window.

I made a copy of the Java2D manual, but it bothers me that the date on it from Sun is 2001. We all know that the language has changed a bit since then, and I'm wondering how it affects Java2D. Also, in my opinion the manual is a little bit terse, and it seems to presume that the programmer has a masterful understanding of Swing.

One thing in particular that bugs me is that I can't figure out when I'm supposed to use paint() and when to use repaint() to draw my content.

Chris
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That, at least, hasn't changed. paint() (or for Swing components, generally paintComponent()) is something you override to provide painting capability for a component; you never call it yourself.

repaint(), on the other hand, is something you call to say "Please, Mr. Java, when you have a chance, pass a valid Graphics object to my paint() method so it can display my painting." You never override it.

Make sense?

I'm a-gonna move this here topic to our Swing/AWT forum for any further discussion.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> One thing in particular that bugs me is that I can't figure out when I'm
> supposed to use paint() and when to use repaint() to draw my content.

bacically
paint() is what/where
repaint() is when

consider a simple animation of a ball bouncing around the screen
in paintComponent() (see explanation from EFH) you would have something like this


where
super.paintComponent(g) clears the screen
g.setColor(Color.BLUE) self-explanatory
g.fillOval(x,y,50,50) is what is painted.
i.e. at point x,y size 50,50 a filled oval (circle) will be drawn

now, in a separate thread, and possibly triggered on program start, or a button click:
in a while() loop (often while(true))
the values of x, and of y, are changed
a call to repaint() - paintComponent will now draw at the new x,y //this is the 'when'
Thread.sleep(whatever milliseconds you want)

the separate thread is required, otherwise the while loop will block the painting

do this and the 'ball' will seem to bounce around the screen
[ July 27, 2006: Message edited by: Michael Dunn ]
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that seems to make sense about the difference between paint and repaint.
Now, looking through the book, it suggests that transforms should be chained to the Graphics2D instance for transforming objects, but just from my own noodling around, it seems more straightforward for me to use AffineTransform applied to my Shape and create a new shape from it. I think that this is the only way to transform individually different Shapes rendered in the same Graphics2D.
[ July 28, 2006: Message edited by: Christopher Arthur ]
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more point of confusion:

paint() vs. paintComponent()

The documentation gives conflicting recommendations for these methods.
I've read that paintComponent() should be overridden in subclasses of Swing components, but paint() should not. In another document it says that the paintXxx() methods should not be overridden but instead use paint() to do custom graphics.

My guess is that one is for top-level container overrides and the other is for component overrides, but I'm not sure.

Chris
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!