This article describes the Java implementation of the image viewer. Share to everyone for your reference. Specifically as follows:
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,t
His);
public void componentresized (ComponentEvent e) {if (bi!= null) {System.out.println ("resize!!");
This.zoom ();
This.repaint (); Componentmoved (ComponentEvent e) {} public void Componentshown (ComponentEvent e) {} is public void comp
Onenthidden (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 ();
The public void init () {frame = new frame ("PictureViewer");
panel PB = new Panel ();
Button select = New Button ("Select Picture");
Previous = New button ("Last Sheet");
Next = New button ("Next sheet");
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);
The public void actionperformed (ActionEvent e) {String command = E.getactioncommand ();
if (Command.equals ("select 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]); }elseif (command.equals ("next Sheet")) {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)); }
}
I hope this article will help you with your Java programming.