Jump to content
  • Advertisement
Sign in to follow this  
caseyd

[java] Applet failing to Initialize

This topic is 4232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Hi.

I dont claim to know a thing about applets. BUT, unless applets has some way of dealing with Runnables, where exactly do you start your thread? ;)

Share this post


Link to post
Share on other sites
As far as I understand it, the thread will be created and started in the applet's start method, which I have overridden here to do just that. I've created applet's using a similar code base before, but in comparing these two somewhat simpler classes to the other's I've written, I don't see any glaring difference in the way that I am trying to do this.

Share this post


Link to post
Share on other sites
You probably understand it better than me, I've never really dealt with applets. If the Applet's start method is its point of entry (like main method for apps), or is called through some funky Applet logic, then I can't see off the top of my head why it wouldnt work. But I guess someone with a little knowledge of Applets might be more helpful here. Sorry ;)

But, to feed my curiousity ... Try manually calling the start method in your constructor to see what happens? Good luck anyways.

Share this post


Link to post
Share on other sites
Quote:

Try manually calling the start method in your constructor to see what happens?


Tried that and calling the init method explicitly. Same thing happens that has been. It's really frustrating that I am not getting any Thread exceptions or any other kind of error. The Graphics2D object is being created as I've verified that through the debugger. I don't know why it isn't actually drawing anything.

Thanks for the suggestion however.

Share this post


Link to post
Share on other sites
You are clobbering the graphics object that will be passed to you by the Event Dispatch Thread. Remove the following code:

/**
*
*
*/

public Graphics2D getGraphics() {
return m_g2d;
}

Share this post


Link to post
Share on other sites
Could you possibly elaborate on this a little more? I was thinking about making that member protected and accessing directly in the derived class earlier this morning when I woke up. I haven't had a chance to change anything and won't until I get home from work.

Thanks,
Casey

Share this post


Link to post
Share on other sites
What I believe Captain means is that you are overrriding Container's getGraphics() method. If you need your specific backbuffer Graphics2D object, rename the method so that the Graphics object handled by your object is not ruined.

Share this post


Link to post
Share on other sites
Oh.. I didn't even realize getGraphics was a method of the Container. I knew it was something simple and silly...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!