views:

14

answers:

0

Basically they want me to make a character consisting of various shapes using the Java Wheels librarry that move as one when dragged across the screen. The catch is, 1 part must be able to move independently, yet still follow the cartoon character when he is moved. In my case, i made a snowman. In this test below, i get the entire snowman to move as one. However i cant figure out at all how to get the carrot to move independently. Below is the code for my snowmanBody class, where snowmanHead,snowmanMiddle, and snowmanBottom are SnowmanParts, which is just a subclass of Ellipse. Below is the code for SnowmanBody and Carrot. Any help would be much appreciated.

import java.awt.Point; import java.awt.event.MouseEvent; import wheels.users.Image;

public class SnowmanBody implements Draggable {
    private int _x,_y; //for our setLocation method
    public int carrotDeltaX,carrotDeltaY; //these keep track of the carrot's movement since it will be able to move independently.
    private Point _lastPoint,_lastCarrotPoint; //keep track of the previous mouse click
    private SnowmanPart snowmanHead,snowmanMiddle,snowmanBottom; //the Snowman's body.
    private Image _carrot;
    public SnowmanBody() {
        _x=5;
        _y=0;
        _lastPoint = new Point();
        snowmanBottom= new SnowmanPart(this);
        snowmanMiddle= new SnowmanPart(this);
        snowmanHead= new SnowmanPart(this);
        _carrot = new Carrot("carrot.png");

        snowmanHead.setSize(100,100);
        snowmanMiddle.setSize(150,150);
        snowmanBottom.setSize(200,200);

        this.setLocation(0, 0);
    }
    @Override
    public void mousePressed(MouseEvent e) {
        _lastPoint= e.getPoint();   
        _lastCarrotPoint= _carrot.getLocation();

    }
    @Override
    public void mouseDragged(MouseEvent e) {

        Point currentPoint = e.getPoint();

        int diffX = currentPoint.x-_lastPoint.x;
        int diffY = currentPoint.y-_lastPoint.y;
        this.setLocation(_x+diffX,_y+diffY);
        _lastPoint = currentPoint;
    }
    private void setLocation(int x, int y) {

        _x=x;       
        _y=y;

        snowmanBottom.setLocation(x, y+280);
        snowmanMiddle.setLocation(x+25, y+200);
        snowmanHead.setLocation(x+55, y+130);
            _carrot.setLocation(x+100,y+175); //I have this code here but unfortunately the carrot will always snap back into place
        System.out.println(_x);

    }




}

The Carrot class only has the following:

import java.awt.event.MouseEvent;
import wheels.users.Image;


public class Carrot extends Image implements Draggable {
    public Carrot(String file) {
        super(file);
    }

    public void mousePressed(MouseEvent e) {
    }
    public void mouseDragged(MouseEvent e) {
        this.setLocation(e.getX(), e.getY());
    }


}