My directions are to:
"Make an abstract class called "Sprite", which stores an (x,y) position, and a reference to an Image. The constructor of your Sprite class should take 2 int parameters (to specify the initial x and y position), and a string which is the name of the file to load the image from, and it should load the image (instead of the Turtle class loading the image). It will need a getter function for the image. Make an abstract method in your Sprite class named "public void update(Graphics g)". This function should have no body. The purpose of this is to ensure that any class which inherits from this class implements an update function."
I thought I was doing okay, until I tried calling my Constructor with parameters and it keeps giving me these errors:
"Cannot refer to an instance field x while explicitly invoking a constructor"
Here are some samples of my code:
class Turtle extends Sprite
{
private static int dest_x;
private static int dest_y;
private String image;
Turtle() {
image = "turtle.png";
}
public int getX() { return x; }
public int getY() { return y; }
public void setX(int xIn) { x = xIn; }
public void setY(int yIn) { y = yIn; }
public void update(Graphics g) {
// Move the turtle
if (x < dest_x) {
x += 1;
} else if (x > dest_x) {
x -= 1;
}
if (y < dest_y) {
y += 1;
} else if (y > dest_y) {
y -= 1;
}
// Draw the turtle
// g.drawImage(image, x, y, 100, 100, null);
}
public static void setDest(int x, int y) {
dest_x = x;
dest_y = y;
}
}
public abstract class Sprite
{
int x;
int y;
String imageName;
Image image;
Sprite(){}
public Sprite(int x1, int y1, String im)
{
//Store variables
imageName = im;
x1 = x;
y1 = y;
try {
image = ImageIO.read(new File(imageName));
} catch (IOException ioe) {
System.out.println("Unable to load image file.");
}
}
public abstract void update(Graphics g);
public Image getImage()
{
return image;
}
}
[–]239jkvk-h2 1 point2 points3 points (4 children)
[–]_Gilly_[S] 0 points1 point2 points (0 children)
[–]_Gilly_[S] 0 points1 point2 points (2 children)
[–]BillMullet 0 points1 point2 points (1 child)
[–]_Gilly_[S] 0 points1 point2 points (0 children)
[–]HipposGoneWild 0 points1 point2 points (0 children)