repaint without flickering

G

Georg Andersson

hi

i have a litte code, that draws a line while the mouse is pressed and
dragged. the line sould allways lead from the startpoint to the
current point. so it is necessary, that the line is deleted and
repainted on every MouseMotionEvent. i thought i just use the
repaint-method. but this results in very ugly flickering...

what would be the correct way for this problem?

any help will be greatly appreciated...
 
A

Andrew Thompson

i have a litte code,

Applet or JApplet? Command line or
GUI'd application perhaps?
..that draws a line
while the mouse is pressed and dragged.

Best group for this is ..
..i thought i just use the
repaint-method. but this results in very ugly flickering...

But I think this is an FAQ.
<http://www.physci.org/guifaq.jsp#2.4>

For an example of renderring in ..
well you did not state Swing or
AWT, so, for both.
<http://www.physci.org/launcher.jsp#JAnimateFrame>
<http://www.physci.org/launcher.jsp#AnimateFrame>

[ follow the links to see the
code and the exmple run ]
 
V

VisionSet

Georg Andersson said:
i have a litte code, that draws a line while the mouse is pressed and
dragged. the line sould allways lead from the startpoint to the
current point. so it is necessary, that the line is deleted and
repainted on every MouseMotionEvent. i thought i just use the
repaint-method. but this results in very ugly flickering...

what would be the correct way for this problem?

If you listen for points with a mousemotion listener as you probably are
doing.
Add them to a two Polygons.

Have your paint method paint the master Polygon and empty the other
(unpainted)Polygon.

call repaint with repaint(unpainted.getBounds());

The crucial thing is to use the repaint(Rectangle) method that only repaints
the area you specify; after all, the stuff painted last time hasen't
changed, why paint it again?
 
M

Mark Sandford

Georg said:
hi

i have a litte code, that draws a line while the mouse is pressed and
dragged. the line sould allways lead from the startpoint to the
current point. so it is necessary, that the line is deleted and
repainted on every MouseMotionEvent. i thought i just use the
repaint-method. but this results in very ugly flickering...

what would be the correct way for this problem?

any help will be greatly appreciated...

It flickers because of the number of drawing operations being carried
out upon the screen, to prevent this you would need to implement
'double-buffering'. This is where the image is drawn off-screen and
only the final image drawn as a whole to the screen.

Sun, as ever, have a tutorial showing how to achieve this with the
BufferedImage class:
http://java.sun.com/docs/books/tutorial/2d/images/doublebuffering.html

Hope this helps, MS.
 
P

perry

there's a few examples on sun's website that focus's solely on
minimizing flickering. do a simple search on java.sun.com

- perry
 
S

Silvio Bierman

Georg Andersson said:
hi

i have a litte code, that draws a line while the mouse is pressed and
dragged. the line sould allways lead from the startpoint to the
current point. so it is necessary, that the line is deleted and
repainted on every MouseMotionEvent. i thought i just use the
repaint-method. but this results in very ugly flickering...

what would be the correct way for this problem?

any help will be greatly appreciated...

If you are talking about a moving drag-style line or box simply use XOR
drawing mode. It is very simple and you do not need repaint calls (which
actually cause most flickering).

Silvio Bierman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top