Ghost image drawing- how to optimize?

B

B Young

I'm using Javaworld Tip #114 to do ghost image drawing in a drag and
drop JTree. The following code is in the dragOver method which I hear
gets called around 10 times per second when drag is occuring:
----------
Graphics2D g2 = (Graphics2D) getGraphics();
// draw my own drag image

// Rub out the last ghost image and cue line
paintImmediately(_raGhost.getBounds());

//remember where we are about to draw new ghost image
_raGhost.setRect(pt.x - _ptOffset.x, pt.y - _ptOffset.y,
_imgGhost.getWidth(), _imgGhost.getHeight());

g2.drawImage(_imgGhost,
AffineTransform.getTranslateInstance(_raGhost.getX(),
_raGhost.getY()), null);
----------
Now, the ghost image flickers when I am dragging. I think this is due
to poor design in this example. Painting using paintImmediately() and
drawing to the graphics context outside of paint methods seems like a
bad idea?

How should I fix this? Try to get everything into paintComponent()
and just issue a repaint() with the correct coordinates to paint? Or
is there a better or easier way?

Thanks
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top