gameapplet.java

来自「how to make a game using java.」· Java 代码 · 共 111 行

JAVA
111
字号

/**
 *
 * The GameApplet is used to create an Applet-Thread. It extends
 * the Applet class and contains the Runnable interface.
 * (C)2002 by [ISSoft]
 *
 **/

package GameLib;

import java.applet.*;
import java.awt.*;

abstract public class GameApplet extends Applet implements Runnable
{

	// This is the main thread, used for ticks.
	public Thread 	gameThread = null;
	
	// The sleep time of the GameApplet
	private long	sleepTime= 50;

	/* Initialize. */
	public void init()
	{
	}



	/* Start the thread. */
	public void start()
	{
		if(gameThread == null)
		{ 
			gameThread = new Thread(this); 
			gameThread.start();
			//gameThread.setPriority (Thread.MAX_PRIORITY);
  		}

	}



	/* Stop the thread. */
	public void stop()
	{
		if (gameThread != null)
		gameThread.stop (); //kill thread when applet is stopped
		gameThread = null;
	}



	public void setSleepTime(long millis)
	{
		this.sleepTime = millis;
	}

	public void setSleepTime(int millis)
	{
		this.sleepTime = (long)millis;
	}


	public long getSleepTime ()
	{
		return this.sleepTime;
	}



	public void run()
	{


		while (gameThread != null)
		{
			game(1);
      			try
			{
				gameThread.sleep(getSleepTime());
			}
			catch(InterruptedException e)
			{
				gameException();
			}
			game(2);
		}
  
		gameThread = null;
  
	}

	public void game(int id) {}

	public void gameException() {}

	public void update(Graphics g)
	{	
		paint(g);
	}


	public String getAppletInfo()
	{
		return ("The GameApplet, a class that creates an Thread from an Applet using the Applet class and the Runnable interface.");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?