周六无聊,花了二个小时写了一个游戏的大体框架.
Game.java
@SuppressWarnings("unused")
public class Game


{
private Scene scene;
private GameEngine gameEngine;
public Game()

{
this.scene = new Scene();
this.gameEngine = new GameEngine();
}
public static void main(String[] args)

{
new Game();
}
}

GameEngine.java
import java.awt.Graphics2D;
import java.awt.Image;


public class GameEngine implements Runnable


{

/** *//** 引擎重画间隔 */
private int engineInterval = 30;

/** *//** 游戏引擎刷新线程 */
private Thread refresher;

/** *//** 游戏主窗口 */
private GameFrame gameFrame;

/** *//** 双缓存作图区 */
private Image imageBuffer;

/** *//** ImageIterm缓存 */
public static ImageItermCahe imageItermCache = new ImageItermCahe();
public GameEngine()

{
this.refresher = new Thread(this);
this.gameFrame = new GameFrame();
this.imageBuffer = this.gameFrame.createImage(
this.gameFrame.getWidth(), this.gameFrame.getHeight());

/** *//** 启动游戏引擎 */
this.refresher.start();
}
private void working()

{
Graphics2D imageBufferGraphics = (Graphics2D)(this.imageBuffer.getGraphics());
imageBufferGraphics.clearRect(0, 0, 785, 360);
for(ImageIterm imgIterm : GameEngine.imageItermCache.getImgItermLst())

{

/** *//** 删除已经删除的图项 */
if(GameEngine.imageItermCache.getImgItermMap().get(imgIterm.getId()) == null)

{
GameEngine.imageItermCache.getImgItermLst().remove(imgIterm);
continue;
}
imageBufferGraphics.drawImage(imgIterm.getImg(),
imgIterm.getXTargetTop(),
imgIterm.getYTargetTop(),
imgIterm.getXTargetBot(),
imgIterm.getYTargetBot(),
imgIterm.getXSourceTop(),
imgIterm.getYSourceTop(),
imgIterm.getXSourceBot(),
imgIterm.getYSourceBot(), null);
}
this.gameFrame.setImageBuffer(this.imageBuffer);
this.gameFrame.repaint();
}
public void run()

{
while(true)

{
this.working();
try

{
Thread.sleep(this.engineInterval);
}
catch(Exception ex)

{
ex.printStackTrace();
}
}
}
public void setEngineInterval(int engineInterval)

{
this.engineInterval = engineInterval;
}
}

GameFrame.java
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class GameFrame extends JFrame


{

/** *//** 缓存作图区 */
private Image imageBuffer;

public GameFrame()

{
this.setTitle("游戏作图区");
this.setSize(785, 360);

/** *//** 定位游戏窗口在屏幕上的位置 */
this.setLocation((this.getToolkit().getScreenSize().width - this.getWidth()) / 2,
(this.getToolkit().getScreenSize().height - this.getHeight()) / 2);

this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setVisible(true);
}

public void update(Graphics g)

{
if (this.imageBuffer != null)

{
this.getGraphics().drawImage(imageBuffer, 0, 0, this);
}
}
public void setImageBuffer(Image imageBuffer)

{
this.imageBuffer = imageBuffer;
}
}

还有三个类在下一篇.