java day2一个模拟双色球的代码

时间:2021-11-07 17:00:43
package day2;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.util.Random;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

//继承
public class day2_c extends JPanel{
    /**
             *
             */
        private static final long serialVersionUID = 1L;

        public int[] Rednumber = {0,0,0,0,0,0,0};
        public int[] checknum = new int[7];
        Random mrRandom = new Random();
        boolean Ischange = false;
        boolean state = false;
        public int counterrun = -1;

        public static void main (String args[])
        {
            JFrame myFrame = new JFrame("NEF");
            myFrame.setSize(1200, 650);
            myFrame.setDefaultCloseOperation(2);
            myFrame.setLocationRelativeTo(null);
            myFrame.setResizable(false);

            day2_c mypanel = new day2_c();
            myFrame.add(mypanel); 

            myFrame.setVisible(true);
            mypanel.changeN();
        }

        public void paint(Graphics myg)
        {
            //画点击口
            myg.setColor(Color.orange);
            myg.fillRect(550, 50, 100, 50);
            myg.setFont(new Font("楷体", 1,25));
            myg.setColor(Color.BLACK);
            myg.drawString("点击开始", 547, 80);

            //画红球
            int x_axis = 100;
            int y_axis = 250;
            myg.setColor(Color.red);
            for(int i = 0; i < 6; i++)
            {
                myg.fillOval(x_axis, y_axis, 100, 100);
                x_axis += 150;
            }
            myg.setColor(Color.blue);
            myg.fillOval(1000, 250, 100, 100);

            //是否change
            if(Ischange)
            {
                //画数字
                int T_axisX = 125;
                int T_axisY = 320;
                int nud = 0;
                for(int i = counterrun; i < 6; i++)
                {
                    nud = mrRandom.nextInt(33) + 1;
                    for(int h = 0; h < i; h++)
                    {
                        if(nud == Rednumber[i])
                        {
                            i--;
                            break;
                        }
                    }
                    Rednumber[i] = nud;
                }
                Rednumber[6] = mrRandom.nextInt(16) + 1;
                myg.setFont(new Font("", 1, 45));
                myg.setColor(Color.GREEN);

                for(int i = 0;i < 7;i++)
                {
                    if(Rednumber[i] % 10 == 0)
                    {
                        myg.drawString(" " + Rednumber[i],T_axisX , T_axisY);
                    }
                    else{
                        myg.drawString("" + Rednumber[i],T_axisX , T_axisY);
                    }
                    T_axisX += 150;
                }
                //myg.drawString(""+Rednumber[6], T_axisX, T_axisY);
            }
        }

        public void changeN()
        {
            MouseAdapter MA = new MouseAdapter() {
            public void mouseClicked(MouseEvent e)
            {
                int x = e.getX();
                int y = e.getY();

                if(x > 550 & x < 650 & y > 50 & y < 100 )
                {
                    Ischange = true;
                    //repaint();
                }
            }
            };
            addMouseListener(MA);        //鼠标时间的监听实际上是和main函数main线程分开的
            //监听是简单事件,无法执行循环
                for(int i = 0; i < 140; i++)
                {
                    try{
                        Thread.sleep(50);
                    }
                    catch(Exception e3){
                        e3.printStackTrace();
                    }
                    if(i % 20 == 0)
                    {
                        counterrun ++;
                    }
                    repaint();
                }
                counterrun = 0;
        }
}

程序有bug,目前还没有找到好的解决办法,有兴趣的可以留言,我发老师的代码,实现的过程不太一样
本来还有一个小功能是排序的,但是太简单了,就没写,,,,一直和bug斗争,就一冒泡排序。

有人愿意帮我解决一下这程序的bug不。。。