1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Viewer extends JPanel {
private static final long serialVersionUID = 1L;
private Image image;
private int imageWidth, imageHeight, iw, ih, image_x, image_y;
private double factor = 1;
private Dimension area;
public static JScrollPane scrollpane;
public Viewer() {
scrollpane = new JScrollPane(this);
scrollpane.getViewport().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
}
});
scrollpane.getViewport().addMouseWheelListener(
new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
int rot = e.getWheelRotation();
if (rot < 0) {
zoomIn();
if (Jview.clientState == true) {
Jview.client.sendCommand(Jview.cmd.zoomIn);
}
} else {
zoomOut();
if (Jview.clientState == true) {
Jview.client.sendCommand(Jview.cmd.zoomOut);
}
}
}
});
area = new Dimension(0, 0);
}
public void setPix(String file) {
image = new ImageIcon(file).getImage();
imageWidth = image.getWidth(this);
imageHeight = image.getHeight(this);
area.width = imageWidth;
area.height = imageHeight;
setPreferredSize(area);
factor = 1;
revalidate();
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
iw = (int) (image.getWidth(this) * (factor));
ih = (int) (image.getHeight(this) * (factor));
image_x = (getWidth() - iw) / 2;
image_y = (getHeight() - ih) / 2;
g.drawImage(image, image_x, image_y, iw, ih, this);
g.dispose();
// g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
// RenderingHints.VALUE_INTERPOLATION_BILINEAR);
area.width = iw;
area.height = ih;
setPreferredSize(area);
setMinimumSize(area);
setMaximumSize(area);
}
}
public void zoomIn() {
factor += 0.05;
updateUI();
}
public void zoomOut() {
if (factor > 0.01) {
factor -= 0.05;
}
updateUI();
}
public void originalSize() {
factor = 1;
revalidate();
repaint();
}
} |
Partager