博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA SE 框架之俄罗斯方块的效果
阅读量:5371 次
发布时间:2019-06-15

本文共 3261 字,大约阅读时间需要 10 分钟。

Mygame

package com.sun.c;import java.awt.event.KeyListener;import com.sun.v.MyJpanel;import com.sun.v.Myjframe;public class Mygame {    public Mygame() {        // TODO Auto-generated constructor stub    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Myjframe jframe = new Myjframe();        MyJpanel panel = new MyJpanel();                jframe.add(panel);        jframe.addKeyListener(panel);    }} Myjframe
package com.sun.v;import javax.swing.JFileChooser;import javax.swing.JFrame;/* * 这里采用MVC的设计模式,讲逻辑和页面分开, */public class Myjframe extends JFrame{    public Myjframe(){               this.setTitle("myworld");       this.setVisible(true);       this.setSize(400,400);       this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);    }     }
 

MyJpanel

package com.sun.v;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JPanel;import com.sun.m.Dog;public class MyJpanel extends JPanel implements KeyListener,Runnable{    public MyJpanel() {        //MyJpanel jp = new MyJpanel();        Thread td = new Thread(this);        td.start();    }    Dog dog = new Dog(20,20,2);        public void paint(Graphics g){        super.paint(g);        g.draw3DRect(dog.x, dog.y,20 ,20, false);    }    @Override    public void keyTyped(KeyEvent e) {        // TODO Auto-generated method stub            }    @Override    public void keyPressed(KeyEvent e) {        // TODO Auto-generated method stub        //System.out.println("sunzhiyan");        switch (e.getKeyCode()){        case KeyEvent.VK_W:            dog.direct = 0;            dog.move(dog.direct);         break;        case KeyEvent.VK_D:            dog.direct = 1;            dog.move(dog.direct);         break;        case KeyEvent.VK_S:            dog.direct = 2;            dog.move(dog.direct);         break;        case KeyEvent.VK_A:            dog.direct = 3;            dog.move(dog.direct);         break;        }                this.repaint();    }    @Override    public void keyReleased(KeyEvent e) {        // TODO Auto-generated method stub            }    @Override    public void run() {        // TODO Auto-generated method stub        while(true){            try {                Thread.sleep(1000);                //System.out.println("我被调用了");                dog.direct = 2;                dog.move(dog.direct);                this.repaint();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                    }            }}

 

 

Dog

 

 
1 package com.sun.m; 2  3 public class Dog { 4     public int x; 5     public int y; 6     public int direct ; 7     public Dog(int x,int y, int direct){ 8         this.x = x; 9         this.y = y;10         this.direct = direct;11     }12     public void move(int direct){13         switch (direct){        14         case 0:15             this.y--;16             break;17         case 1:18             this.x++;19             break;20         case 2:21             this.y++;22             break;23         case 3:24             this.x--;25             break;      26         }27         28     }29 }
 

 

 

 

转载于:https://www.cnblogs.com/sunxun/p/3908727.html

你可能感兴趣的文章
ubuntu15.10运行android studio出错unable to run mksdcard sdk tool
查看>>
HashMap面试知多少
查看>>
Effective C# 学习笔记(二十七)使你的类型可被序列化
查看>>
LDAP客户端配置
查看>>
(转)NAT原理与NAT穿越
查看>>
13.内存原理
查看>>
24.函数信号机制(本质上就是函数指针)
查看>>
The dependency `xxx` is not used in any concrete target.
查看>>
Bootstrap 中 下拉菜单和滚动监听插件(十一)(持续更新中。。。)
查看>>
团队-科学计算器-项目总结
查看>>
python 发邮件
查看>>
[Codeforces Round #247 (Div. 2)] A. Black Square
查看>>
[HDU] 1285 确定比赛名次(拓扑排序)
查看>>
UML第四次作业:绘制活动图
查看>>
GOOGLE API - 翻译, 后台c# 调用前台js代码, asp.net后台调用js代码
查看>>
JVM内存的设置
查看>>
HBase API 基础操作
查看>>
【洛谷4920】[WC2015] 未来程序(提答题)
查看>>
1003. Check If Word Is Valid After Substitutions
查看>>
PHP与最丑的后台管理系统
查看>>