# 3
Thanks for your help ArikArikArik, the problem with that method is.... 1. im not too sure how to use it :P and 2. In my original code the images are added to the tracker which waits from all the images to be loaded before it continues.
import javax.swing.*;
import java.applet.AudioClip;
import java.awt.*;
public class ICGGSApplet extends JApplet implements Runnable {
// constants describing possible game states
private static final int SHOW_MOVIE = 1;
private static final int PLAY_GAME = 2;
private static final int STOPPED = 3;
private static final int SHOW_MOVIE2 = 4;
private int gameState;// the current game state
private String name;// the player's name
private long frameStartTime; // the time the last frame was displayed
private Thread gameLoop;// thread to run the game and movie
// associated objects
private Movie theMovie;// Panel to display the movie
private MonsterGame theGame; // Panel to display the game
private ICGGSPanel currentPanel; // the currently displayed Panel (can be null)
private Movie2 theMovie2; // Panel to display the second movie
/**
* Called when applet is first loaded
* sets up the attributes, load media, creates game objects
* and sets up interface
*/
public void init() {
// neither the movie nor the game is currently playing
currentPanel = null;
gameState = STOPPED;
name = "";
// load up the audio files
AudioClip theAudio = getAudioClip(getDocumentBase(), "../media/shock.wav");
AudioClip theAudio2 = getAudioClip(getDocumentBase(), "../media/applause.wav");
AudioClip theAudio3 = getAudioClip(getDocumentBase(), "../media/bomb.wav");
AudioClip theAudio4 = getAudioClip(getDocumentBase(), "../media/cashtill.wav");
AudioClip theAudio5 = getAudioClip(getDocumentBase(), "../media/pacman.wav");
// create a media tracker to monitor the loading of the images
MediaTracker theTracker = new MediaTracker(this);
// load up the images for the movie
Image movieImages[] = new Image[25];
for (int i=0; i<movieImages.length; i++) {
movieImages[i] = getImage(getDocumentBase(), "../media/movie" + (i+1) + ".jpg");
theTracker.addImage(movieImages[i], i);
}
// load up the images for the second movie
Image movieImages2[] = new Image[25];
for (int i=0; i><movieImages2.length; i++) {
movieImages2[i] = getImage(getDocumentBase(), "../media/firingGun" + (i+1) + ".jpg");
theTracker.addImage(movieImages2[i], i);
}
// load up the images for the munchies
Image image1 = getImage(getDocumentBase(), "../media/munchie1.gif");
theTracker.addImage(image1, 4);
Image image2 = getImage(getDocumentBase(), "../media/munchie2.gif");
theTracker.addImage(image2, 5);
Image image3 = getImage(getDocumentBase(), "../media/munchie3.gif");
theTracker.addImage(image3, 6);
// wait for all the images to be loaded
try {
theTracker.waitForAll();
} catch (InterruptedException e) {
}
// create the movie object
theMovie = new Movie(movieImages);
theMovie2 = new Movie2(movieImages2);
// create the game object
theGame = new MonsterGame(image1, image2, image3, theAudio, theAudio2, theAudio3, theAudio4, theAudio5);
this.getContentPane().setBackground(Color.WHITE);
// Initialise key-input functionality
addKeyListener(new Input());// add keyboard listener to applet
}
/**
* Called when the Applet is ready to start executing
*/
public void start() {
// ask the user for their name
name = JOptionPane.showInputDialog(this, "What is your name?");
// start running the movie/game animation
if (gameLoop == null) {
gameLoop = new Thread(this);
gameLoop.start();
}
}
/**
* Called when the gameLoop thread is started
* loops continuously until user quits
*/
public void run() {
int input = Input.NONE;
// game loop
while (input != Input.QUIT) {
// what is the current time?
frameStartTime = System.currentTimeMillis();
input = checkInput();
if (gameState != STOPPED) // only necessary if the movie or game is showing
{
currentPanel.processInput(input);
currentPanel.update();
currentPanel.repaint();
} else
showStatus("Press 'G' to start game, 'M' to show movie or 'F' to show the second movie");
frameDelay(100);
}
}
/**
* called when the Applet is stopped
* stops the gameLoop thread
*/
public void stop() {
if (gameLoop != null) {
gameLoop.interrupt();// stops the animation thread
gameLoop = null;
}
}
/**
* called from the gameLoop
* checks for the last key stroke input by the user
* @return int - Input class constant representing user input
*/
public int checkInput() {
int input = Input.checkUserInput();
switch (input) // how the input is handled depends on the current game state
{
case Input.GAME:
// the user wants to play the game
if (gameState == STOPPED) // not currently playing a game or movie
{
// display the panel showing the game
currentPanel = theGame;
this.getContentPane().add(BorderLayout.CENTER, currentPanel);
this.validate();
// set up a new game
theGame.resetGame();
gameState = PLAY_GAME;
showStatus("Use arrow keys to move Monster, S key to stop");
} // otherwise ignore
break;
case Input.MOVIE:
// the user wants to see the movie
if (gameState == STOPPED) // not currently playing a game or movie
{
// display the panel showing the movie
currentPanel = theMovie;
this.getContentPane().add(BorderLayout.CENTER, currentPanel);
this.validate();
gameState = SHOW_MOVIE;
showStatus("Press S key to stop");
} // otherwise ignore
break;
case Input.MOVIE2:
// the user wants to see the second movie
if (gameState == STOPPED) // not currently playing a game or movie
{
// display the panel showing the movie
currentPanel = theMovie2;
this.getContentPane().add(BorderLayout.CENTER, currentPanel);
this.validate();
gameState = SHOW_MOVIE2;
showStatus("Press S key to stop");
} // otherwise ignore
break;
case Input.STOP:
// the user wants to stop the movie or game being shown
if (gameState == SHOW_MOVIE ) {
this.getContentPane().remove(currentPanel);
currentPanel = null;
JOptionPane.showMessageDialog(this,
"OK " + name + " I've stopped the movie");
} else if (gameState == PLAY_GAME) {
this.getContentPane().remove(currentPanel);
currentPanel = null;
JOptionPane.showMessageDialog(this,
"OK " + name + " I've stopped the game");
} else if (gameState == SHOW_MOVIE2 ) {
this.getContentPane().remove(currentPanel);
currentPanel = null;
JOptionPane.showMessageDialog(this,
"OK " + name + " I've stopped the movie"); }
gameState = STOPPED;
showStatus("Press 'G' to start game, 'M' to show movie");
break;
}
Input.reset(); // reset the input module
requestFocus();
this.repaint(); // redisplay the applet
return input; // return for further processing (eg to control movement of Monster)
}
/**
* called from the gameLoop
* pauses between frames
* @param millis long - minimum frame time
*/
public void frameDelay(long millis) {
long currentTime = System.currentTimeMillis(); // what time is it now?
long elapsedTime = frameStartTime - currentTime; // how long has it been since the start of the frame?
if (elapsedTime >< millis) // is it time to showing the new frame yet?
try {
Thread.sleep(millis - elapsedTime); // pause for the remaining time
} catch(Exception e) {
}
}}
Thats the whole code for the applet- preloadin the image (and also everything that doesnt relate to this problem lol). Ive asked around and the answer I was given I wasnt satisfied with- make the files smaller/delete some- the files are already at their smallest about 10kb each. And also apparently (I didnt know this...) but due to security restirictions I wouldnt be able to increase the heap size anyway (this is to run the applet on my own... and others computers).
So Im really too sure what to do at the minute. Anymore help would be appreciated though.
Thank you!
Senor-Cojones