• 开窗户游戏


    package tuxing;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Calendar;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPopupMenu;
     
    class ChuangKou extends JFrame implements ActionListener
    {
        int x=0,y=0;
        Panel p1 = new Panel();
        int COUNT = 2;
        JButton[][] jButtons = new JButton[COUNT][COUNT];
        int[][] a = new int[][]//到达周围的四个窗户以及本身
        {
        { 1, 0 },
        { 0, 1 },
        { -1, 0 },
        { 0, -1 },
        {0,0} };
      public void start()
      {
          //this.removeAll();
          jButtons = new JButton[COUNT][COUNT];
     
            for (int i = 0; i < COUNT; i++)
                for (int j = 0; j < COUNT; j++)
                {
     
                    jButtons[i][j] = new JButton();
                    jButtons[i][j].setName(i + " " + j);
                    //jButtons[i][j].setText(i + " " + j);
                    jButtons[i][j].setBackground(Color.white);
                    jButtons[i][j].addActionListener(this);
                    p1.add(jButtons[i][j]);
                }
     
      }
        public ChuangKou(int n,int x,int y)
        {
            this.COUNT=n;
            setSize(500, 500);
            //setUndecorated(true);
            //this.setOpacity(32f);
             setLocation(x, y);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setTitle("开窗户"+COUNT);
             p1.setLayout(new GridLayout(COUNT, COUNT));
            add(p1);
            start();
            validate();
     
     
        }
     
        public boolean isWin()
        {
            int op = 0;
            for (int i = 0; i < COUNT; i++)
                for (int j = 0; j < COUNT; j++)
                    if (jButtons[i][j].getBackground() == Color.white)
                        op = 1;
     
            if (op == 0)
            {
                for (int i = 0; i < COUNT; i++)
                    for (int j = 0; j < COUNT; j++)
                        jButtons[i][j].setBackground(Color.red);
     
     
                 //p1.removeAll();
                return true;
            }
     
            return false;
        }
     
        public void actionPerformed(ActionEvent e)
        {
            for (int i = 0; i < 5; i++)
            {
                String s = ((JButton) e.getSource()).getName();
                String[] s1 = s.split(" ");
                int x = Integer.valueOf(s1[0]);
                int y = Integer.valueOf(s1[1]);
                int tx = x + a[i][0];
                int ty = y + a[i][1];dg
                //if (tx != x || ty != y)
                    if (tx >= 0 && tx <= COUNT - 1 && ty >= 0 && ty <= COUNT - 1)
                    {
                        System.out.println(tx + "  " + ty);
                        if (jButtons[tx][ty].getBackground() == Color.white)//如果是白色就变成黑色
                            jButtons[tx][ty].setBackground(Color.black);
                        else if (jButtons[tx][ty].getBackground() == Color.black)
                            jButtons[tx][ty].setBackground(Color.white);
                    }
            }
            if (isWin())
            {
    //            JPopupMenu.
    //             
    //             Calendar c1=Calendar.getInstance();
    //             int s=c1.get(Calendar.MINUTE)*60+c1.get(Calendar.SECOND);
    //             while(true)
    //             {
    //                 c1=Calendar.getInstance();
    //                 int s1=c1.get(Calendar.MINUTE)*60+c1.get(Calendar.SECOND);
    //                 if(s1-s>=10)break;
    //                 
    //             }
     
                      this.dispose();
                COUNT++;//难度增加
                x=this.getLocation().x;
                y=this.getLocation().y;
                new ChuangKou(COUNT, x,  y);
     
                return;
            }
        }
     
    }
     
    public class KaiChaungHu
    {
        public static void main(String[] args)
        {
     
                    ChuangKou c1 = new ChuangKou(2,0 ,0);
     
     
            //while(true)
            //{
            ///    c1.start();
            //}
        }
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    行为模式
    行为模式
    行为模式
    行为模式
    行为模式
    结构模式
    kafka 学习整理
    Hive文件格式,以及ORC创建使用
    GBDT 介绍
    机器学习中的特征工程 —— 七月在线总结
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710754.html
Copyright © 2020-2023  润新知