import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; package cumin.planewar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; public class MainFrame extends JFrame { /** * @param args */ public static final int FRAME_HEIGHT = 800; public static final int FRAME_WIDIH = 480; Image cacheImage = null; private Image background = null; private Image wait = null; private Image result = null; //手柄控制类 Handle controller = null; /** * 游戏窗口显示构造函数 */ public MainFrame() { // 游戏窗口 this.setLocation(0, 0); this.setSize(FRAME_WIDIH, FRAME_HEIGHT); this.setDefaultCloseOperation(EXIT_ON_CLOSE); background = Toolkit.getDefaultToolkit().createImage("res/背景.png"); wait = Toolkit.getDefaultToolkit().createImage("res/暂停界面.png"); result = Toolkit.getDefaultToolkit().createImage("res/result_t.png"); // 游戏窗口监听器 this.addKeyListener(new MyKeyListener()); // 游戏开始 this.setVisible(true); } @Override public void paint(Graphics g) { if (controller.getGameState() == Handle.GAMESTATE_START) controller.onDraw(g); else if (controller.getGameState() == Handle.GAMESTATE_STOP) { g.drawImage(result, FRAME_WIDIH / 2 - 160, FRAME_HEIGHT / 2 - 115, null); g.drawString("" + controller.getScore(), FRAME_WIDIH / 2 - 10, FRAME_HEIGHT / 2); } } @Override public void update(Graphics g) { // super.update(g); if (cacheImage == null) cacheImage = this.createImage(FRAME_WIDIH, FRAME_HEIGHT); Graphics gCacheImage = cacheImage.getGraphics(); gCacheImage.drawImage(background, 0, 0, FRAME_WIDIH, FRAME_HEIGHT, null); this.paint(gCacheImage); g.drawImage(cacheImage, 0, 0, null); } public static void main(String[] args) { // 主游戏开启 MainFrame mf = new MainFrame(); } }