游戏图片的显示

显示图片:  

        在onDisplayUpdate()方法中调用工具类,画出图片:DrawUtils.draw("img/2.jpg", 0, 0);

        关于界面的坐标系:
                左上角是原点(0,0)
                x轴越往右越大
                y轴越往下越大

                设置图片坐标的时候是把图片的左上角设置为指定的坐标位置

 

 

显示步骤:

      

 1 import java.io.IOException;
 2 
 3 import org.itheima.game.utils.DrawUtils;
 4 
 5 public class Tank {
 6     int x;    //定义成员变量存储坐标
 7     int y;
 8     String imagePath = "res/img/tank_u.gif";  //定义图片路径
 9     
10     public Tank(int x,int y) {        //定义一个构造方法   接收图片坐标
11         this.x=x;
12         this.y=y;
13     }
14     public void draw(){        //显示方法
15         try {
16             DrawUtils.draw(imagePath, x, y);
17         } catch (IOException e) {
18             // TODO 自动生成的 catch 块
19             e.printStackTrace();
20         }
21     }
22 }
1、创建一个类
 1 import org.itheima.game.Window;
 2 
 3 public class GameSet extends Window {
 4     Tank tank;    //成员变量
 5     public GameSet(String title, int width, int height, int fps) {
 6         super(title, width, height, fps);
 7         // TODO 自动生成的构造函数存根
 8     }
 9 
10     @Override
11     protected void onCreate() {        //初始化界面的设置
12         tank = new Tank(0,0);        //生成图片坐标
13     }
14 
15     @Override
16     protected void onMouseEvent(int key, int x, int y) {
17         // TODO 自动生成的方法存根
18 
19     }
20 
21     @Override
22     protected void onKeyEvent(int key) {
23         // TODO 自动生成的方法存根
24 
25     }
26 
27     @Override
28     protected void onDisplayUpdate() {    //实时更新显示
29         tank.draw();    //画一个tank类对象
30     }
31 
32 }
2、设置类添加图片

 

随笔说:

      在方法中调用类的方法必须将类定义在方法外,也就是成员变量,所有方法都能

    够访问到这个类。窗口左上角的坐标为x=0,y=0

 

posted @ 2017-08-21 22:58  B_Lasting_尊  阅读(182)  评论(0编辑  收藏  举报