File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Resizing window erases content of Graphics2d Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resizing window erases content of Graphics2d" Watch "Resizing window erases content of Graphics2d" New topic
Author

Resizing window erases content of Graphics2d

Fernando Piovani
Greenhorn

Joined: Feb 09, 2011
Posts: 10
Every time i resize the window, or open a new window in front of it, the content (drawed lines) are erased. How can i avoid that?

Also, can some explain why this happens in technical terms?

[ ]´s
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Please post some code, because we shall need more details to work on.

I shall move this thread to our GUIs forum, because it sounds a GUI-related question.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Most probably you are missing the call to super.paintComponent()
If that doesn't fix the problem post your SSCCE code like Campbell suggested.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Fernando Piovani
Greenhorn

Joined: Feb 09, 2011
Posts: 10
Maneesh Godbole wrote:Most probably you are missing the call to super.paintComponent()
If that doesn't fix the problem post your SSCCE code like Campbell suggested.






I didnt start to work on the GUI yet. I am using netBeans as you can see in the code-generated below. The only thing the program does is display lines that represents a map, using gps coordinates.

Every time i resize the window, or open a new window in front of it, all the drawing is gone.

Oh, and one more question, how can i transform the label that i've draw on into a image, so i can work on the pixels to zoom it and etc.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

:groan:

Never never never use getGraphics() of a Component. Learn about Performing Custom Painting the correct way.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

I am using netBeans as you can see in the code-generated below.


You have every right to take the hard road, but at least you could remove the inane commencts and make your code shorter for posting.
Fernando Piovani
Greenhorn

Joined: Feb 09, 2011
Posts: 10
Maneesh Godbole wrote:Most probably you are missing the call to super.paintComponent()
If that doesn't fix the problem post your SSCCE code like Campbell suggested.


Yeah, that definitely works. I dont yet understand how and why but its ok... Still, resizing cause the program to reload all drawing, and since i've been working with a huge amout of data it take several time to resize (when growing to larger dimen. ).

My question is:

Why, when i resize to a smaller dimensions didnt reload the drawing process and how does exactly super.paintComponent() works on this matter?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Why, when i resize to a smaller dimensions didnt reload the drawing process and how does exactly super.paintComponent() works on this matter?


Still using getGraphics(), huh?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
and how does exactly super.paintComponent() works on this matter?


I have no idea because this should NOT have fixed your problem. Plus your posted code doesn't even have a paintComponent() method so I have no idea what you are doing.

The whole point of invoking super.paintComponent() is to clear the background of the panel by repainting the panel in its background color which is gray(ish).

Darryl has twice given you the solution.
Fernando Piovani
Greenhorn

Joined: Feb 09, 2011
Posts: 10
Darryl Burke wrote:
Why, when i resize to a smaller dimensions didnt reload the drawing process and how does exactly super.paintComponent() works on this matter?


Still using getGraphics(), huh?


Actually, no. I've read the perform custom painting that you show to me, and i done what it says so now it looks like that:

[On Drawing class]

[On Custom_JFrame class]


Its working, but still got this issue with resizing to a bigger window.>
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

In that case, this is a performance problem and you should follow what is mentioned in the tutorial:
"To keep our custom painting as efficient as possible, we will track the mouse coordinates and repaint only the areas of the screen that have changed. This is a recommended best practice that will keep your application running as efficiently as possible. "

Refer: Clipping


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resizing window erases content of Graphics2d
 
Similar Threads
Resizing Frames/Window
how to get the div offsetheight in JSP
check whether a page is loaded
IE-New Window/CTRL-N & Duplicate sessions
how to open an html page in a new window along with some divisions hidden.