Sorry about that. I didn't mean to. I have solved the zoom problem. I am still working on the drag issue. Please see the updated code below:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package indiamapo;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.event.MouseInputListener;
/**
*
* @author dingding
*/
public class IndiaMapo extends JFrame {
DisplayCanvas canvas;
public IndiaMapo() {
super();
Container container = getContentPane();
canvas = new DisplayCanvas();
container.add(canvas);
JPanel panel = new JPanel();
panel.setBounds(0,0,100,900);
getContentPane().add(panel, BorderLayout.WEST);
JButton ZoominButton = new JButton("+");
ZoominButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.increment();
}
});
ZoominButton.setHorizontalAlignment(SwingConstants.LEADING);
ZoominButton.setVerticalAlignment(SwingConstants.TOP);
panel.add(ZoominButton);
JButton ZoomoutButton = new JButton("-");
ZoomoutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.decrement();
}
});
panel.add(ZoomoutButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(900, 800);
setVisible(true);
}
public static void main(String arg[]) {
new IndiaMapo();
}
}
class DisplayCanvas extends JPanel implements MouseListener {
int x, y;
int ctr = 0 ;
int dx=0, dy=0;
int height, width;
int scale = 1;
IndiaMapo im;
BufferedImage bi;
int lastx = 0;
int lasty = 0;
int currentoriginx = 0, currentoriginy = 0;
int currentreferencex = 0, currentreferencey = 0;
public Image image;
//public int drawFromx;
//public int drawFromy;
DisplayCanvas() {
//setBackground(Color.white);
setSize(900, 800);
addMouseListener(this);
addMouseMotionListener(new MouseInputListener() {
@Override
public void mouseClicked(MouseEvent mea) {
}
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseDragged(MouseEvent me) {
dx = me.getX() - currentreferencex;
dy = me.getY() - currentreferencey;
repaint();
System.out.println("Draged");
}
@Override
public void mouseMoved(MouseEvent me) {
}
});
image = getToolkit().getImage("1.gif");
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Exception while loading image.");
}
if (image.getWidth(this) == -1) {
System.out.println("no gif file");
System.exit(0);
}
bi = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
big.drawImage(image, 0,0 , image.getWidth(this), image.getHeight(this), null);
//read original image
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(bi, , , this);
// System.out.println("Current Origin ="+currentoriginx +" "+currentoriginy);
System.out.println((currentoriginx - dx) +" "+ (currentoriginy - dy));
//check jump here
// currentoriginx = currentoriginx - dx;
//currentoriginy = currentoriginy - dy;
}
public void increment()
{
scale = scale + 1;
if (scale > 25)
{
scale = 25;
}
drawandScale();
}
public void decrement()
{
scale = scale - 1;
if (scale < 2)
{
scale = 1;
}
drawandScale();
}
public void drawandScale()
{
Graphics2D big = bi.createGraphics();
big.drawImage(image, -((scale-1)*image.getWidth(this)-0*scale)/2, -((scale-1)*image.getHeight(this)/2), scale*image.getWidth(this), scale*image.getHeight(this), null);
repaint();
//check scale parameter
}
@Override
public void mousePressed(MouseEvent me) {
//x = me.getX();
//y = me.getY();
currentreferencex = me.getX();
currentreferencey = me.getY();
System.out.println("Press");
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
lastx=currentreferencex-currentoriginx;
lasty=currentreferencey-currentoriginy;
lastx = me.getX();
lasty = me.getY();
repaint();
System.out.println("Releaess");
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
}
I realize I am still using dx and dy in my drawImage function but I am not sure how to update it with the latest position of the image. I have tried dx - lastx and dy - lasty in the drawImage function but that doesn't work. Would I still use dx and dy in the drawImage function in addition to adding or subtracting something from them?
Thank you!