Java初学之华容道游戏

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初学之华容道游戏

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109772.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 谈谈 Java 中自定义注解及使用场景

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:快给我饭吃 www.jianshu.com/p/a7bedc771204 Java自定义注解一般使用场景为:自…

    2021年6月24日
    79
  • react路由跳转

    react路由跳转React中几种页面跳转方式1、使用react-router-dom中的Link实现页面跳转一般适用于,点击按钮或其他组件进行页面跳转,具体使用方式如下:<Linkto={{pathname:’/path/newpath’,state:{//页面跳转要传递的数据,如下data1:{},data2:[]},}}><Butt

    2022年5月27日
    396
  • 六种进程间通信方式[通俗易懂]

    六种进程间通信方式[通俗易懂]前言开场小故事炎炎夏日,张三骑着单车去面试花了1小时,一路上汗流浃背。结果面试过程只花了5分钟就结束了,面完的时候,天还是依然是亮的,还得在烈日下奔波1小时回去。面试五分钟,骑车两小时。你看,张三因面试没准备好,吹空调的时间只有…

    2022年10月11日
    0
  • 百度–计算机安全

    百度–计算机安全木马终结 …电脑病毒是什么东西呢?是否会像其他病毒,如“H5N1”、“O-157大肠杆菌”、“HIV”一样对人体造成伤害呢?电脑病毒是会造成伤害,但不是对你造成伤害,而是对你的电脑系统造成一定的伤害。其实,电脑病毒是一段非常小的(通常只有几KB)会不断… 14508字 2007-06-20 popo8819 C盘杀手 …病毒病毒名称 : W97M/Thus.A 别名:C盘杀手 病毒特点

    2022年7月25日
    10
  • web.config中customErrors节点的配置「建议收藏」

    web.config中customErrors节点的配置「建议收藏」一、customErrors节点在web.config中的位置configuration->system.web-> customerErrors 二、customErrors节点常见用法三、customErrors节点属性值介绍 1、de

    2022年7月16日
    19
  • [Arm]使用modprobe时 “modprobe: can‘t change directory to …”

    [Arm]使用modprobe时 “modprobe: can‘t change directory to …”最近在加载一个叫dmatest的模块时,报错modprobe:can’tchangedirectoryto’…’:Nosuchfileordirectory,做一个问题记录。首先,modprobe的解释:modprobe是linux的一个命令,可载入指定的个别模块,或是载入一组相依的模块。modprobe会根据depmod所产生的相依关系,决定要载入哪些模块。若…

    2025年7月5日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号