PlaneFrame.java
上传用户:xueping400
上传日期:2022-02-08
资源大小:888k
文件大小:3k
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.imageio.ImageIO;
- public class PlaneFrame extends Frame {
- static final int WINDOW_WIDTH =700;
- static final int WINDOW_HEIGHT = 800;
- private Plane plane;
- private Image bufImage;
- public static ArrayList<Shell> shell = new ArrayList<Shell>();
- public void update(Graphics g) {
-
- if(bufImage == null){
- bufImage = this.createImage(PlaneFrame.WINDOW_WIDTH, PlaneFrame.WINDOW_HEIGHT);
- }
- Graphics gBuf = bufImage.getGraphics();
- Color clr = gBuf.getColor();
- paint(gBuf);// 第二步:调用paint方法
- gBuf.setColor(clr);
- clr = g.getColor();
- g.drawImage(bufImage, 0, 0, PlaneFrame.WINDOW_WIDTH,PlaneFrame.WINDOW_HEIGHT, null);
- g.setColor(clr);
- }
-
- public void paint(Graphics g) {
- Color clr = g.getColor();
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
- plane.paint(g);
- Shell s = null;
- for (int i = 0; i < shell.size(); i++) {
- s = shell.get(i);
- if (s.isLive()) {
- s.paint(g);
- }else{
- shell.remove(i);
- }
- }
- g.setColor(Color.RED);
- g.drawString("炮弹个数:" + shell.size(), 5, 40);
- g.setColor(clr);
- }
-
- public void lunchFrame(){
- plane = new Plane();
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension de = tk.getScreenSize();
- int w = (int)de.getWidth();
- int h = (int)de.getHeight();
- int x = (w - WINDOW_WIDTH) / 2;
- int y = (h - WINDOW_HEIGHT) / 2;
- this.setLocation(x, y);
- this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- this.setTitle("PlaneWar 5.0");
-
- this.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
-
- this.addKeyListener(new KeyAdapter(){
-
- public void keyPressed(KeyEvent e) {
- plane.keyPressed(e.getKeyCode());
- }
- public void keyReleased(KeyEvent e) {
- plane.keyRelease(e.getKeyCode());
- }
-
- });
-
- this.setResizable(false);
- this.setVisible(true);
- PaintThread pt = new PaintThread();
- pt.start();
- }
- public static void main(String[] args) {
- new PlaneFrame().lunchFrame();
- }
- private class PaintThread extends Thread{
- public void run() {
- while(true){
- try {//建立线程,设置重画时间
- repaint();//第一步:调用update()方法,在调用paint()方法;
- sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }