Hi Tomas, I think I am able to reproduce problem you observe (second image does not appear after first mouse click).
However, it seems that problem here has no relation to the image loading, but rather is related to components layout. I've noticed that after construction, second instance of JLabel (jl2) has meaningful preferred size but actual size is w=0,h=0. I guess this is because this component is not bound to container yet and dimension is not calculated. As a result offset calculation procedure (method movePanel() in your example) shifts viewpoint outside of the frame border. Explicit request to doLayout() method after setting new view helps to workaround the problem (cause recalculation of actual size). On other hand explicit layout requests are typically bad thing to do. You may want to ask Swing team what is the best way to proceed (Swing forum is here: http://forums.java.net/jive/forum.jspa?forumID=74&start=0) Thanks, Andrew Thomas Busey wrote:
I'm having problems loading images when using JLabels in conjunction with a JViewport. The following code demonstrates the problem. It will create a frame that displays one of two images. The user cycles between the two images by clicking within the frame, so that the second image is effectively a zoom of the first. The first JLabel loads fine, and is the first displayed. The first time the images are cycled (after a user clicks), the second image comes up as invalid. Subsequent cycling results in normal behavior. I've tried utilizing the built in Mediatracker, as well as explicitly setting the JViewport's view to try to explicitly load the second image. It seems like the second image doesn't load the first time, although changing the image loading order doesn't change the behavior; the image that is shown after the click is never valid the first time, but is the second time. I'd appreciate any insights people might have. Below is a complete program that isolates the issue. Thanks, Tom // Begin Code -------------------------------- import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test implements MouseListener { int view = 1; JViewport viewport; /* REPLACE WITH YOUR OWN IMAGES */ String p1 = "C:/Documents and Settings/dwyatte/Desktop/fingerzoom/Img/ABLF1.jpg"; String p2 = "C:/Documents and Settings/dwyatte/Desktop/fingerzoom/Img/ABLF2.jpg"; JLabel jl1, jl2; ImageIcon ii1, ii2; JFrame frame; /* creates a frame with two images. cycle between them by clicking within the frame. * first click will result in an invalid second image, but subsequent clicks result * in normal behavior */ public Test() { ii1 = new ImageIcon(p1); ii2 = new ImageIcon(p2); jl1 = new JLabel(ii1); jl2 = new JLabel(ii2); frame = new JFrame("Test"); frame.setSize(800, 600); frame.getContentPane().setLayout(new BorderLayout()); viewport = new JViewport(); viewport.setView(jl1); frame.getContentPane().add(viewport, BorderLayout.CENTER); frame.addMouseListener(this); frame.setVisible(true); } public void mouseReleased(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseClicked(MouseEvent e) { System.out.println("jl1 is " + jl1); System.out.println(); /* Here, you can see that the size of jl2 * is invalid on the first click */ System.out.println("jl2 is " + jl2); System.out.println(); if(view == 1) { viewport.setView(jl2); movePanel(0,0); view = 2; } else { viewport.setView(jl1); movePanel(0,0); view = 1; } } public void movePanel(int xmove, int ymove) { Point pt = new Point(xmove, ymove); /* getMaxXExtent and getMaxYExtent will systematically * fail on the first click because jl2's size is invalid */ pt.x = Math.max(0, pt.x); pt.x = Math.min(getMaxXExtent(), pt.x); pt.y = Math.max(0, pt.y); pt.y = Math.min(getMaxYExtent(), pt.y); viewport.setViewPosition(pt); } /* viewport.getView().getWidth is image width * viewport.getWidth() is frame width. */ public int getMaxXExtent() { return viewport.getView().getWidth() - viewport.getWidth(); } public int getMaxYExtent() { return viewport.getView().getHeight() - viewport.getHeight(); } public static void main(String args[]) { new Test(); } } Thomas Busey, PhD Associate Professor Department of Psychological and Brain Sciences Program in Cognitive Science Indiana University, Bloomington 1101 E. 10th St Bloomington, IN, 47405 (812) 855-4261 [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> www.indiana.edu/~busey AIM / IChat AV: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> (video feed) =========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".
=========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".