问题
I have a class which has among its attributes an Image. I made a simple getter to return this image..
public Image getImage() {
return image;
}
Later I try to use this getter in a drawImage() but do not get an image.
g.drawImage(c.getImage(), c.getXcord(), c.getYcord(), null, this);
As you can see I have other getters for this class (getXcord, getYcord) which work fine, but I can't seem to get the image.
Here the class WoodlandCreatures
public class WoodlandCreatures extends Animals {
public String favPlant;
public WoodlandCreatures(String fPlant, String animal, Image im, int x, int y) {
this.favPlant = fPlant;
this.animalType = animal;
this.image = im;
this.xCord = x;
this.yCord = y;
}
/*
public Image getImage() {
return image;
}
*/
This class extends Animals
public abstract class Animals {
public String animalType;
public Image image;
public int xCord;
public int yCord;
public Image getImage() {
return image;
}
public int getXcord() {
return xCord;
}
public int getYcord() {
return yCord;
}
This is how I created the image I am using.
Image squir2;
BufferedImage squir1 = createImage("images/Squirrel/Squirrel1.png");
squir2 = (squir1.getScaledInstance(100, 100, Image.SCALE_SMOOTH));
and here is the createImage function...
private BufferedImage createImage(String x){
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new File(x));
return bufferedImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Hope some of this code clears some things up.
来源:https://stackoverflow.com/questions/13258790/how-to-use-getters-to-return-an-image-in-java