最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)的圖像查看器完整實(shí)例

 更新時(shí)間:2015年10月08日 11:07:01   作者:神仙  
這篇文章主要介紹了Java實(shí)現(xiàn)的圖像查看器,以完整實(shí)例形式較為詳細(xì)的分析了java處理圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

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

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("選擇圖片");
    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程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 詳解Java 類的加載、連接和初始化

    詳解Java 類的加載、連接和初始化

    這篇文章主要介紹了Java 類的加載、連接和初始化的的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • java通過HTTP接收json詳細(xì)實(shí)例代碼

    java通過HTTP接收json詳細(xì)實(shí)例代碼

    Java作為一門廣泛使用的編程語言,很多開發(fā)人員會(huì)用它來進(jìn)行http請求,獲取json數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java通過HTTP接收json的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Java熱門筆試試題整理

    Java熱門筆試試題整理

    給大家整理了現(xiàn)在很熱門的java程序員面試時(shí)候的筆試試題以及答案,希望能幫助到你。
    2017-11-11
  • JDK的具體安裝步驟(帶圖帶解釋巨詳細(xì))

    JDK的具體安裝步驟(帶圖帶解釋巨詳細(xì))

    Java是一種廣泛使用的編程語言,許多應(yīng)用程序和系統(tǒng)都依賴于它,如果您想進(jìn)行Java編程或運(yùn)行Java應(yīng)用程序,首先需要安裝Java開發(fā)工具包(JDK),這篇文章主要給大家介紹了關(guān)于JDK具體安裝步驟的相關(guān)資料,文中介紹的方法帶圖帶解釋巨詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Spring為什么要用三級緩存解決循環(huán)依賴呢

    Spring為什么要用三級緩存解決循環(huán)依賴呢

    本文主要介紹了Spring如何使用三級緩存解決循環(huán)依賴問題,本文為了方便說明,先設(shè)置兩個(gè)業(yè)務(wù)層對象,命名為AService和BService,結(jié)合示例給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-01-01
  • springboot?publish?event?事件機(jī)制demo分享

    springboot?publish?event?事件機(jī)制demo分享

    這篇文章主要介紹了springboot?publish?event?事件機(jī)制demo,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java微信二次開發(fā)(三) Java微信各類型消息封裝

    Java微信二次開發(fā)(三) Java微信各類型消息封裝

    這篇文章主要為大家詳細(xì)介紹了Java微信二次開發(fā)第三篇,Java微信各類型消息封裝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • java中pdf轉(zhuǎn)圖片的實(shí)現(xiàn)方法

    java中pdf轉(zhuǎn)圖片的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄猨ava中pdf轉(zhuǎn)圖片的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • java springmvc亂碼解決歸納整理詳解

    java springmvc亂碼解決歸納整理詳解

    本篇文章介紹了java 中spring mvc 解決亂碼的問題方法實(shí)例,需要的朋友可以參考下
    2017-04-04
  • Tomcat啟動(dòng)分析(我們?yōu)槭裁匆渲肅ATALINA_HOME環(huán)境變量)

    Tomcat啟動(dòng)分析(我們?yōu)槭裁匆渲肅ATALINA_HOME環(huán)境變量)

    本文主要介紹Tomcat啟動(dòng)分析的知識,這里整理了相關(guān)資料及分析原因和如何實(shí)現(xiàn)的方法,有興趣的小伙伴可以參考下
    2016-09-09

最新評論

商丘市| 佛坪县| 公安县| 罗定市| 闵行区| 深圳市| 凤庆县| 逊克县| 舒兰市| 克什克腾旗| 光山县| 孝感市| 宜兴市| 象山县| 会昌县| 云浮市| 忻州市| 阜新| 上思县| 婺源县| 长葛市| 松滋市| 巴彦淖尔市| 辽阳县| 应用必备| 万荣县| 凤庆县| 赤峰市| 红河县| 麻城市| 凉山| 互助| 平和县| 余干县| 富平县| 三亚市| 台东县| 仙游县| 尼玛县| 黑山县| 富裕县|