79.游戏项目-加载窗口-加载动图
1 package test; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Frame; 5 import java.awt.Graphics; 6 import java.awt.Image; 7 import java.awt.event.WindowAdapter; 8 import java.awt.event.WindowEvent; 9 10 import test.GameFrame.PaintThread; 11 /** 12 * 游戏窗口类 13 * @author Nicholas 14 * 窗口以左上角为坐标 15 */ 16 public class GamePicture extends Frame {//GUI编程AWT,SWING 17 Image img = GameUtil.getImage("picture/test.jpeg");//加载图片 18 //加载窗口 19 public void launchFrame(){ 20 setSize(800,600);//设置大小 21 setLocation(100,100);//设置初始位置 22 setVisible(true);//设置图形可见 23 24 new PaintThread().start();//自动重画线程 25 26 addWindowListener(new WindowAdapter(){//实现窗口关闭 27 public void windowClosing(WindowEvent e) { 28 System.exit(0); 29 } 30 }); 31 } 32 private double x=0,y=0; 33 public void paint(Graphics g) { 34 g.drawImage(img, (int)x, (int)y, null);//设置显示图片 35 //设置轨迹 36 x+=1; 37 y+=1; 38 } 39 40 /** 41 * 定义一个重画窗口的线程类,是一个内部类 42 * @author Nicholas 43 * 44 */ 45 class PaintThread extends Thread{ 46 public void run(){ 47 while(true){ 48 repaint(); 49 try { 50 Thread.sleep(40);//40毫秒 1s=1000ms; 51 } catch (InterruptedException e) { 52 e.printStackTrace(); 53 } 54 } 55 } 56 } 57 58 public static void main(String[] args) { 59 GamePicture gp=new GamePicture(); 60 gp.launchFrame(); 61 } 62 63 }