Alien Image
To spice things up further why not use an image for the alien? To do this we are going to edit the SpaceInvaders.java file and the Alien.java file. We need our Java program to load the image into its memory to do this we use the following line (SpaceInvaders.java):
alienImage = new javax.swing.ImageIcon("alien.jpg").getImage();
Note: In the quote marks you can see the name of an image file, in this case a jpg called alien. Therefore if we want to load another image file we can change this filename, but before we do that we need to edit the paint code to tell our Java program to actually paint the image. To do this, we change the following code in Alien.java:
/**
* Draw the image of the Alien
*/
public void drawAlien(Graphics g) {
if (!hitState) {
g.setColor(Color.red);
g.fillRect(leftPosition, heightPosition, ALIEN_WIDTH, ALIEN_HEIGHT);
}
}
To this:
/**
* Draw the image of the Alien
*/
public void drawAlien(Graphics g) {
if (!hitState) {
g.drawImage(alienImage, leftPosition, heightPosition, spaceInvaders);
}
}
Why don't you have a look on Google images to find some more exciting images. Or create your own in paint. Aim for an image size of height 25 pixels and width 15 pixels.
To compile your code type:
javac *.java
If you don't encounter any errors then you can run your game with this command:
java -cp . SpaceInvaders
If you get any errors just ask your lab tutor.
Return to the start to make more changes <here>.