I've been trying to figure out what I am doing wrong here for awhile now. I am not sure if I am missing something completely obvious or what. But when I try and run this code as an Applet through Eclipse, it opens up the appletviewer which says the applet has started. But it doesn't do anything. If I try to restart it, it than says that the applet failed to initialize. I've done a couple traces through the code and the code inside the init method is being called. I do not get any errors or warnings at all.
GameCore.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public abstract class GameCore extends Applet implements Runnable {
private BufferedImage m_backBuffer;
private Graphics2D m_g2d;
private int m_screenWidth;
private int m_screenHeight;
private Thread m_gameLoop;
public abstract void gameStartup();
public abstract void gameRefreshScreen();
public GameCore( int width, int height ) {
// TODO: Create accessors for these
m_screenWidth = width;
m_screenHeight = height;
}
/**
*
*
*/
public void start() {
m_gameLoop = new Thread( this );
m_gameLoop.start();
}
/**
*
*
*/
public void run() {
Thread temp = Thread.currentThread();
while ( temp == m_gameLoop ) {
try {
Thread.sleep( 20 );
}
catch( InterruptedException e ) {}
repaint();
}
}
/**
*
*
*/
public void stop() {
m_gameLoop = null;
}
/**
*
*
*/
public Graphics2D getGraphics() {
return m_g2d;
}
/**
*
*
*/
public Applet applet() {
return this;
}
/**
*
*
*/
public void init() {
m_backBuffer = new BufferedImage( m_screenWidth, m_screenHeight, BufferedImage.TYPE_INT_RGB );
m_g2d = m_backBuffer.createGraphics();
gameStartup();
}
/**
*
* @param g
*/
public void update( Graphics g ) {
gameRefreshScreen();
paint( g );
}
/**
*
* @param g
*/
public void paint( Graphics g ) {
g.drawImage( m_backBuffer, 0, 0, this );
}
}
CoreTest.java
import java.awt.Color;
import java.awt.Graphics2D;
public class CoreTest extends GameCore {
public CoreTest() {
super( 640, 480 );
}
public void gameStartup() {
}
public void gameRefreshScreen() {
Graphics2D g2d = getGraphics();
g2d.setColor( Color.BLACK );
g2d.fillRect(0, 0, 640, 480 );
g2d.setColor( Color.WHITE );
g2d.drawString( "gameRefreshScreen() called." , 50,50 );
}
}
Thanks,
Casey