Mover imagem ao arrastar o mouse pressionado[RESOLVIDO]

1 resposta
Z

Boa noite, tenho um código que move a imagem, um mapa na verdade, mas cada vez que clico novamente o mapa volta para posição original...qria fazer parecido como no google maps, ir arrastando o mapa.

Se alguem tem alguma dica pra me ajudar agradeço

segue o código abaixo

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nao_usados;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.border.TitledBorder;

public class MoveImage extends JFrame {
	ShowCanvas canvas;

	public MoveImage() {
		super();
		Container container = getContentPane();
		canvas = new ShowCanvas();
		container.add(canvas);
		//setSize(300, 200);
                setExtendedState(MAXIMIZED_BOTH);
		setVisible(true);
	}

	public static void main(String arg[]) {
		new MoveImage();
	}
}

class ShowCanvas extends JPanel {
	int x, y;
	BufferedImage image;

	ShowCanvas() {
		setBackground(Color.white);
		//setSize(450, 400);
		addMouseMotionListener(new MouseMotionHandler());

		Image img = getToolkit().getImage("img\\mapa-geo.png");

		MediaTracker mt = new MediaTracker(this);
		mt.addImage(img, 1);
		try {
			mt.waitForAll();
		} catch (Exception e) {
			System.out.println("Image not found.");
		}
		image = new BufferedImage(img.getWidth(this), img.getHeight(this),
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = image.createGraphics();
		g2.drawImage(img, 0, 0, this);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2D = (Graphics2D) g;
		g2D.drawImage(image, x, y, this);
	}

	class MouseMotionHandler extends MouseMotionAdapter {
		public void mouseDragged(MouseEvent e) {
			x = e.getX();
                        System.out.println(x);
			y = e.getY();
                        System.out.println(y);
			repaint();
		}

		public void mouseMoved(MouseEvent e) {
		}
	}
}

1 Resposta

Z

RESOLVIDO, segue abaixo o cod

package grafica;

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.<em>;

import javax.swing.event.</em>;

public class Mapas extends JFrame {
public Mapas() {
    super("Mapa");
    ImageIcon ii = new ImageIcon("img\\mapa-geo.png");
    JScrollPane jsp = new JScrollPane(new GrabAndScrollLabel(ii));
    getContentPane().add(jsp);
//        setSize(300,250);

setExtendedState(MAXIMIZED_BOTH);

setVisible(true);
WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(l);
}
//    public static void main(String[] args) {

//        new Mapas();

//    }

}
class GrabAndScrollLabel extends JLabel{

public GrabAndScrollLabel(ImageIcon i){

super(i);

MouseInputAdapter mia = new MouseInputAdapter() {

int m_XDifference, m_YDifference;

Container c;

public void mouseDragged(MouseEvent e) {

c = GrabAndScrollLabel.this.getParent();

if (c instanceof JViewport) {

JViewport jv = (JViewport) c;

Point p = jv.getViewPosition();

int newX = p.x - (e.getX()-m_XDifference);

int newY = p.y - (e.getY()-m_YDifference);

int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();

int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();

if (newX < 0)

newX = 0;

if (newX > maxX)

newX = maxX;

if (newY < 0)

newY = 0;

if (newY > maxY)

newY = maxY;

jv.setViewPosition(new Point(newX, newY));

}

}
public void mousePressed(MouseEvent e) {
          setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
          m_XDifference = e.getX();
          m_YDifference = e.getY();
      }

      public void mouseReleased(MouseEvent e) {
          setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      }        
  };
  addMouseMotionListener(mia);
  addMouseListener(mia);
}

}

Criado 29 de abril de 2013
Ultima resposta 29 de abr. de 2013
Respostas 1
Participantes 1