import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Map extends JPanel implements MouseWheelListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private BufferedImage originalImage;
int x, y;
private BufferedImage resizedImage;
public Map(String source) {
super();
this.addMouseWheelListener(this);
this.originalImage = this.loadImage(source);
this.x = this.originalImage.getWidth();
this.y = this.originalImage.getHeight();
this.resizedImage = this.originalImage;
}
public BufferedImage resizeImage(int newW, int newH) {
BufferedImage dimg = new BufferedImage(newW, newH, originalImage.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(originalImage, 0, 0, newW, newH, 0, 0, x, y, null);
g.dispose();
return dimg;
}
public static BufferedImage loadImage(String ref) {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new File(ref));
} catch (Exception e) {
e.printStackTrace();
}
return bimg;
}
public BufferedImage getMap() {
return this.originalImage;
}
public void setMap(BufferedImage img) {
this.originalImage = img;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.resizedImage, 0, 0, null);
}
final static String newline = "\n";
public void mouseWheelMoved(MouseWheelEvent e) {
// TODO Auto-generated method stub
String message;
int notches = e.getWheelRotation();
if (notches < 0) {
resizedImage = resizeImage(new Double(resizedImage.getWidth() * 0.9).intValue(), new Double(resizedImage.getHeight() * 0.9)
.intValue());
message = "Mouse wheel moved UP " + -notches + " notch(es)" + newline;
} else {
resizedImage = resizeImage(new Double(resizedImage.getWidth() * 1.1).intValue(), new Double(resizedImage.getHeight() * 1.1)
.intValue());
message = "Mouse wheel moved DOWN " + notches + " notch(es)" + newline;
}
System.out.println(message);
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Map map = new Map("test");
frame.getContentPane().add(map);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}