Java 图片查看

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*支持拖拽放大,鼠标滚轮放大缩小
*/
public class ShowImageDialog extends JDialog {
	private BufferedImage image = null;
	JPanel panel = null;
	private int difference = 0;

	public ShowImageDialog(JFrame frame, BufferedImage image) {
		super(frame, true);
		setTitle("查看图片");
		this.image = image;
		setSize(800, 600);
		init();
		difference = getHeight() - panel.getHeight();
		this.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				formMouseClicked(evt);
			}

		});
		this.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
			public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
				formMouseWheelMoved(evt);
			}
		});
	}

	private void init() {

		getContentPane().setLayout(new java.awt.FlowLayout());
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		panel = new ImgPanel();
		if (image.getWidth() < getWidth() && image.getHeight() < getHeight()) {
			this.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
			panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
		} else {
			int height = getImageHeight(this.getWidth());
			this.setSize(new Dimension(800, height));
			panel.setPreferredSize(new Dimension(800, height));
		}
		this.add(panel);
		pack();
	}

	private void formMouseClicked(java.awt.event.MouseEvent evt) {
		if (evt.getClickCount() >= 2) {
			this.dispose();
		}
	}

	private void formMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
		int dir = evt.getWheelRotation();
		if (dir == 1) {
			int height = getImageHeight(getWidth() - 10);
			this.setSize(new Dimension(getWidth() - 10, height));
			this.paint(getGraphics());
		} else if (dir == -1) {
			int height = getImageHeight(getWidth() + 10);
			this.setSize(new Dimension(getWidth() + 10, height));
			this.paint(getGraphics());
		}
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		panel.setPreferredSize(new Dimension(getWidth(), getHeight() - difference));
	}

	public class ImgPanel extends JPanel {

		private boolean ampl = false;
		private int x;
		private int y;
		private int i_width;
		private int i_height;

		public ImgPanel() {
			i_width = image.getWidth();
			i_height = image.getHeight();
			this.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent evt) {
					panelMousePressed(evt);
				}

				public void mouseReleased(java.awt.event.MouseEvent evt) {
					panelMouseReleased(evt);
				}
			});

			addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
				public void mouseDragged(java.awt.event.MouseEvent evt) {
					panelMouseDragged(evt);
				}
			});
		}

		@Override
		public void paint(Graphics g) {
			if (i_width <= getWidth() && i_height <= getHeight()) {
				g.drawImage(image, 0, 0, this);
			} else {
				if (ampl) {
					g.drawImage(image.getSubimage(getImageX(getWidth(), x), getImageY(getHeight(), y), getWidth(), getHeight()), 0, 0, getWidth(), getHeight(), this);
				} else {
					int width = getWidth();
					int height = getImageHeight(panel.getWidth());
					g.drawImage(image, 0, 0, width, height, this);
				}
			}
		}

		private void panelMousePressed(java.awt.event.MouseEvent evt) {
			ampl = true;
			x = evt.getX();
			y = evt.getY();
			paint(this.getGraphics());

		}

		private void panelMouseReleased(java.awt.event.MouseEvent evt) {
			ampl = false;
			paint(this.getGraphics());
		}

		private void panelMouseDragged(java.awt.event.MouseEvent evt) {
			x = evt.getX();
			y = evt.getY();
			if (ampl) {
				paint(this.getGraphics());
			}
		}

		private int getImageX(int w_width, int m_x) {
			int i_x = 0;
			float f = i_width;
			if (m_x > w_width) {
				m_x = w_width;
			}
			i_x = (int) ((f / w_width) * m_x - m_x);
			if (i_x < 0) {
				i_x = 0;
			}
			return i_x;
		}

		private int getImageY(int w_height, int m_y) {
			int i_y = 0;
			float f = i_height;
			if (m_y > w_height) {
				m_y = w_height;
			}
			i_y = (int) ((f / w_height) * m_y - m_y);
			if (i_y < 0) {
				i_y = 0;
			}
			return i_y;
		}
	}

	private int getImageHeight(int width) {
		int imageHeight = 0;
		double f = image.getWidth();
		double x = f / width;
		imageHeight = (int) (image.getHeight() / x);
		return imageHeight;
	}

}

 

 

 

你可能感兴趣的:(Java 图片查看)