Java初学之华容道游戏

1 package hhuarongdao;
2 
3 public class example {
4  public static void main(String args[])
5  {
6     new Hua_Rong_Road();
7  }
8  }
  1 package hhuarongdao;
  2 import java.awt.*;
  3 import javax.swing.*;
  4 
  5 import java.awt.event.*;
  6 public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener
  7 {
  8     Person [] person = new Person[10];   //person 为自定义的一个变量类型
  9     JButton left,right,above,below ;
 10     JButton restart = new JButton("重新开始");
 11     public Hua_Rong_Road()
 12     {
 13        init();
 14        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 15        setBounds(100,100,320,500);  //设置窗口初始位置以及大小的一个函数
 16        setVisible(true);  //窗口是否可见
 17        validate();  //使用 validate 方法会使容器再次布置其子组件。确保布局有效。
 18     }
 19     public void init()
 20     {
 21         setLayout(null); 
 22         add(restart);
 23         restart.addActionListener(this);
 24         String name [] = {"曹操","关","张","刘","周","黄","兵","兵","兵","兵"};
 25         for(int i=0;i<name.length;i++)
 26         {
 27           person[i]= new Person(i,name[i]);
 28           person[i].addMouseListener(this);
 29           person[i].addKeyListener(this);
 30           add(person[i]);
 31         }
 32         person[0].setBounds(104, 54, 100, 100);
 33         person[1].setBounds(104,154,100,50);
 34         person[2].setBounds(54,154,50,100);
 35         person[3].setBounds(204,54,50,100);
 36         person[4].setBounds(54,54,50,100);
 37         person[5].setBounds(204, 54, 50, 100);
 38         person[6].setBounds(54,254,50,50);
 39         person[7].setBounds(204,254,50,50);
 40         person[8].setBounds(104,204,50,50);
 41         person[9].setBounds(154,204,50,50);
 42         person[9].requestFocus();   //将焦点放在控件上面
 43         left=new JButton();
 44         right=new JButton();
 45         above=new JButton();
 46         below = new JButton();
 47         add(left);
 48         add(right);
 49         add(above);
 50         add(below);
 51         left.setBounds(49, 49, 5, 260);
 52         right.setBounds(254,49,5,260);
 53         above.setBounds(49,49,210,5);
 54         below.setBounds(49,304,210,5);
 55         validate();  //确保布局有效...这条语句不要也不会对程序有太大的影响
 56      }
 57     public void keyTyped(KeyEvent e){}
 58     public void keyReleased(KeyEvent e){}
 59     public void keyPressed(KeyEvent e)
 60     {
 61         Person man= (Person)e.getSource();
 62        if(e.getKeyCode()==KeyEvent.VK_DOWN)
 63            go(man,below);
 64        if(e.getKeyCode()==KeyEvent.VK_UP)
 65            go(man,above);
 66        if(e.getKeyCode()==KeyEvent.VK_LEFT)
 67            go(man,left);
 68        if(e.getKeyCode()==KeyEvent.VK_RIGHT)
 69            go(man,right);
 70     }
 71     public void mousePressed(MouseEvent e)
 72     {
 73          Person man=(Person)e.getSource();
 74          int x=-1,y=-1;
 75          x=e.getX();
 76          y=e.getY();
 77          int w=man.getBounds().width;
 78          int h=man.getBounds().height;
 79          if(y>h/2) go(man,below);
 80          if(y<h/2) go(man,above);
 81          if(x<w/2) go(man,left);
 82          if(x>w/2) go(man,right);
 83     }
 84     public void mouseReleased(MouseEvent e){}
 85     public void mouseEntered(MouseEvent e){}
 86     public void mouseExited(MouseEvent e){}
 87     public void go(Person man, JButton direction) {
 88         // TODO Auto-generated method stub
 89        boolean move=true;
 90        Rectangle manRect=man.getBounds();
 91        int x=man.getBounds().x;
 92        int y=man.getBounds().y;
 93        if(direction==below) y+=50;
 94        else if(direction==above)y-=50;
 95        else if(direction==left)x-=50;
 96        else if(direction==right)x+=50;
 97        manRect.setLocation(x, y);
 98        Rectangle directionRect=direction.getBounds();
 99        for(int k=0;k<10;k++)
100        {
101            Rectangle personRect=person[k].getBounds();
102            if(manRect.intersects(personRect)&&man.number!=k)
103                move=false;
104        }
105        if(manRect.intersects(directionRect))
106            move=false;
107        if(move==true) man.setLocation(x,y);
108     }
109     @Override
110     public void mouseClicked(MouseEvent e) {
111         // TODO Auto-generated method stub
112         
113     }    
114     public void actionPerformed(ActionEvent e)
115     {
116        dispose();  //注销
117        new Hua_Rong_Road();
118     }
119     
120 }
 1 package hhuarongdao;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 public class Person extends JButton implements FocusListener
 6 {
 7     
 8     int number ;
 9     Color c= new Color(255,245,170);
10     Font font=new Font("宋体",Font.BOLD,12);
11     Person(int number,String s)
12     {
13         super(s);
14         setBackground(c);
15         setFont(font);
16         this.number=number;
17         c=getBackground();
18         addFocusListener(this);
19     }
20     public void focusGained(FocusEvent e)
21     {
22         setBackground(Color.red);
23     }
24     public void focusLost(FocusEvent e)
25     {
26        setBackground(c);        
27     }
28 }

效果图:

Java初学之华容道游戏,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。