This topic is now archived and is closed to further replies.


[java] Swing and resizing custom components

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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);

Share this post

Link to post
Share on other sites