Question

So, my assignment was to create a class with multiple animals that each moved in a certain way as practice for working with multiple objects. One that has stumped me to no end, is the "Turtle" class. The Turtle is supposed to move in a clockwise motion in a 5x5 square, first going south, then west, then north then east. It goes in one direction for 5 "steps" and then switches. so it would go south for 5 steps and then go west for 5 steps, etc. My problem right now is that it only goes in a diagonal line going northeast whenever I try to run the code.Anyway, here's the code I needed to work with:

This is the main method:

import java.awt.Color;  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;


public class AnimalSimulator {


public static void main(String[] args) {

    DrawingPanel forest = new DrawingPanel(720, 640);
    Graphics2D pen = forest.getGraphics();
    Animal [] animals = new Animal[15];

    for (int i = 0; i < animals.length;i++){
        //int selection = (int) (Math.random()*6+1);
        int randX = (int) (Math.random()*720+1);
        int randY = (int) (Math.random()*640+1);



        Turtle t = new Turtle("Reptile", "Tortoise", new Point(randX, randY), Color.CYAN);
        animals [i] = t;

    }
    for (int time = 1; time <= 100; time++){
        for (int i = 0; i < animals.length; i++){
            //System.out.println(animals[i]);
            //Graphics.drawString(animals[i].toString(), animals[i].getLocation().x, animals[i].getLocation().y);
            pen.setColor(animals[i].getColor());
            pen.fillRect(animals[i].getLocation().x, animals[i].getLocation().y, 10,10);            
            animals[i].move();
            for (int a = 0; a< animals.length; a++){
                if(a!= i){
                    //if (animals[i].equals(animals[a])){
                        //animals[a] = null;
                    }
                }
            }
            forest.sleep(50);
        }

    }






}

this is the turtle method:

import java.awt.Color; 
import java.awt.Point;


public class Turtle extends Animal {

public Turtle(String type, String name, Point location, Color color) {
    super(type, name, location, color);
    // TODO Auto-generated constructor stub
}

@Override
public String toString() {

    return "T";
}

@Override

public void move() {
    int newX = getLocation().x+3;
    int negX = getLocation().x-3;
    int newY = getLocation().y+3;
    int negY = getLocation().y-3;

    for(int i = 1; i <= 5; i++){
        setLocation(new Point(getLocation().x, newY));

    }
    for(int i = 1; i <= 5; i++){
        setLocation(new Point(negX, getLocation().y));      
    }

    for(int i = 1; i <= 5; i++){
        setLocation(new Point(getLocation().x, negY));
    }
    for(int i = 1; i <= 5; i++){
        setLocation(new Point(newX, getLocation().y));
    }

}

}

and finally, we have the animal class that turtle extends into:

import java.awt.Color;
import java.awt.Point;


public abstract class Animal {
private String type;
public Animal(String type, String name, Point location, Color color) {
    super();
    this.type = type;
    this.name = name;
    this.location = location;
    this.color = color;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Color getColor() {
    return color;
}
public void setColor(Color color) {
    this.color = color;
}
private String name;
private Point location;
private Color color;

public abstract String toString();
public abstract void move ();
public Point getLocation() {
    return location;
}
public void setLocation(Point location) {
    this.location = location;
}


}

I know that's a lot to sift through and I'm sorry if I'm not allowed to post questions about something this complex. I've just been running around in circles inside my head and I decided to ask other peoples opinions. Oh yeah, I'm using eclipse as well. Thank you for taking a look!

Was it helpful?

Solution

The problem is with your move method. All the new points are being set before the image can be repainted. Add the following to the Turtle class.

public class Turtle extends Animal {
    private int steps=0;

And then replace the move method with:

@Override

public void move() {
    if(steps<5)
        setLocation(new Point(getLocation().x+3, getLocation().y));
    else if(steps<10)
        setLocation(new Point(getLocation().x, getLocation().y+3));
    else if(steps<15)
        setLocation(new Point(getLocation().x-3, getLocation().y));
    else
        setLocation(new Point(getLocation().x, getLocation().y-3));
    steps++;
    if(steps>=20)
        steps=0;
}

Here, the position is only being re-evaluated every time the repaint function is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top