我的java代码特别不面向对象。

实现的功能是查看,左右切换,放大缩小。


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.MouseInputAdapter;public class pictureview extends JFrame {private File file;private int lar, nar, tim;private boolean flag;private JLabel lab1;private JMenuBar bar;private jmenu fileMenu;private String path = "";private ImageIcon bufImage;private JPanel jpanel, ipanel;private FileDialog openDia, saveDia;private jmenuItem next, open, save, close, large, narrow, prev, wr;pictureview() {super("图片");init();}public void init() {this.setBounds(200, 100, 650, 600);this.setLayout(new BorderLayout());bar = new JMenuBar();bar.setPreferredSize(new Dimension(-1, 60));bar.setBackground(new Color(70,70,70));bar.setBorderPainted(true);bar.setMargin(new Insets(5, 0, 0, 0));fileMenu = new jmenu("查看图片");ImageIcon image = new ImageIcon("C:\\Users\\LENOVO\\Pictures\\love.png");wr = new jmenuItem(" 旋转");wr.setIcon(image);large = new jmenuItem(" 放大");large.setIcon(image);narrow = new jmenuItem(" 缩小");narrow.setIcon(image);prev = new jmenuItem("上一张");prev.setIcon(image);next = new jmenuItem("下一张");next.setIcon(image);open = new jmenuItem("  打开 ");save = new jmenuItem("  保存 ");close = new jmenuItem("  关闭 ");fileMenu.add(open);fileMenu.add(save);fileMenu.add(close);bar.add(fileMenu);bar.add(large);bar.add(narrow);bar.add(wr);bar.add(prev);bar.add(next);this.setJMenuBar(bar);lab1 = new JLabel();ipanel = new JPanel();ipanel.setBackground(Color.gray);ipanel.add(lab1);this.add(ipanel, BorderLayout.CENTER);mymouse listener=new mymouse();lab1.addMouseListener(listener);lab1.addMouseMotionListener(listener);openDia = new FileDialog(this, "打开", FileDialog.LOAD);saveDia = new FileDialog(this, "保存", FileDialog.SAVE);myEvent();this.setVisible(true);}public class mymouse extends MouseInputAdapter{Point point=new Point(0,0);public void mousePressed(MouseEvent e) {point = SwingUtilities.convertPoint(lab1, e.getPoint(),lab1.getParent() );}public void mouseDragged(MouseEvent e) {Point newPoint = SwingUtilities.convertPoint(lab1, e.getPoint(), lab1.getParent());lab1.setLocation(lab1.getX() + (newPoint.x - point.x), lab1.getY() + (newPoint.y - point.y));point = newPoint;}}public void clear() {lar = 1;nar = 1;tim = 0;flag = true;}public void judge() {if (lar >= nar)addimage(path, true, lar - nar + 1);else if (nar > lar)addimage(path, false, -lar + nar + 1);}public void myEvent() {lab1.addMouseListener(new MouseAdapter() {public void mouseMoved(MouseEvent e) {}});open.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {openDia.setVisible(true);String dir = openDia.getDirectory();String name = openDia.getFile();if (dir == null || name == null)return;else {file = new File(dir);path = dir + name;clear();addimage(path, flag, lar);}}});save.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {saveDia.setVisible(true);String dir = saveDia.getDirectory();String name = saveDia.getFile();if (dir == null || name == null)return;file = new File(dir, name);try {BufferedImage ima = ImageIO.read(new File(path));String fileName = file.getName();String fileTyle = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());ImageIO.write(ima, fileTyle, file);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});close.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});large.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {lar++;if (path.equals(new String("")))return;if ((lar - nar) >= 4 || (nar - lar) >= 4) {lar--;return;}judge();}});narrow.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {nar++;if (path.equals(new String("")))return;if ((lar - nar) >= 4 || (nar - lar) >= 4) {nar--;return;}judge();}});wr.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (path.equals(new String("")))return;tim++;BufferedImage ima;try {ima = ImageIO.read(new File(path));ima = rotateImage(ima, (tim * 90) % 360);ImageIcon im = new ImageIcon();im.setImage((Image) ima);lab1.setIcon(im);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}});next.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (path.equals(new String("")))return;String path1 = readFile1(file);clear();addimage(path1, flag, lar);path = path1;}});prev.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (path.equals(new String("")))return;String path1 = readFile2(file);clear();addimage(path1, flag, lar);path = path1;}});this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});}public void addimage(String iname, boolean flag, int times) {bufImage = new ImageIcon(iname);int cx = bufImage.getIconWidth();int cy = bufImage.getIconHeight();if (cx > this.getWidth() - 100 || cy > this.getHeight() - 130) {cx = this.getWidth() - 100;cy = this.getHeight() - 130;}if (flag == true) {cx = cx * times;cy = cy * times;} else {cx = cx / times;cy = cy / times;}bufImage.setImage(bufImage.getImage().getScaledInstance(cx, cy, Image.SCALE_DEFAULT));/** if(bufImage.getIconWidth() > this.getWidth() - 100 ||* bufImage.getIconHeight() > this.getHeight() - 130)* bufImage.setImage(bufImage.getImage().getScaledInstance(this.getWidth() -* 100, this.getHeight() - 130, Image.SCALE_DEFAULT));*/lab1.setIcon(bufImage);}public static BufferedImage rotateImage(final BufferedImage bufferedimage, final int degree) {int w = bufferedimage.getWidth();int h = bufferedimage.getHeight();int type = bufferedimage.getColorModel().getTransparency();BufferedImage img;Graphics2D graphics2d;(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);graphics2d.drawImage(bufferedimage, 0, 0, null);graphics2d.dispose();return img;}public String readFile1(File mfile) {File[] file = mfile.listFiles();int flag = 0;for (int i = 0; i < file.length; i++) {if ((!(file[i].getAbsolutePath().toString().equals(path))) && (flag == 0))continue;flag++;String filename = file[i].getName();if (flag > 1 && (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".gif")|| filename.endsWith(".jpeg"))) {return file[i].getAbsolutePath().toString();}}for (int i = 0; i < file.length; i++) {String filename = file[i].getName();if (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".gif")) {return file[i].getAbsolutePath().toString();}}return path;}public String readFile2(File mfile) {File[] file = mfile.listFiles();int flag = 0;for (int i = file.length - 1; i >= 0; i--) {if ((!(file[i].getAbsolutePath().toString().equals(path))) && (flag == 0))continue;flag++;String filename = file[i].getName();if (flag > 1 && (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".gif")|| filename.endsWith(".jpeg"))) {return file[i].getAbsolutePath().toString();}}for (int i = file.length - 1; i >= 0; i--) {String filename = file[i].getName();if (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".gif")) {return file[i].getAbsolutePath().toString();}}return path;}public static void main(String[] args) {// TODO Auto-generated method stubnew pictureview();}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;import javax.swing.JMenu;
import javax.swing.border.LineBorder;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.event.MouseInputAdapter;
public class jmenu extends JMenu{jmenu(String s){super(s);this.setFont(new Font("黑体",Font.PLAIN,20));this.setForeground(Color.BLACK);this.setOpaque(true);//this.setBackground(new Color(138,43,226));this.setBackground(new Color(138,43,226));this.setFocusPainted(false);//this.setBorder(new LineBorder(Color.BLACK));this.setBorderPainted(false);this.setMargin(new Insets(0,0,0,0));my_Event();}public void init() {this.setFont(new Font("黑体",Font.PLAIN,20));this.setForeground(Color.BLACK);this.setOpaque(true);//this.setBackground(new Color(138,43,226));setback();this.setFocusPainted(false);this.setBorderPainted(false);this.setMargin(new Insets(0,0,0,0));}public void set() {this.setBackground(new Color(138,43,226));}public void setback() {this.setBackground(Color.white);}public void my_Event() {this.addMouseListener(new MouseInputAdapter() {public void mouseExited(MouseEvent e) {setback();}public void mouseClicked(MouseEvent e){set();} public void mouseReleased(MouseEvent e){set();} public void mousePressed(MouseEvent e) {set();}public void mouseDragged(MouseEvent e) {setback();}public void mouseMoved(MouseEvent e) {setback();}});this.addMouseMotionListener(new MouseInputAdapter() {public void mouseExited(MouseEvent e) {setback();}public void mouseClicked(MouseEvent e){set();} public void mouseReleased(MouseEvent e){set();} public void mousePressed(MouseEvent e) {set();}public void mouseDragged(MouseEvent e) {setback();}public void mouseMoved(MouseEvent e) {setback();}});}
}

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.*;
import java.io.File;import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.MenuDragMouseListener;
import javax.swing.event.MouseInputAdapter;
public class jmenuItem extends JMenuItem{jmenuItem(String s){super(s);init();my_Event();}jmenuItem(ImageIcon image){super(image);init();my_Event();}public void init() {this.setFont(new Font("黑体",Font.PLAIN,18));this.setForeground(Color.BLACK);this.setPreferredSize(new Dimension(100,50));this.setBackground(Color.WHITE);this.setOpaque(true);this.setFocusPainted(false);this.setBorderPainted(false);this.setMargin(new Insets(0,0,0,0));}public void setback() {this.setOpaque(true);this.setBackground(new Color(138,43,226));}public void my_Event() {this.addMouseListener(new MouseInputAdapter() {public void mouseExited(MouseEvent e) {init();}public void mouseClicked(MouseEvent e){setback();} public void mouseReleased(MouseEvent e){setback();} public void mousePressed(MouseEvent e) {setback();}public void mouseDragged(MouseEvent e) {init();}public void mouseMoved(MouseEvent e) {init();}});this.addMouseMotionListener(new MouseInputAdapter() {public void mouseExited(MouseEvent e) {init();}public void mouseClicked(MouseEvent e){setback();} public void mouseReleased(MouseEvent e){setback();} public void mousePressed(MouseEvent e) {setback();}public void mouseDragged(MouseEvent e) {init();}public void mouseMoved(MouseEvent e) {init();}});}
}

java实现图片查看器相关推荐

  1. JavaSwing图片绘制,实现简单的图片查看器

    刚学到JavaSwing图片绘制,于是自己做了个简易的图片查看器小程序,在这里分享给大家,请多多指教. 话不多说先上部分图: 绘制图片需要自定义一个控件,我们这里写一个继承自JPanel的类,重写pa ...

  2. java小程序查看器_JAVA的一个查看图片的小程序

    只是一个查看本地图片展示的小Demo.发微博字数超长,好像只能发文章. 直接上代码,本代码已经在JDK1.8上测试完毕. import java.io.File; import java.beans. ...

  3. java swing awt绘制一个图片查看器 图片显示 图片控件

    感谢 java图片查看器 的代码 java似乎没有一个名字叫图片控件的 控件,使用swing 的Label显示图片 他的代码如下: package swing.draw; import java.aw ...

  4. 【Swing】图片查看器

    实现一个图片查看器: -加载文件夹里的图片显示 -显示缩略图 -点击缩略图时显示大图 -当前图片高亮显示 package my;import java.awt.Container; import ja ...

  5. 图片查看器:Android支持图片查看、缩放、滑动的PhotoView

    上效果: Android 图片查看器Demo效果 导入PhotoView,导入Glide. maven { url "https://jitpack.io" }implementa ...

  6. 图片查看器 PhotoView

    一.使图片能够全部显示在自定义 View 中 1.自定义 View 采用自定义View继承AppCompatImageView,重写构造方法: public class PhotoView exten ...

  7. 【React组件】写一个模仿蓝湖的图片查看器

    前言 最近公司让写一个可以自由拖拽放大的图片查看器,我寻思这还不简单,一顿操作猛如虎,俩小时后: 事实证明,一旦涉及到 DOM 的变换操作,如果很多细节考虑不全,抓过来就写,那基本就凉了.于是我仔细分 ...

  8. 图片html代码查看器,360度全景商品图片查看器

    360 Degrees Product Viewer360度全景商品图图片查看器是个非常简单的轻松展示图片全景插件. HTML Handle CSS .cd-product-viewer-wrappe ...

  9. 强大的jQuery图片查看器插件Viewer.js

    简介 Viewer.js 是一款强大的图片查看器 Viewer.js 有以下特点: 支持移动设备触摸事件 支持响应式 支持放大/缩小 支持旋转(类似微博的图片旋转) 支持水平/垂直翻转 支持图片移动 ...

  10. Win7图片查看器打印不了图片怎么办

    当我们想浏览电脑中的图片文件时,可以选择系统自带的图片查看器或者第三方看图工具打开,但是有些win7用户发现自己想通过windows图片查看器打印图片却没有反应,Win7图片查看器打印不了图片怎么办? ...

最新文章

  1. 一个java的DES加解密类转换成C#
  2. MDT跨网段UEFI部署系统
  3. 征途LINUX服务端脚本,bat脚本实例征途夜行
  4. [NOI 2017]整数
  5. 多个goruntine 性能变慢_提高 JavaScript 性能的 12 个技巧
  6. 浅谈大前端的代表技术及其影响,值得我们思考
  7. 两个分数化简比怎么化_怎么化行最简形矩阵?
  8. hibernate级联删除问题
  9. LeetCode 链表相关题目总结
  10. chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式
  11. 写一简单kernel心得
  12. 浙江大学黄杨思博计算机学院,黄杨-江南大学 理学院
  13. 程序员朋友们,请答应我?别再去东南亚写代码了好么?
  14. python圆形_利用 Python 实现裁剪圆形头像
  15. 7-37 模拟EXCEL排序 (25 分)
  16. 使用HSqlDB的SQL/JRT功能
  17. 微信公众号编辑器的附件功能(如Word、Excel、Pdf等)
  18. 没有你看不懂的Kmeans聚类算法
  19. 【Joshua B. Tenenbaum】非线性降维的全局几何框架
  20. java从入门到精通二十四(三层架构完成增删改查)

热门文章

  1. 升级Spring Boot 2.x后RelaxedPropertyResolver不可用的解决方案
  2. 8.15游戏背包设计
  3. 2021年广东省安全员A证第三批(主要负责人)及广东省安全员A证第三批(主要负责人)证考试
  4. 数据库系统-实体-联系模型
  5. 使用python绘制一个太阳花代码_如何绘制多样化的太阳花?
  6. 伽罗华域, 二维码和CRC
  7. php多域名跳转,旱的旱死,涝的涝死,中超联赛的怪事
  8. 小白白红队初成长(7)win权限提升
  9. pyecharts绘制K线
  10. 开发过程中沟通的重要性