Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't clear glassPane

 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, I am afraid.

Anybody else?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try super.paintComponent before your call to draw the rectangles.
 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super.paintComponent makes the entire frame blank.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alejandro Barrero:
super.paintComponent makes the entire frame blank.


Can you post the relevant code so we can figure out whats going wrong?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code that I am using:
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alejandro Barrero:
This is the code that I am using:


super.paintComponent makes the entire frame blank.

I do not see any call to super.paintComponent in your code.

Anyway, that's a moot point.

Who is calling drawInGlassPane?
When is it called?
Any particular reason why you cant
1) Extend JPanel
2) override paintComponent to do your painting stuff
3) Set this panel as the glass pane?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies Pete. I have been busy with other things.
 
Alejandro Barrero
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Pete. Your class works beautifully; I am sure i can find the solution in your code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic