?? hua_rong_road.java
字號:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class People extends Button implements FocusListener{
Rectangle rect=null;
int left_x,left_y;
int width,height;
String name; int number;
People(int num,String s,int x,int y,int w,int h,Hua_Rong_Road road){
super(s);
name=s; this.number=number;
left_x=x; left_y=y;
width=w; height=h; setBackground(Color.orange);
road.add(this); addKeyListener(road);
setBounds(x,y,w,h);addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e){
setBackground(Color.red);
}
public void focusLost(FocusEvent e){
setBackground(Color.orange);
}
}
public class Hua_Rong_Road extends Applet implements KeyListener,ActionListener{
People people[]=new People[10];
Rectangle left,right,above,below;
Button restart=new Button("重新開始");
public void init(){
setLayout(null); add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
people[0]=new People(0,"曹操",104,54,100,100,this);
people[1]=new People(1,"關羽",104,154,100,100,this);
people[2]=new People(2,"張飛",54,154,50,100,this);
people[3]=new People(3,"劉備",204,154,50,100,this);
people[4]=new People(4,"張遼",54,54,50,100,this);
people[5]=new People(5,"曹仁",204,54,50,100,this);
people[6]=new People(6,"兵",54,254,50,50,this);
people[7]=new People(7,"兵",104,204,50,50,this);
people[8]=new People(8,"兵",154,204,50,50,this);
people[9].requestFocus();
left=new Rectangle(49,49,5,260);
people[0].setForeground(Color.white);
right=new Rectangle(254,49,5,260);
above=new Rectangle(49,49,210,5);
below=new Rectangle(49,304,210,5);
}
public void paint(Graphics g){
g.setColor(Color.cyan);
g.fillRect(49,49,5,260);
g.fillRect(254,49,5,260);
g.fillRect(49,49,210,5);
g.fillRect(49,309,210,5);
g.setColor(Color.red);
g.drawString("曹操到達該位置",110,20);
}
public void keyPressed(KeyEvent e){
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN){
man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i)){
man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below)){
man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP){
man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i)){
man.left_y=man.left_y+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below)){
man.left_y=man.left_y-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i)){
man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below)){
man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
man.left_x=man.left_x+50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
for(int i=0;i<10;i++)
{if((man.rect.intersects(people[i].rect))&&(man.number!=i)){
man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below)){
man.left_x=man.left_x-50;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e){
this.removeAll();
this.init();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -