国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現的圖像查看器完整實例

Java實現的圖像查看器完整實例

2020-01-08 14:34神仙 JAVA教程

這篇文章主要介紹了Java實現的圖像查看器,以完整實例形式較為詳細的分析了java處理圖片的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Java實現的圖像查看器。分享給大家供大家參考。具體如下:

1. MyCanvas.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
?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("選擇圖片");
    previous = new Button("上一張");
    next = new Button("下一張");
    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,"打開文件",FileDialog.LOAD);
    fd_load.setFilenameFilter(filter);
  }
  public void actionPerformed(ActionEvent e){
    String command = e.getActionCommand();
    if(command.equals("選擇圖片")){
      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("上一張")){
      findex--;
      if(findex<0)
        findex = 0;
      this.display(files[findex]);
    }else if(command.equals("下一張")){
      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)));
  }
}

希望本文所述對大家的java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 剑来在线观看 | av网站观看| 黄色一级毛片在线观看 | 男人天堂av网 | 日韩一区二区三区视频 | 免费一级片视频 | 综合色久 | 亚洲免费视频网 | 99re在线观看 | 久久精品成人 | 久久久国产一区二区三区 | 国产午夜精品一区二区三区视频 | 专干老肥女人88av | 天天操天天添 | 久操资源 | 日本日韩中文字幕 | 欧美日韩精品综合 | 国产91久久久久蜜臀青青天草二 | 日韩欧美在线综合 | 欧美一区二区三区在线观看视频 | 久草在线资源福利站 | 91麻豆产精品久久久久久 | 黄a在线观看 | 成人精品福利视频 | 精品国产99 | 自拍偷拍av | 日本午夜在线 | 国产成人在线播放 | 久久久久久亚洲av毛片大全 | 午夜免费小视频 | 国产精品免费av | 一区二区久久久 | 免费特级黄毛片 | 久久免费99精品久久久久久 | 综合久久亚洲 | 日韩精品免费视频 | 午夜视频在线免费观看 | 久久影院久久 | 日韩中文字幕在线播放 | 国产日日夜夜操 | 福利片在线观看 |