Following mouse motion?

ok what i trying to do is that i wan a image to move according to the mouse direction..

just like in rpg game..when let said you wan to move your image to west..

then you move your mouse west and click it..then image will follow..

which mouselistener will go the job and is there any tutorial for it?

[325 byte] By [workerhqa] at [2007-10-1 13:43:06]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JFrame;

import javax.swing.border.*;

public class TestPlates {

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

PlateCanvas mainPanel = new PlateCanvas();

Plate p1 = new Plate("Plate 1");

Plate p2 = new Plate("Plate 2");

Plate p3 = new Plate("Plate 3");

mainPanel.add(p1);

mainPanel.add(p2);

mainPanel.add(p3);

f.getContentPane().add(mainPanel);

f.setSize(800, 600);

f.setVisible(true);

}

}

class PlateCanvas extends JPanel implements MouseListener, MouseMotionListener {

private Plate hitPlate;

private double deltaX;

private double deltaY;

Point previousLocation;

boolean first = true;

public PlateCanvas() {

init();

}

private void init() {

addMouseListener(this);

addMouseMotionListener(this);

}

public void mouseClicked(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

Component c = getComponentAt(e.getPoint());

if (c instanceof Plate) {

hitPlate = (Plate) c;

deltaX = e.getX() - hitPlate.getX();

deltaY = e.getY() - hitPlate.getY();

}

}

public void mouseReleased(MouseEvent e) {

hitPlate = null;

}

int max,newX,newY;

public void mouseDragged(MouseEvent e) {

if (hitPlate != null) {

int x = (int) (e.getX() - deltaX);

int y =(int) (e.getY() - deltaY);

hitPlate.setLocation(x, y);

hitPlate.stayInContainer();

repaint();

}

}

public void mouseMoved(MouseEvent e) {

}

}

class Plate extends JPanel {

private String name;

private Container c;

private Point top_leftPoint, top_rightPoint, bottom_leftPoint, bottom_rightPoint ;

private Point top_leftPointC, top_rightPointC, bottom_leftPointC, bottom_rightPointC ;

private int w,h,wC,hC;

public Plate(String name) {

this.name = name;

init();

}

private void init() {

setPreferredSize(new Dimension(100, 100));

setBorder(new TitledBorder(new LineBorder(Color.BLACK), name));

}

private boolean isInContainer(){

c = getParent();

Rectangle r = new Rectangle( c.getWidth(), c.getHeight() );

wC = (int)r.getWidth();

hC = (int)r.getHeight();

top_leftPointC = r.getLocation();

top_rightPointC = new Point( (int)top_leftPointC.getX()+wC, (int)top_leftPointC.getY() );

bottom_leftPointC = new Point( (int)top_leftPointC.getX(), (int)top_leftPointC.getY()+hC );

bottom_rightPointC = new Point( (int)top_leftPointC.getX()+wC, (int)top_leftPointC.getY()+hC );

w = getWidth();

h = getHeight();

top_leftPoint = getLocation();

top_rightPoint = new Point( (int)top_leftPoint.getX()+w, (int)top_leftPoint.getY() );

bottom_leftPoint = new Point( (int)top_leftPoint.getX(), (int)top_leftPoint.getY()+h );

bottom_rightPoint = new Point( (int)top_leftPoint.getX()+w, (int)top_leftPoint.getY()+h );

if( !r.contains(top_leftPoint) ) return false;

if( !r.contains(top_rightPoint) ) return false;

if( !r.contains(bottom_leftPoint) ) return false;

if( !r.contains(bottom_rightPoint) ) return false;

return true;

}

public void stayInContainer(){

/* if the plate is too far in right-direction, move it back */

if( !isInContainer() ){

double x = top_rightPoint.getX();

double xC = top_rightPointC.getX();

if( x > xC ){

setLocation( (int)(top_leftPoint.getX()+xC-x), (int)top_leftPoint.getY());

}

}

/* if the plate is too far in left-direction, move it back */

if( !isInContainer() ){

double x = top_leftPoint.getX();

double xC = top_leftPointC.getX();

if( x < xC ){

setLocation( (int)(top_leftPoint.getX()+xC-x), (int)top_leftPoint.getY());

}

}

/* if the plate is too far in top-direction, move it back */

if( !isInContainer() ){

double y = top_leftPoint.getY();

double yC = top_leftPointC.getY();

if( y < yC ){

setLocation( (int)top_leftPoint.getX(), (int)(top_leftPoint.getY()+yC-y));

}

}

/* if the plate is is too far in bottom-direction, move it back */

if( !isInContainer() ){

double y = bottom_leftPoint.getY();

double yC = bottom_leftPointC.getY();

if( y > yC ){

setLocation( (int)top_leftPoint.getX(), (int)( top_leftPoint.getY()+yC-y) );

}

}

}

}

uhranda at 2007-7-10 16:48:14 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...