标签:cat his ide color get efault java.awt override frame
package tanchishe;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Snack extends JFrame implements KeyListener{
int x=100,y=100;
public Snack(){
super("贪吃蛇");
setSize(400,400);
setVisible(true);
addKeyListener(this);
new PaintThread().start();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillRect(x,y,20,20);
}
public static void main(String[] args){
Snack sn=new Snack();
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case 37:
{x-=10;
break;
}
case 39:
{x+=10;
break;}
case 40:
{y+=10;break;}
case 38:
{y-=10;break;}
default: break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
}
标签:cat his ide color get efault java.awt override frame
原文地址:http://www.cnblogs.com/jgrs/p/7617767.html