已实现基础功能,等待后续改进。
可以直接编译运行。
代码如下:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
/*使用awt的图形缩放器,已实现功能
*后期会有部分改进
*Version:1.0
*Author:Tom
*Date:2016.12.20
* */
public class ZoomImage {
//声明创建打开文件和关闭程序按钮
private Button open = new Button("打开文件");
private Button close = new Button("退出");
//缓冲区,缩放后的图片存放在这里
BufferedImage image = new BufferedImage(300 , 300 , BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
//图形窗口,使用默认Borderlayout,后期改进
private Frame f = new Frame("图形缩放器");
//打开和保存文件的对话框
private FileDialog fileDialogA = new FileDialog(f, "打开文件", FileDialog.LOAD);
private FileDialog fileDialogB = new FileDialog(f, "输出文件", FileDialog.SAVE);
//初始文件
private Image img;
//图形预览区
MyPanel p = new MyPanel();
//存放打开、关闭按钮
private Panel p1 = new Panel();
//存放功能按键
private Panel p2 = new Panel();
//定义功能区组件:像素输入区,长宽比保持选项、提交保存按钮
private TextField tf1 = new TextField("输入宽",4);
private TextField tf2 = new TextField("输入高",4);
private Checkbox cb = new Checkbox("保持长宽比",false);
private Button submit = new Button("提交");
private boolean isMaintainEn = false;
//用来保存文件后缀名
private String srcExt;
/* 图形界面初始化
* 完成初始化界面的构造
* */
public void init()
{
//窗口初始化大小
f.setBounds(100, 100, 500, 500);
//对话框单选
fileDialogA.setMultipleMode(false);
//初始绘制,背景色为白色
g.setColor(p.getBackground());
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLACK);
g.drawRect(0, 0, image.getWidth()-2, image.getHeight()-2);
p.setPreferredSize(new Dimension(300,300));
p1.setBackground(Color.CYAN);
p2.setBackground(Color.CYAN);
//添加窗口组件
f.add(p, BorderLayout.CENTER);
f.add(p1, BorderLayout.SOUTH);
f.add(p2, BorderLayout.EAST);
//设置p2布局管理器为网格布局
GridBagLayout layout = new GridBagLayout();
p2.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
//添加功能按键
p1.add(open);
p1.add(close,BorderLayout.SOUTH);
//添加功能组件
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.ipadx = 20;
gbc.insets = new Insets(10, 10, 10, 10);
layout.setConstraints(tf1, gbc);
layout.setConstraints(tf2, gbc);
layout.setConstraints(cb, gbc);
gbc.ipady = 20;
layout.setConstraints(submit, gbc);
p2.add(tf1);
p2.add(tf2);
p2.add(cb);
p2.add(submit);
//添加窗口监听器
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示窗口
f.setVisible(true);
f.pack();
}
/*程序入口
* 为相关组件添加监听器
* */
public static void main(String[] args) {
//初始化窗口
ZoomImage view = new ZoomImage();
view.init();
//为打开文件的按钮添加监听器
view.open.addActionListener(e ->
{
//显示文件对话框
view.fileDialogA.setVisible(true);
//所选图片的路径
String imgSrc = view.fileDialogA.getDirectory()+view.fileDialogA.getFile();
if(view.fileDialogA.getFile() != null)
{
try
{
//获得文件后缀以保存修改后的文件
view.srcExt = view.fileDialogA.getFile().split("\\.")[1];
view.img = ImageIO.read(new File(imgSrc));
view.p.setPreferredSize(new Dimension(view.img.getWidth(null),view.img.getHeight(null)));
view.tf1.setText(Integer.toString((view.img.getWidth(null))));
view.tf2.setText(Integer.toString(view.img.getHeight(null)));
view.image = new BufferedImage(view.img.getWidth(null) , view.img.getHeight(null) , BufferedImage.TYPE_INT_RGB);
view.g = view.image.getGraphics();
view.p.repaint();
view.f.pack();
System.out.println("sssdfasd");
}
//捕获选择文件错误引发的异常
catch(Exception e1)
{
System.out.println("不受支持的图像类型!");
}
}
});
//确定是否保持长宽比
view.cb.addItemListener(e ->
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
view.isMaintainEn = true;
try
{
view.tf2.setText(Integer.toString(Integer.parseInt(view.tf1.getText())
*view.img.getHeight(null)/view.img.getWidth(null)));
}
catch(Exception e1)
{
System.out.println("输入像素值!");
}
view.tf2.setEditable(false);
}
else
{
view.isMaintainEn = false;
view.tf2.setEditable(true);
}
});
//为文本域添加监听器,值变化时自动重新加载图片
view.tf1.addTextListener(e ->
{
try
{
if(view.isMaintainEn)
{
view.tf2.setText(Integer.toString(Integer.parseInt(view.tf1.getText())
*view.img.getHeight(null)/view.img.getWidth(null)));
}
view.p.repaint();
}
catch(Exception e1)
{
System.out.println("数据格式错误!");
}
});
view.tf2.addTextListener(e ->
{
try
{
view.p.repaint();
}
catch(Exception e1)
{
System.out.println("数据格式错误!");
}
});
//为提交按钮添加监听器,保存修改
view.submit.addActionListener(e ->
{
view.fileDialogB.setVisible(true);
if(view.fileDialogB.getFile() != null)
{
String imgSrc = view.fileDialogB.getDirectory()+view.fileDialogB.getFile();
try
{
ImageIO.write(view.image, view.srcExt , new File(imgSrc+"."+view.srcExt));
}
catch(Exception e1)
{
System.out.println("不受支持的图像类型!");
}
}
});
//退出按钮添加监听器
view.close.addActionListener(e ->
{
System.exit(0);
});
}
/*内部类:MyPanel
* 图片加载区
* */
class MyPanel extends Panel
{
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image, 0, 0, null);
}
public void repaint()
{
g.setColor(p.getBackground());
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.drawImage(img, 0, 0, Integer.parseInt(tf1.getText()),
Integer.parseInt(tf2.getText()), null);
p.setPreferredSize(new Dimension(Integer.parseInt(tf1.getText()),Integer.parseInt(tf2.getText())));
paint(p.getGraphics());
}
}
}
网友评论