import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ReadDemo {
private Frame frame=new Frame("图片查看器");
MenuBar menuBar=new MenuBar();
Menu menu=new Menu("文件");
MenuItem open=new MenuItem("打开");
MenuItem save=new MenuItem("另存为");
//声明对象,记录本地读取到内存中的图片
BufferedImage image;
private class MyCanvas extends Canvas{
@Override
public void paint(Graphics g) {
g.drawImage(image,0,0,null);
}
}
MyCanvas drawArea=new MyCanvas();
public void init(){
//组装视图
//组装菜单
open.addActionListener(e->{
//打开文件对话框
FileDialog fileDialog=new FileDialog(frame,"打开图片",FileDialog.LOAD);
fileDialog.setVisible(true);
//获取用户选择的图片路径及其名称
String dir = fileDialog.getDirectory();
String fileName = fileDialog.getFile();
try {
image = ImageIO.read(new File(dir, fileName));
drawArea.repaint();
} catch (IOException ex) {
ex.printStackTrace();
}
});
save.addActionListener(e->{
//展示一个文件对话框
FileDialog fileDialog=new FileDialog(frame,"保存图片",FileDialog.SAVE);
fileDialog.setVisible(true);
//获取用户设置的保存路径及文件名称
String dir = fileDialog.getDirectory();
String fileName = fileDialog.getFile();
try {
ImageIO.write(image,"JPEG",new File(dir,fileName));
} catch (IOException ex) {
ex.printStackTrace();
}
});
menu.add(open);
menu.add(save);
menuBar.add(menu);
frame.setMenuBar(menuBar);
frame.add(drawArea);
frame.setBounds(200,200,740,508);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new ReadDemo().init();
}
}
图片查看器(Java实现)
最新推荐文章于 2024-06-29 20:42:30 发布