How to use getters to return an image in java?

此生再无相见时 提交于 2019-12-11 09:42:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!