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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • apache安装教程详解_Apache安装

    apache安装教程详解_Apache安装1.Apache安装1.下载apache下载链接2.将解压文件移动到目标目录(整个文件目录最好用英文不要有空格)3.命令行注册apache服务(不同于exe文件的安装)(以管理员身份打开cmdwin10)(管理员身份和普通用户身份区别)(在命令行中找到目标httpd.exe文件目录输入cdC:\wamp\Apache24\bin…

    2022年9月21日
    0
  • 查看idea是否激活成功[最新免费获取]

    (查看idea是否激活成功)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~2…

    2022年3月29日
    912
  • Python 根据AIC准则定义向前逐步回归进行变量筛选(二)

    Python 根据AIC准则定义向前逐步回归进行变量筛选(二)Python根据AIC准则定义向前逐步回归进行变量筛选(二)AIC简介AIC即赤池值,是衡量模型拟合优良性和模型复杂性的一种标准,在建立多元线性回归模型时,变量过多,且有不显著的变量时,可以使用AIC准则结合逐步回归进行变量筛选。AICD数学表达式如下:AIC=2p+n(log(SSE/n))AIC=2p+n(log(SSE/n))AIC=2p+n(log(SSE/n))其中,ppp…

    2022年5月24日
    180
  • deepin uos 是否开源_deepin docker

    deepin uos 是否开源_deepin docker国产操作系统UOSDeepinLinux源码编译安装Nginx

    2022年10月5日
    0
  • data:image/png;base64[通俗易懂]

    data:image/png;base64[通俗易懂]<imgsrc=”data:image/gif;base64,R0lGODlhJQAlAJECAL3L2AYrTv///wAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgACACwAAAAAJQAlAAACi5SPqcvtDyGYIFpF690i8xUw3qJBwUlSadmcLqYmGQu6KDIeM13beGzYWWy3DlB4IYaMk+Dso2RWkFCfLPcRvFbZxFLUDTt21BW56TyjRep1e20+i+eYMR145W2eefj+6VFmgTQ

    2022年10月12日
    0
  • 卷积神经网络(conv2d参数含义、卷积层、池化层)

    本文转载自罗翌新:中科大数学博士,深度学习医学应用专家;廖星宇:中科大硕士,计算机视觉专家,Face++资深工程师,《深度学习之Pytorch》作者;的深度学习理论与实战(基于TensorFlow实现)一、tf.nn.conv2d()卷积函数各参数解析二、卷积例子三、池化函数tf.nn.max_pool()函数解析…

    2022年4月7日
    201

发表回复

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

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