Skip to main content
gamedev.net
🔒 Locked

[java] Swing and resizing custom components

Started by huiski 1 reply 350+ views
Original Post
huiski
huiski
Hallo, I have a java application (JFrame) that has a JPanel made up of one JPanel (contains some text components) and one custom component derived from JPanel laid out using border layout. The panels are added with BorderLayout.EAST and BorderLayout.CENTER parameters respectively. The custom component implements its own paintComponent() method to paint a hex map. Now, everything looks nice at the default window size, but once I make the window larger, problems appear. The normal JPanel-component stays at the right edge as expected and the custom map component expands to fill the new space (I can see the borders move), but the area that gets painted remains the same. Taking a look with a debugger reveals that the while the component expands the clipping rectagle of the Graphics object used for painting remains the same. So the painting routine happily paints using the new dimensions of the component unaware of the fact that most of the work is wasted. So probably there is something in the java swing painting mechanics that I''m overlooking. Any ideas would be appreciated.
mill-o
mill-o
I''m not sure.. but you can check if the component have been resized and do something about it. perhaps something like this (not tested):


Dimension d = getSize();
if (oldDimension.width != d.width || backDimension.height != d.height ) {
Image temp = backImage;
backImage = createImage(d.width, d.height);
oldDimension = d;
backGraphics = backImage.getGraphics();
backGraphics.drawImage(temp, 0, 0, this);
}
huiski
huiski
yes, thanks. The solution was very simple. I was simply overlooking the fact that the backbuffer dimensions need to change as well.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.