java实现图片查看器

我的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 block
					e1.printStackTrace();
				}
			}
		});
		close.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.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 block
					e1.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 stub
		new 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)