I'm creating a program in Java Swing, and I have run into a problem. The program has two images; one of the images is the full image, and it gets displayed in the image navigator. The other image is a zoomed-in portion of the full image and is displayed in another component.
I have in my main class something that goes like this whenever the part to view in the ZoomedImage container updates:
BufferedImage zoom = imageNavigator.getZoomedImage();
((ZoomedImage)container.getTopComponent()).updatePanel(zoom);
which calls:
public BufferedImage getZoomedImage(){
BufferedImage img = image.getSubimage((int)(startX/scaleX), (int)(startY/scaleY), (int)(image.getWidth()*percentViewWidth), (int)(image.getHeight()*percentViewHeight));
BufferedImage copyOfImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(img, 0, 0, null);
return copyOfImage;
}
I also call similar code when initializing the component.
startX/Y are the starting points of the box that shows the area it is zoomed in on. ScaleX/Y have to do with the scale between the size of the image being displayed and the size of the component displaying the image. percentViewWidth/Height are part of the zooming information.
After the components have all been packed, the functionality works fine. The zoomed image updates just fine whenever I make changes to what portion of the original image I'm looking at. The problem is that in order to do that, I have to depend on the size of the component holding the main image so that I can grab the correct section of the image. When it is first initializing, the size is 0, so I can't grab the correct section. How can I tell it to update the image immediately after it has a size?