1 // Person.java 每个方块人物是一个类 2 package test; 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 public class Person extends JButton implements FocusListener { 7 private int number; 8 private Color c=new Color(205,205,205); 9 private Font font=new Font("宋体",Font.BOLD,15); 10 11 public int getnum() { 12 return number; 13 } 14 public Person(int number,String s) { 15 super(s); 16 setBackground(c); 17 setFont(font); 18 this.number=number; 19 addFocusListener(this); 20 } 21 public void focusGained(FocusEvent e) {//获得焦点是调用 22 setBackground(Color.blue); 23 } 24 public void focusLost(FocusEvent e) {//失去焦点是调用 25 setBackground(c); 26 } 27 }
1 //package test; 2 //Hua_Rong_Road.java 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener { 7 private Person person[]=new Person[10]; 8 private JButton left,right,above,below; 9 private JButton restart=new JButton("重新开始"); 10 private JButton begin=new JButton ("欢迎来到华容道!!"); // 标题 11 private final int t_x=50;// 起始坐标 12 private final int t_y=100; 13 private final int l1=100;// 长度 14 private final int l2=15; 15 public Hua_Rong_Road() { 16 init(); 17 setBounds(200,200,520,800); 18 setVisible(true); 19 } 20 public void init() { 21 Color c=new Color(0,255,205); 22 begin.setBounds(100, 30, 300,50); 23 begin.setBackground(c); 24 25 restart.setBounds(150,650,200,50); 26 restart.addActionListener(this); 27 getContentPane().setLayout(null); 28 getContentPane().add(restart);//getContentPane()得到一个内容画板 29 getContentPane().add(begin); 30 31 String name[]={"女神吴嘉欣","迷弟一","迷弟二","迷弟三","迷弟四","迷弟五","迷弟六","迷弟七","迷弟八","迷弟九"}; 32 for(int k=0;k<name.length;k++) { 33 person[k]=new Person(k,name[k]); 34 person[k].addMouseListener(this); 35 person[k].addKeyListener(this); 36 getContentPane().add(person[k]); 37 } 38 person[0].setBounds(t_x+l1,t_y,2*l1,2*l1); 39 person[1].setBounds(t_x+l1,t_y+2*l1,2*l1,l1); 40 person[2].setBounds(t_x,t_y,l1,2*l1); 41 person[3].setBounds(t_x+3*l1,t_y,l1,2*l1); 42 person[4].setBounds(t_x,t_y+2*l1,l1,2*l1); 43 person[5].setBounds(t_x+3*l1,t_y+2*l1,l1,2*l1); 44 person[6].setBounds(t_x+l1,t_y+3*l1,l1,l1); 45 person[7].setBounds(t_x+2*l1,t_y+3*l1,l1,l1); 46 person[8].setBounds(t_x,t_y+4*l1,l1,l1); 47 person[9].setBounds(t_x+3*l1,t_y+4*l1,l1,l1); 48 person[9].requestFocus(); 49 left=new JButton(); 50 right=new JButton(); 51 above=new JButton(); 52 below=new JButton(); 53 getContentPane().add(left); 54 getContentPane().add(right); 55 getContentPane().add(above); 56 getContentPane().add(below); 57 left.setBounds(t_x-l2,t_y-l2,l2,l2*2+l1*5); 58 right.setBounds(t_x+4*l1,t_y-l2,l2,l2*2+l1*5); 59 above.setBounds(t_x-l2,t_y-l2,l2*2+l1*4,l2); 60 below.setBounds(t_x-l2,t_y+5*l1,l2*2+l1*4,l2); 61 validate(); 62 } 63 public void keyTyped(KeyEvent e){} 64 public void keyReleased(KeyEvent e){} 65 public void keyPressed(KeyEvent e) { 66 Person man=(Person)e.getSource(); 67 if(e.getKeyCode()==KeyEvent.VK_DOWN) 68 go(man,below); 69 if(e.getKeyCode()==KeyEvent.VK_UP) 70 go(man,above); 71 if(e.getKeyCode()==KeyEvent.VK_LEFT) 72 go(man,left); 73 if(e.getKeyCode()==KeyEvent.VK_RIGHT) 74 go(man,right); 75 } 76 public void mousePressed(MouseEvent e) {} 77 public void mouseReleased(MouseEvent e) {} 78 public void mouseEntered(MouseEvent e) {} 79 public void mouseExited(MouseEvent e) {} 80 public void mouseClicked(MouseEvent e) {} 81 public void go(Person man,JButton direction) { 82 boolean move=true; 83 Rectangle manRect=man.getBounds(); 84 int x=man.getBounds().x; 85 int y=man.getBounds().y; 86 if(direction==below) 87 y=y+l1; 88 else if(direction==above) 89 y=y-l1; 90 else if(direction==left) 91 x=x-l1; 92 else if(direction==right) 93 x=x+l1; 94 manRect.setLocation(x,y); 95 Rectangle directionRect=direction.getBounds(); 96 for(int k=0;k<10;k++) { 97 Rectangle personRect=person[k].getBounds(); 98 if((manRect.intersects(personRect))&&(man.getnum()!=k)) 99 move=false; 100 } 101 if(manRect.intersects(directionRect)) 102 move=false; 103 // if(move==true) //因为是女神吴嘉欣所以不受任何约束可以直接移动,如果需要改成正确的华容道形式把这一行注释打掉就行了 104 man.setLocation(x,y); 105 if (man.getnum()==0&&x==(t_x+l1)&&y==(t_y+3*l1)) {//游戏是否胜利 106 int res=JOptionPane.showConfirmDialog(null, "恭喜你游戏胜利", "是否继续", JOptionPane.YES_NO_OPTION); 107 if(res==JOptionPane.YES_OPTION){ 108 dispose(); 109 new Hua_Rong_Road(); 110 } 111 else return ; 112 } 113 } 114 public void actionPerformed(ActionEvent e) { 115 dispose(); 116 new Hua_Rong_Road(); 117 } 118 }
1 //package test; 2 //Mainclass.java 主类 3 public class Mainclass { 4 public static void main(String args[]) { 5 new Hua_Rong_Road(); 6 } 7 }
效果图 :
有bug?!都说了是迷弟版