Java界面程序实现图片的放大缩小
Java界面程序实现图片的放大缩小。这个程序简单地实现了图片的打开、保存、放大一倍、缩小一倍和固定缩放尺寸,但是并没有过多的涵盖对图片的细节处理,只是简单地实现了图片大小的放缩。
思维导图如下:

效果图如下:


代码如下:
package picture; import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO; public class DialogDemo implements ActionListener{ private JFrame frame;
private Panel panel, panelButton, panelText;
private JLabel labelHight, labelWidth;
//固定缩放宽度和长度
private JTextField textHight;
private JTextField textWidth;
//操作记录提示框
private JTextArea textArea;
//操作按钮
private JButton buttonReduce, buttonEnlarge, buttonZoom;
//菜单栏:打开图片、保存图片、关于、退出
private JMenuItem itemSave, itemOpen,itemAbout, itemExit;
//打开图片窗口,保存图片窗口
private FileDialog dialogOpen;
private FileDialog dialogSave;
//BufferedImage用于保存图片
private BufferedImage bufferedImage;
//图片显示imageCanvas类(继承Canvas)
private imageCanvas canvas;
private Image image;
private Graphics graphics; public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DialogDemo window = new DialogDemo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//图片显示imageCanvas类(继承Canvas),用于图片重新绘制
class imageCanvas extends Canvas
{
//重写Canvas的paint方法
public void paint(Graphics g)
{
//将image绘制到该组件上
g.drawImage(bufferedImage, 0, 0, null);
//f.setVisible(true);
}
} //构造函数初始化图像界面
public DialogDemo() { frame = new JFrame();
frame.setBounds(100, 100, 900, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0)); canvas = new imageCanvas();
canvas.setPreferredSize(new Dimension(800, 600));
frame.getContentPane().add(canvas, BorderLayout.CENTER); panel = new Panel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
panel.setLayout(new GridLayout(1, 0, 0, 0)); textArea = new JTextArea();
textArea.setEditable(false);
textArea.setText("\u63D0\u793A\uFF1A\r\n");
panel.add(textArea); panelButton = new Panel();
panel.add(panelButton);
panelButton.setLayout(new GridLayout(3, 1, 0, 0)); buttonReduce = new JButton("\u56FE\u7247\u7F29\u5C0F\u4E00\u500D");
panelButton.add(buttonReduce);buttonReduce.addActionListener(this); buttonEnlarge = new JButton("\u56FE\u7247\u653E\u5927\u4E00\u500D");
panelButton.add(buttonEnlarge);buttonEnlarge.addActionListener(this); panelText = new Panel();
panelButton.add(panelText);
panelText.setLayout(new GridLayout(1, 0, 0, 0)); labelHight = new JLabel("\u957F\u5EA6(px)");
panelText.add(labelHight); textHight = new JTextField();
panelText.add(textHight);
textHight.setColumns(10); labelWidth = new JLabel("\u5BBD\u5EA6(px)");
panelText.add(labelWidth); textWidth = new JTextField();
panelText.add(textWidth);
textWidth.setColumns(10); buttonZoom = new JButton("\u56FA\u5B9A\u7F29\u653E");
panelText.add(buttonZoom);
buttonZoom.addActionListener(this); JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); JMenu newMenu = new JMenu("\u6587\u4EF6\r\n");
menuBar.add(newMenu); itemOpen = new JMenuItem("\u6253\u5F00\u56FE\u7247\r\n");
newMenu.add(itemOpen);itemOpen.addActionListener(this); itemSave = new JMenuItem("\u4FDD\u5B58\u56FE\u7247\r\n");
newMenu.add(itemSave);itemSave.addActionListener(this); itemAbout = new JMenuItem("\u5173\u4E8E");
newMenu.add(itemAbout);itemAbout.addActionListener(this); JSeparator separator = new JSeparator();
newMenu.add(separator); itemExit = new JMenuItem("\u9000\u51FA\r\n");
newMenu.add(itemExit);itemExit.addActionListener(this); dialogOpen = new FileDialog(frame, "选择一张图片", FileDialog.LOAD);
dialogSave = new FileDialog(frame, "选择保存图片的路径", FileDialog.SAVE);
} /**
* 界面交互,响应事件(调用对应的函数)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == itemSave) {
saveImage();
} else if (e.getSource() == itemOpen) {
openImage();
} else if (e.getSource() == itemExit) {
System.exit(0);
} else if (e.getSource() == itemAbout) {
JOptionPane.showMessageDialog(null, "图片缩放程序:PhotoZoomer 1.0",
"版本", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == buttonEnlarge) {
enlargeImage();
} else if (e.getSource() == buttonReduce) {
reduceImage();
} else if (e.getSource() == buttonZoom) {
zoomImage();
} } /**
* 响应事件封装成函数
*/
//打开图片
private void openImage() {
try {
// 创建一个不带透明色的BufferedImage对象
bufferedImage = new BufferedImage(1920, 890, BufferedImage.TYPE_INT_RGB);
bufferedImage.flush();
graphics = bufferedImage.getGraphics();
//打开对话框
dialogOpen.setVisible(true);
image = ImageIO.read(new File(dialogOpen.getDirectory() + dialogOpen.getFile()));
//判断图片是否存在
if (image != null) {
graphics.drawImage(image,0,0, null);
canvas.repaint();
}
//添加提示
textArea.append("打开图片成功!\n图片路径:" +
dialogOpen.getDirectory()+"\n"+"图片名称:"+dialogOpen.getFile()+"\n");
} catch (IOException e) {
e.printStackTrace();
System.out.println("打开图片发生错误!");
}
} //保存图片
private void saveImage() {
try {
dialogSave.setVisible(true);
ImageIO.write(bufferedImage, "jpeg",
new File(dialogSave.getDirectory() + dialogSave.getFile()));
//添加提示
textArea.append("添加图片成功!\n保存目录:"+dialogSave.getDirectory()+"\n");
} catch (IOException e) {
e.printStackTrace();
System.out.println("保存图片发生错误!");
}
} //固定放缩图片
private void zoomImage() {
int height = Integer.parseInt(textHight.getText());
int width = Integer.parseInt(textWidth.getText());
//判断输入是否符合条件
if (height > 0 && width > 0 && height <= 890 && width <= 1920 ) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
graphics = bufferedImage.getGraphics();
graphics.drawImage(image, 0, 0, width, height, null);
canvas.repaint();
textArea.append("\n图片缩放为高:"+height+"px,宽:"+width+"px\n");
textHight.setText("");
textWidth.setText("");
} else {
textArea.append("\n请输入正确的图片宽度和长度!");
textHight.setText("");
textWidth.setText("");
}
} //放大图片一倍
private void enlargeImage() {
int height = image.getHeight(null) * 2;
int width = image.getWidth(null) * 2;
//判断输入是否符合条件
if (height > 0 && width > 0 && height <= 890 && width <= 1920 ) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
graphics = bufferedImage.getGraphics();
graphics.drawImage(image, 0, 0, width, height, null);
canvas.repaint();
textArea.append("\n图片缩放为高:"+height+"px,宽:"+width+"px\n");
textHight.setText("");
textWidth.setText("");
} else {
textArea.append("\n不能再进行放大了!");
textHight.setText("");
textWidth.setText("");
}
} //缩小图片一倍
private void reduceImage() {
int height = image.getHeight(null) / 2;
int width = image.getWidth(null) / 2;
//判断输入是否符合条件
if (height > 0 && width > 0 && height <= 890 && width <= 1920 ) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
graphics = bufferedImage.getGraphics();
graphics.drawImage(image, 0, 0, width, height, null);
canvas.repaint();
textArea.append("\n图片缩放为高:"+height+"px,宽:"+width+"px\n");
textHight.setText("");
textWidth.setText("");
} else {
textArea.append("\n不能再进行缩小了!");
textHight.setText("");
textWidth.setText("");
}
}
}
Java界面程序实现图片的放大缩小的更多相关文章
- jquery 实现点击图片居住放大缩小
该功能是基于jquery实现的,所以 第一步则是引入jquery jquery下载地址:https://jquery.com/download/ 或者使用此时调试的版本(3版本) /*! jQuery ...
- Android 本地/网路下载图片实现放大缩小
Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...
- java画图程序_图片用字母画出来_源码发布_版本二
在上一个版本:java画图程序_图片用字母画出来_源码发布 基础上,增加了图片同比例缩放,使得大像素图片可以很好地显示画在Notepad++中. 项目结构: 运行效果1: 原图:http://imag ...
- java画图程序_图片用字母画出来_源码发布
在之前写了一篇blog:java画图程序_图片用字母画出来 主要是把一些调试的截图发布出来,现在程序调试我认为可以了(当然,你如果还想调试的话,也可以下载源码自己调试). 就把源码发布出来. 项目结构 ...
- java画图程序_图片用字母画出来
最近在研究怎样将图片用字母在文本编辑工具中“画”出来. 你看了这个可能还不知道我想说什么? 我想直接上图,大家一定就知道了 第一张:小猫 原图:http://www.cnblogs.com/hongt ...
- JS控制图片拖动 放大 缩小 旋转 支持滚轮放大缩小 IE有效
<html> <head> <title>图片拖动,放大,缩小,转向</title> <script type="text/ja ...
- JS中图片的放大缩小没反应
这段代码无反应: 代码如下: <script type="text/javascript"> onload = function () { document.getEl ...
- Android DIY之路 (一) 指定区域多图片合成 放大 缩小 镜像 旋转 等(转)
惯例先看效果图 // 注意做类似这种模板功能时候 方位由后台数据提供,这里我们用假数据 4个点 或者xy 加区域来做示例 //一开始我们公司用的是透明盖住 操作图片 但发现 局限性较大.后来直接限定区 ...
- 用css3实现图片的放大缩小
记录一个公用的css实现图片的放大缩小 @keyframes scaleDraw { /*定义关键帧.scaleDrew是需要绑定到选择器的关键帧名称*/ 0%{ transform: scale(1 ...
随机推荐
- 基于docker安装pxc集群
基于docker安装pxc集群 一.PXC 集群的安装 PXC集群比较特殊,需要安装在 linux 或 Docker 之上.这里使用 Docker进行安装! Docker的镜像仓库中包含了 PXC数据 ...
- mysql数据库: 用户管理、pymysql使用、navicat插件使用
一.用户管理 二.pymysql增删改查 三.sql注入攻击 一.用户管理 数据安全非常重要 不可能随便分配root账户 应该按照不同开发岗位分配不同的账户和权限 mysql中 将于用户相关的数据放在 ...
- PAT Basic 1092 最好吃的月饼 (20 分)
月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出 ...
- zencart用sql将某个产品属性值设为只读和默认
zencart用sql将某个产品属性值设为只读和默认 UPDATE `products_attributes` SET `attributes_display_only` = '1', `attrib ...
- linux下进程间通信的机制
今天突然想起了nginx解决惊群的方法,就是在多个进程间利用锁来保证同一时刻只能有一个worker进程在自己的epoll中加入监听的句柄,那么进程间是怎么共享变量的呢,下面就介绍一下共享内存 共享内存 ...
- 最简单之安装hive
一,安装模式介绍 Hive官网上介绍了Hive的3种安装方式,分别对应不同的应用场景. a.内嵌模式(元数据保村在内嵌的derby种,允许一个会话链接,尝试多个会话链接时会报错) b.本地模式(本地安 ...
- Spring 事务相关
事务类型 数据库事务类型有本地事务和分布式事务: 本地事务:就是普通事务,能保证单台数据库上的操作的ACID,被限定在一台数据库上: 分布式事务:涉及两个或多个数据库源的事务,即跨越多台同类或异类数据 ...
- request_time和upstream_response_time详解
下图是request_time. 下图是upstream_response_time. 精准的描述就是:request_time是从接收到客户端的第一个字节开始,到把所有的响应数据都发送完为止.ups ...
- jQuery方法介绍
//jQuery与JavaScript在申明变量的区别: var $variable = jQuery对象 var variable = DOM对象 $variabl[0] //jQuery对象转换成 ...
- union共同体
定义: union 共用体名{ 成员列表}: 与结构体不同的是,共用体的所有成员占用同一段内存,修改一个成员会影响其余成员.但是结构体的各个成员会占不同的内存. 结构体占用的内存大于等于所有成员占用的 ...