讲师中心 微信公众号

工具推荐

视频效率加速
AniShort
AniShort

AI短剧协同制作平台

Laper
Laper

剧本策划到成片协作

剧云
剧云

中文短剧剧本制作平台

Seko
Seko

AI短剧一键生成成片

Java图像查看器

轻婷小哥_6430

轻婷小哥_6430

发布时间:2016-11-10 10:49:51

|

1842人浏览过

|

来源于php中文网

原创

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);
    }
      
}

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;
    }  
}

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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
咔片AIPPT

一款在线AI演示文稿制作工具,可根据主题和内容需求辅助生成PPT结构与页面,提高演示材料制作效率。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

立刻MV
立刻MV Hot

立刻MV是一款AI文本写作工具,AI 音乐视频(MV)创作工具。

讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

超级简历WonderCV

一款AI办公效率工具,主要用于免费求职简历模版下载制作,应届生职场人必备简历制作神器,适合需要提升相关任务效率的用户。

小橡皮AI

一款AI工具,主要用于小橡皮一键去AI味,将机器味重的内容改得更有活人感,防止因为 AI 内容被平台打上 AI 标签,以及限流,适合需要提升相关任务效率的用户。

音述AI
音述AI Hot

一款AI音频处理工具,主要用于音述AI是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

60

2026.11.12

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

40

2026.11.12

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

20

2026.11.12

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

140

2026.11.06

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

120

2026.11.06

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

100

2026.11.06

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

100

2026.11.05

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

80

2026.11.05

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

80

2026.11.04

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn