Can't clear glassPane

Alejandro Barrero

Ranch Hand

Posts: 371

posted 9 years ago

I am drawing rectangles on the frame glass pane as the user drags the mouse, but I don't know how to clear the glass pane for every new rectangle. I am redrawing old rectangles with new Color(225,225,225,225) but they are still visible.

Thanks for your help,

Alejandro Barrero

Your help will be greatly appreciated,
Alejandro Barrero

Campbell Ritchie

Marshal

Posts: 55672

161

posted 9 years ago

Why are you using the glass pane?

Add a JPanel to the frame (it will default to the content pane in Java5) and draw on that with the mouse listener.

Alejandro Barrero

Ranch Hand

Posts: 371

posted 9 years ago

I was drawing the rectangles originally in the JPanel which I have loaded with A JPG image; however, the graphics.drawRect() method was not displaying anything. I thought that the problem was because the drawRect() code was being executed inside the mouseDragged method of a MouseMotionAdapter. My thought was that the JVM was refusing to repaint while the awt queue was being bombarded with events. I started executing the graphics.drawRect() in the glass pane and it works like a charm. Also, even if I raw on the JPanel, a rectangle will be drawn every time that a mouseDragged event occurs and I would need to get rid of it too.

I'll bet that you're not drawing properly, that you're not doing things from within a proper paintcomponent override. My recommendation is to stop what you are doing and go to the Sun 2D Graphics tutorials and see how to do it correctly from the book. Then try it again.

Again, please read the Sun tutorials on how to do graphics with Swing. It won't work right til you read and understand it.

Alejandro Barrero

Ranch Hand

Posts: 371

posted 9 years ago

Maneesh:

Who is calling drawInGlassPane? It is called from a mouse dragged listener. I start drawing rectangles when the user presses a mouse buttom and the dragged listener draws the rectangle from the point the mouse was presses to the point of the dragged event. I want the user to specify the rectangle with the mouse.

Any particular reason why you cant

1) Extend JPanel I am using a class that extends JPanel. In this object I draw an image from a JPG file. I want the user to select a rectangle in the image and I extract the subimage. It is all working. 2) override paintComponent to do your painting stuff 3) Set this panel as the glass pane? The reason that I want to use the glass pane and not the pane with the image is that repainting thw hole image to display the rectangles takes a lot of time. For the same reason I don't want to call super.paintComponent (besides, it will repaint the previous rectangles).

Your help will be greatly appreciated,
Alejandro Barrero

Alejandro Barrero

Ranch Hand

Posts: 371

posted 9 years ago

Hi Pete:

Thank you for your suggestions. The problem is that before I posted the question I did my homework and tried to find a solution in several books: CORE JFC and Core Swing Advanced programming by Topley, the Core Java by SUN, Java 2D Graphics by knudsen and Java 2D API Graphics by Hardy. I also did extensive internet searching with Goggle and other browsers. One possible conclusion is that I am to dumb to find the answer in those sources, but then I think that the forum at JavaRach is precisely for dumb people like me. My program is already working. I have a dialog with a class that extends JPanel and I display in the object of this class the image in a JPG file. The user can use the mouse to specify a rectangle in the image that contains the users signature. The program extract the sub image in the rectangle and it is stored as a blob in a database (of course I use encryption). With another program, the user can paste the signature on images of JPG files and then save them. To capture the rectangle, the program records as top left corner the point where the mouse is pressed and while the user is dragging the mouse, on every drag event, the the program draws a rectangle from the top left corner to the point og the drag event (the points are reversed if necessary). i draw the rectangles in the glass pane because if I use the object (JPanel) with the image, repainting takes a long time. My problem is that all the previous rectangles are drawn obscuring the background and I would like erase the previous rectangle before drawing the new one. What I am doing is drawing the previous rectangle with a transparent white color, it still shows but it is a solution I can live with. If you could point to me where in the sources I mentioned I could learn how to clear the glass pane I would appreciate it very much.

Thanks Pete,

Alejandro

Your help will be greatly appreciated,
Alejandro Barrero

pete stein

Bartender

Posts: 1561

posted 9 years ago

Originally posted by Alejandro Barrero: 3) Set this panel as the glass pane? The reason that I want to use the glass pane and not the pane with the image is that repainting thw hole image to display the rectangles takes a lot of time. For the same reason I don't want to call super.paintComponent (besides, it will repaint the previous rectangles).

Check out the API on the repaint method. There are overloads that take parameters, one of which could possibly speed this process up considerably:repaint method in Component API

pete stein

Bartender

Posts: 1561

posted 9 years ago

Maybe I'm over-simplifying things here, but even using the default non-parameter repaint method call, this demo program seems pretty responsive. If I am over-simplifying, please let me know, and better still, please post a small compilable demo program of your own. Good luck!

Edit: no response? Perhaps I just wasted my time then. I won't make the same mistake in the future. [ June 26, 2008: Message edited by: pete stein ]

Alejandro Barrero

Ranch Hand

Posts: 371

posted 9 years ago

My apologies Pete. I have been busy with other things.

Your help will be greatly appreciated,
Alejandro Barrero

Alejandro Barrero

Ranch Hand

Posts: 371

posted 9 years ago

Thank you very much Pete. Your class works beautifully; I am sure i can find the solution in your code.