hey I have 2 programs both wiht identical code. one does not work and one does.
here is the code for the one that is not working:
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class Actor {
protected int x,y;
protected int width, height;
protected String[] spriteNames;
protected int currentFrame;
protected int frameSpeed;
protected int t;
protected Stage stage;
protected SpriteCache spriteCache;
protected boolean markedForRemoval;
public Actor(Stage stage) {
this.stage = stage;
spriteCache = stage.getSpriteCache();
currentFrame = 0;
frameSpeed = 1;
t=0;
}
public void remove() {
markedForRemoval = true;
}
public boolean isMarkedForRemoval() {
return markedForRemoval;
}
public void paint(Graphics2D g){
// BufferedImage img = spriteCache.getSprite(spriteNames[currentFrame]);
g.drawImage( spriteCache.getSprite(spriteNames[currentFrame]), x,y, stage );//it cannot find this method
}
public int getX() { return x; }
public void setX(int i) { x = i; }
public int getY() { return y; }
public void setY(int i) { y = i; }
public int getFrameSpeed() {return frameSpeed; }
public void setFrameSpeed(int i) {frameSpeed = i; }
public void setSpriteNames(String[] names) {
spriteNames = names;
height = 0;
width = 0;
for (int i = 0; i < names.length; i++ ) {
BufferedImage image = spriteCache.getSprite(spriteNames[i]);
height = Math.max(height,image.getHeight());
width = Math.max(width,image.getWidth());
}
}
public int getHeight() { return height; }
public int getWidth() { return width; }
public void setHeight(int i) {height = i; }
public void setWidth(int i) { width = i; }
public void act() {
t++;
if (t % frameSpeed == 0){
t=0;
currentFrame = (currentFrame + 1) % spriteNames.length;
}
}
public Rectangle getBounds() {
return new Rectangle(x,y,width,height);
}
public void collision(Actor a){
}
}
And here is the method it is calling from:
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class SpriteCache {
private HashMap sprites;
public SpriteCache() {
sprites = new HashMap();
}
private BufferedImage loadImage(String name) {
URL url=null;
try {
url = getClass().getClassLoader().getResource(name);
return ImageIO.read(url);
} catch (Exception e) {
System.out.println("No se pudo cargar la imagen " + name +" de "+url);
System.out.println("El error fue : "+e.getClass().getName()+" "+e.getMessage());
System.exit(0);
return null;
}
}
public BufferedImage getSprite(String name) {
BufferedImage img = (BufferedImage)sprites.get(name);
if (img == null) {
img = loadImage(name);
sprites.put(name,img);
}
return img;
}
}
Any help would be deaply appreciated.
Thx in advance