File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes repaint() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "repaint() problem" Watch "repaint() problem" New topic

repaint() problem

RavindraDSL Nandam

Joined: May 11, 2004
Posts: 6

In my swing application, i am plotting a graph with more than 20000 points.
The graph should be such that. user is allowed to select certain area by mouse clicked, mouse Dragged and mouse released event. once the mouse released event is fired, it should open another graph with the selected points.

I am showing the selected area graph by a rectangle. Means.. when the user clicks on the graph and drags the mouse, then choosen area will be shown as a rectangle. As long as the user drags, then the rectangle will also be elongated or decreases the size.

Issue is when we elongate the rectagle it calls the repaint method, which calls the repaint of the graph also as well. As the graph points are more than 20000, so the drawing of the rectangle is not in sync with the mouse dragged. if the no of points are less then drawing of the rectangle will be in sync.
Is there any way i can repaint only the rectagle. means... there should be a transparent layer on the graph, so that when ever the mouse drag takes place then it should not repaint the original graph.
I have tried repaint(x,y, width, height) method for repainting the selected part. but no use..

I am in tight deadlines.. I would be glad if someone helps me on this.

KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
If you are working with a JFrame, maybe you could make use of the GlassPane?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's a possiblity. When you have finished drawing your 20,000 graph points and are ready to begin drawing your selection/clipping rectangle convert your graph data to a BufferedImage. This will be easier to (re)paint as the rectangle is drawn on/over it.

In the example below I made up a fake glass pane (non–opaque component) and put it over the graphing panel using OverlayLayout. Then on the first mouse press we switch from data to buffered image in DotPanel (initiated in mousePressed). Actually you could skip the overlay panel and draw the rectangle directly on the buffered image if you like.

In DotPanel I used g2.drawRenderedImage which takes a BufferedImage and an AffineTransform. This makes it easy to scale/zoom into the clipped area using AffineTransform methods. Also BufferedImage has a getSubimage method.

Seems to work okay.

[ May 27, 2004: Message edited by: Craig Wood ]
RavindraDSL Nandam

Joined: May 11, 2004
Posts: 6
Hi Craig Wood, KR Campbell

Thanks a lot for the kind timely help.. It is working fine.

Best wishes and Regards,
Ravindra N
I agree. Here's the link:
subject: repaint() problem
It's not a secret anymore!