Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use getters to return an image in java?
    primarykey
    data
    text
    <p>I have a class which has among its attributes an Image. I made a simple getter to return this image..</p> <pre><code>public Image getImage() { return image; } </code></pre> <p>Later I try to use this getter in a drawImage() but do not get an image.</p> <pre><code>g.drawImage(c.getImage(), c.getXcord(), c.getYcord(), null, this); </code></pre> <p>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.</p> <p>Here the class WoodlandCreatures</p> <pre><code>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; } */ </code></pre> <p>This class extends Animals</p> <pre><code>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; } </code></pre> <p>This is how I created the image I am using.</p> <pre><code>Image squir2; BufferedImage squir1 = createImage("images/Squirrel/Squirrel1.png"); squir2 = (squir1.getScaledInstance(100, 100, Image.SCALE_SMOOTH)); </code></pre> <p>and here is the createImage function...</p> <pre><code>private BufferedImage createImage(String x){ BufferedImage bufferedImage; try { bufferedImage = ImageIO.read(new File(x)); return bufferedImage; } catch (IOException e) { e.printStackTrace(); } return null; } </code></pre> <p>Hope some of this code clears some things up.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload