0

Okay so i am trying to code out a canvas that has two complex objects(I have MainCanvas , Human, Dog) and I want to make a piece of the complex object move when i drag it, but i dont know if i need to put in another moving method in the complex object? Heres my code;

{import objectdraw.*;

import java.awt.*;
/**
 * Write a description of class Human here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Human

    // instance variables - replace the example below with your own
    private FilledRect leg1,leg2,body,foot1,foot2;
    private FilledOval head, eye1,eye2,frisbee;
    private Line arm1,arm2;
    private Text manCall;
    private double x,y;
    private boolean frisbeeDrag = false;
    private RandomIntGenerator randCol;
    private Color customCol;
    private RandomIntGenerator rig1;
    private int n;
    //private boolean frisbeeDrag = false;

    /**
     * Constructor for objects of class Human
     */
    public Human(Location pointIn, DrawingCanvas canvas){

        x = pointIn.getX();
        y = pointIn.getY();
        body = new FilledRect(x,y,30,70,canvas);
        body.setColor(Color.red);
        head = new FilledOval(x,y-30,30,30,canvas);
        head.setColor(Color.gray);
        eye1 = new FilledOval(x+7,y-20,6,6,canvas);
        eye1.setColor(Color.blue);
        eye2 = new FilledOval(x+17,y-20,6,6,canvas);
        eye2.setColor(Color.blue);
        leg1 = new FilledRect(x+2,y+70,5,50,canvas);
        leg1.setColor(Color.blue);
        leg2 = new FilledRect(x+20,y+70,5,50,canvas);
        leg2.setColor(Color.blue);
        foot1 = new FilledRect(x+1,y+120,13,5,canvas);
        foot1.setColor(Color.orange);
        foot2 = new FilledRect(x+19,y+120,13,5,canvas);
        foot2.setColor(Color.orange);
        arm1 = new Line (x,y+ 10,x-35,y+35,canvas);
        arm1 = new Line (x+30,y+10,x+65,y+35,canvas);
        frisbee = new FilledOval(x+65,y+30,25,10,canvas);
        frisbee.setColor(Color.black);
        manCall = new Text("Fetch Boy!", x-20,y-50,canvas);
        manCall.setColor(Color.green);
        manCall.hide();

    }

    public boolean myHumanClick(Location pointIn){
        if(body.contains(pointIn) || (head.contains(pointIn)) || (leg1.contains(pointIn) || leg2.contains(pointIn)||
        foot1.contains(pointIn)|| foot2.contains(pointIn) || (frisbee.contains(pointIn)))){
            return true;

        } 
        else{
            return false;
        }

    }

    public void randomChange(){

        rig1 = new RandomIntGenerator(1,5);

        n = rig1.nextValue();
        if(n == 1){
            randCol = new RandomIntGenerator(0,255);
            int green = randCol.nextValue();
            int red = randCol.nextValue();
            int blue = randCol.nextValue();
            customCol = new Color(red,green,blue);
            body.setColor(customCol);
            foot1.setColor(customCol);
            foot2.setColor(customCol);
            frisbee.setColor(customCol);
            manCall.hide();

        }else {
            manCall.show();

        }
    }



    public boolean frisbeeClick(Location pointIn){
        if (frisbee.contains(pointIn)){
            return true;
        }else{
            return false;

        }
    }

Thats my Human and here is my main canvas :

//ALWAYS IMPORT THESE TWO LIBRARIES

import objectdraw.*;

import java.awt.*;

/**
 * ObjectDraw Class Template
 *
 * @author Sharon Small
 * @version 1/24/2013
 */

//ALL objectdraw CLASSES MUST EXTEND WindowController

public class MainCanvas extends WindowController

{

    // instance variables - replace the example below with your own
    private Human myHuman;
    private Dog myDog;
    private FilledRect leftPost,rightPost,topPost,swingSeat1,swingSeat2;
    private Line swing1L,swing1R, swing2L,swing2R;
    private FilledRect grass,sky,nightSky;
    private FilledOval sun,moon;
    private int clickCounter = 1;
    private Text nightText,dayText;
    private boolean ifDragging = false;

    //CODE THAT WILL BE EXECUTE ONCE WHEN THE PROGRAM FIRST RUNS
    public void begin(){
        sky = new FilledRect(0,0,1000,250,canvas);
        sky.setColor(Color.cyan);
        nightSky = new FilledRect(0,0,1000,250,canvas);
        sky.setColor(Color.blue);
        nightSky.hide();
        topPost = new FilledRect(250,100,250,15,canvas);
        topPost.setColor(new Color(156,102,31));
        leftPost = new FilledRect(250,100,10,150,canvas);
        leftPost.setColor(new Color(156,102,31));
        rightPost = new FilledRect(500,100,10,150,canvas);
        rightPost.setColor(new Color(156,102,31));
        swingSeat1 = new FilledRect(300,200,50,10,canvas);
        swingSeat1.setColor(new Color(156,102,31));
        swingSeat2 = new FilledRect(400,200,50,10,canvas);
        swingSeat2.setColor(new Color(156,102,31));
        swing1L = new Line(310,100,310,200,canvas);
        swing1R = new Line(340,100,340,200,canvas);
        swing2L = new Line(410,100,410,200,canvas);
        swing2R = new Line(440,100,440,200,canvas);
        grass = new FilledRect(0,250,1000,200,canvas);
        grass.setColor(Color.green);

        sun = new FilledOval(0,0,60,60,canvas);
        sun.setColor(Color.yellow);
        moon = new FilledOval(0,0,60,60,canvas);
        moon.setColor(Color.white);
        moon.hide();

        nightText = new Text ("Time To Go Home!", 100,50,canvas);
        nightText.setColor(Color.white);
        nightText.hide();
        dayText = new Text ("It's A  Beautiful Day!", 100,50,canvas);
        dayText.setColor(Color.red);

    }
    //CODE TO BE EXECUTED WHEN THE USER PRESSES THE MOUSE
    //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE LOCATION WHEN IT WAS PRESSED
    public void onMousePress(Location point)
    {

        if( myHuman !=null && myHuman.myHumanClick(point)){
            myHuman.randomChange();
        }
        if(myHuman.frisbeeClick(point)){
            ifDragging=true;
        }
        //CODE TO BE EXECUTED WHEN THE USER RELEASES THE MOUSE
        //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE LOCATION WHEN IT WAS RELEASES
    }

    public void onMouseRelease(Location point)
    { 
        ifDragging = false;

    }
    //CODE TO BE EXECUTED WHEN THE USER CLICKS THE MOUSE
    //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE LOCATION WHEN IT WAS CLICKED
    public void onMouseClick(Location point)
    { if(clickCounter == 1){
            myHuman = new Human(point,canvas);
            clickCounter++;
        }else if (clickCounter == 2){
            myDog = new Dog(point,canvas);
            clickCounter++;
        }
        if (myDog != null && myDog.myDogClick(point)){
            myDog.randomChange();

        }else{

        }
    }
    //CODE TO BE EXECUTED WHEN THE MOUSE ENTERS THE CANVAS
    //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE LOCATION WHEN IT ENTERED THE CANVAS
    public void onMouseEnter(Location point)
    {
        dayText.show();
        nightText.hide();
        nightSky.hide();
        moon.hide();

    }
    //CODE TO BE EXECUTED WHEN THE MOUSE EXITS THE CANVAS
    //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE LOCATION WHEN IT EXITED THE CANVAS
    public void onMouseExit(Location point)
    {
        dayText.hide();
        nightText.show();
        nightSky.show();
        moon.show();

    }
    //CODE TO BE EXECUTED WHEN THE USER MOVES THE MOUSE
    //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE AS IT IS MOVING
    public void onMouseMove(Location point)
    {
        // put your code here

    }

    //CODE TO BE EXECUTED WHEN THE USER DRAGS THE MOUSE
    //THE point VARIABLE CONTAINS THE X,Y COORDINATES OF THE MOUSE AS IT IS DRAGGING
    public void onMouseDrag(Location point)
    {
        if ((myHuman != null) && (ifDragging == true)){
            myHuman.moveTo(point);
        }

    }
}

any help would be appreciated

2
Contributors
1
Reply
23
Views
4 Years
Discussion Span
Last Post by M4trixSh4d0w
0

EDIT: I don't think you would need any other code than that, I missed your dragging methods

You would probably want to use this method (below) and add in some kind of setter for the Human or Dog class (whichever you want to have move) and set an x and y location of where the object should be moved to. like below.

boolean clicked = false;
Point point1; //You can use Location also
public void onMouseClick(Location point) {
    this.point = point;
    clicked = true;
}
public void onMouseMove(Location point)
    {
        if(clicked) {
            this.point = point;
            human.setLocation(point.x,point.y);
       }

    }
    //add an on mouse released method and set click to false

Edited by M4trixSh4d0w

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.