Home>

1. mycanvas.java:

package pictureviewer;
import java.awt. *;
import java.awt.event. *;
import java.awt.image. *;
public class mycanvas extends canvas implements componentlistener {
  private bufferedimage bi;
  private image im;
  private int image_width;
  private int image_height;
  public void setimage (bufferedimage bi) {
    this.bi=bi;
    this.zoom ();
  }
  public void paint (graphics g) {
    g.drawimage (im, (this.getwidth ()-image_width)/2, (this.getheight ()-image_height)/2, this);
  }
  public void componentresized (componentevent e) {
    if (bi!=null) {
      system.out.println ("resize !!");
      this.zoom ();
      this.repaint ();
    }
  }
  public void componentmoved (componentevent e) {}
  public void componentshown (componentevent e) {}
  public void componenthidden (componentevent e) {}
  public void zoom () {
    if (bi == null)
      return;
    int screen_width=this.getwidth ();
    int screen_height=this.getheight ();
    double screen_proportion=1.0 * screen_height/screen_width;
    system.out.println ("screen:w" + screen_width + ", h" + screen_height + ", p0" + screen_proportion);
    image_width=bi.getwidth (this);
    image_height=bi.getheight (this);
    double image_proportion=1.0 * image_height/image_width;
    system.out.println ("image:w" + image_width + ", h" + image_height + ", p1" + image_proportion);
    if (image_proportion>screen_proportion) {
      image_height=screen_height;
      image_width=(int) (image_height/image_proportion);
      system.out.println ("p1>p0 w =" + image_width);
    } else {
      image_width=screen_width;
      image_height=(int) (image_width * image_proportion);
      system.out.println ("p0>p1 h =" + image_height);
    }
    im=bi.getscaledinstance (image_width, image_height, image.scale_smooth);
  }
}

2. myfilter.java:

package pictureviewer;
import java.io.file;
import java.io.filenamefilter;
public class myfilter implements filenamefilter {
  private string [] extension;
  public myfilter () {
    extension=new string [] {". jpg", ".jpg", ".gif", ".gif", ".png", ".png", ".jpeg", ".jpeg"};
  }
  public myfilter (string [] extension) {
    this.extension=extension;
  }
  public boolean accept (file dir, string name) {
    for (string s:extension) {
      if (name.endswith (s)) {
        return true;
      }
    }
    return false;
  }
}

3. pictureviewer.java:

package pictureviewer;
import java.awt. *;
import java.awt.event. *;
import java.awt.image. *;
import java.io. *;
import javax.imageio. *;
public class pictureviewer implements actionlistener {
  private frame frame;
  private mycanvas mc;
  private string fpath;
  private string fname;
  private file [] files;
  private int findex;
  private filedialog fd_load;
  private myfilter filter;
  private button previous;
  private button next;
  public static void main (string args []) throws exception {
    new pictureviewer (). init ();
  }
  public void init () {
    frame=new frame ("pictureviewer");
    panel pb=new panel ();
    button select=new button ("Select image");
    previous=new button ("Previous");
    next=new button ("Next Picture");
    select.addactionlistener (this);
    previous.addactionlistener (this);
    next.addactionlistener (this);
    pb.add (select);
    pb.add (previous);
    pb.add (next);
    mc=new mycanvas ();
    mc.setbackground (new color (200,210,230));
    mc.addcomponentlistener (mc);
    frame.add (pb, "north");
    frame.add (mc, "center");
    frame.setsize (360,360);
    frame.setlocation (400,200);
    frame.addwindowlistener (new windowadapter () {
      public void windowclosing (windowevent e) {
        system.exit (0);
      }
    });
    frame.setvisible (true);
    this.validatebutton ();
    filter=new myfilter ();
    fd_load=new filedialog (frame, "Open file", filedialog.load);
    fd_load.setfilenamefilter (filter);
  }
  public void actionperformed (actionevent e) {
    string command=e.getactioncommand ();
    if (command.equals ("choose picture")) {
      fd_load.setvisible (true);
      fpath=fd_load.getdirectory ();
      fname=fd_load.getfile ();
      if ((fpath!=null)&&(fname!=null)) {
        this.display (new file (fpath + fname));
        files=new file (fpath) .listfiles (filter);
        this.setindex ();
      }
    } else if (command.equals ("Previous")) {
      findex--;
      if (findex<0)
        findex=0;
      this.display (files [findex]);
    } else if (command.equals ("Next"))} {
      findex ++;
      if (findex>= files.length)
        findex=files.length-1;
      this.display (files [findex]);
    }
    this.validatebutton ();
  }
  public void display (file f) {
    try {
      bufferedimage bi=imageio.read (f);
      mc.setimage (bi);
      frame.settitle ("pictureviewer-[" + f.getname () + "]");
    } catch (exception e) {
      e.printstacktrace ();
    }
    mc.repaint ();
  }
  public void setindex () {
    file current=new file (fpath + fname);
    if (files!=null) {
      for (int i=0;i<files.length;i ++) {
        if (current.equals (files [i])) {
          findex=i;
        }
      }
    }
  }
  public void validatebutton () {
    previous.setenabled ((files!=null)&&(findex>0));
    next.setenabled ((files!=null)&&(findex<(files.length-1)));
  }
}
  • Previous JS + CSS blue table tab effect
  • Next Detailed explanation of $ajax () method parameters in JQuery