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
www.indiana.edu/~busey
AIM / IChat AV:  [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".

Reply via email to