Hi,
I have to make a space invaders game and am having some problems with using images for my gaming entities. I have a GameEntites class that will be extended by AlienEntity, shot and ship. I have a class called ImageHandler that will be used to represent the entites with images. I’ve never used BufferImage’s before and can’t get it running.
My ImageHandler class is as follows:
import java.awt.image.BufferedImage;
import java.io.PrintStream;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class ImageHandler
{
private HashMap images;
public ImageHandler()
{
images = new HashMap();
}
public BufferedImage loadImage(String name)
{
try
{
java.net.URL url = null;
url = getClass().getResource(name);
return ImageIO.read(url);
}
catch(Exception e)
{
System.exit(0);
return null;
}
}
public BufferedImage getImage(String name)
{
BufferedImage image = (BufferedImage)images.get(name);
return image;
}
public HashMap getImages()
{
return images;
}
public void setImages(HashMap val)
{
images = val;
}
}
and my GameEntites class that will use the ImageHandler is as follows:
package InvadersGame;
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
public abstract class GameEntities {
private double x, y;
private int width, height;
private String imageName;
private boolean deletionMark = false;
private SpaceInvaders invaders;
private BufferedImage image;
public GameEntities(SpaceInvaders invaders)
{
this.invaders = invaders;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public Rectangle getBounds()
{
return new Rectangle((int)x, (int)y, width, height);
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public void setWidth(int width)
{
this.width = width;
}
public void setHeight(int height)
{
this.height = height;
}
public boolean deletionMarked()
{
return deletionMark;
}
public void setDeletionMarkedTrue(boolean value)
{
deletionMark = value;
}
public String getImageName()
{
return imageName;
}
public void setImageName(String name)
{
imageName = name;
image = ImageHandler.getImage(imageName);
height = image.getHeight();
width = image.getWidth();
}
public boolean collidesWith(GameEntities other)
{
Rectangle thisEntity = new Rectangle();
Rectangle otherEntity = new Rectangle();
thisEntity.setBounds((int)this.getX(), (int)this.getY(), this.getWidth(), this.getHeight());
otherEntity.setBounds((int)other.getX(),(int)other.getY(), other.getWidth(), other.getHeight());
return thisEntity.intersects(otherEntity);
}
public abstract void move();
public abstract void paint(Graphics g);
}
The ImageHandler class compiles fine but when I compile the GameEntites class the setImageName method causes an error and I get the following error:
Compiling 1 source file to C:\Documents and Settings\Nick\SpaceInvaders\build\classes
C:\Documents and Settings\Nick\SpaceInvaders\src\InvadersGame\GameEntities.java:89: cannot access InvadersGame.ImageHandler
bad class file: C:\Documents and Settings\Nick\SpaceInvaders\src\InvadersGame\ImageHandler.java
file does not contain class InvadersGame.ImageHandler
Please remove or make sure it appears in the correct subdirectory of the classpath.
image = ImageHandler.getImage(imageName);
1 error
BUILD FAILED (total time: 2 seconds)
Can someone PLEASE explain what’s going wrong and how I can sort the problem.
Any help would really be appreciated,
Cheers