仿真实现为什么疫情期间不能出门。计算机仿真初始人群5000人位置, 状态,初始假定有400感染,并进行显示,然后分别模拟10人出门

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test9_5 extends JFrame implements ActionListener
{
    protected int amount=10;
    private final panel p = new panel(amount);

    Test9_5()//构造方法,设置一下这个窗口,插入按钮,插入面板
    {
        super("为什么疫情期间不能出门");
        setLayout(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        setBounds(200,200,800,900);

        Button b_start = new Button("黑夜降临了");
        b_start.setBounds(50,20,70,20);
        add(b_start);
        b_start.addActionListener(this);

        //    仿真实现为什么疫情期间不能出门。计算机仿真初始人群5000人位置, 状态,初始假定有400感染,并进行显示,然后分别模拟10人出门,
        //    模拟100出门的情况下的人群感染情况,假定当两人距离小于30时感染概率为40%,当两人距离小于20时感染的概率为60%。
        add(p);

        p.setBounds(0,100,800,800);
        p.draw();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        ExecutorService exec= Executors.newCachedThreadPool();
        exec.execute(p);
        exec.shutdown();
    }

    public static void main(String[] args)
    {
        new Test9_5();
    }
}


class panel extends JPanel implements Runnable//懒得起名字了,这个类就叫panel吧
{
    Graphics g;
    private int amount;//这个amount是被感染人数
    private int x,y,a,b;
    panel(int amount)
    {

        this.amount=amount;
        setBackground(Color.WHITE);
    }

    public void draw()//先把最开始的画出来
    {
        g=getGraphics();
        for(int i=0;i<amount;i++)
        {
            x=(int)(Math.random()*800);
            y=(int)(Math.random()*800);
            g.fillOval(x,y,4,4);
            g.setColor(Color.red);
        }
        for(int i=0;i<5000-amount;i++)
        {
            a=(int)(Math.random()*800);
            b=(int)(Math.random()*800);
            g.fillOval(a,b,4,4);
            g.setColor(Color.black);
        }
    }
    @Override
    public void run()
    {
        int time=100;
        g=getGraphics();

        while(time<1000)
        {


            try
            {
            //防止图像覆盖
                update(g);
                //接下来模拟的是人群的流动
                for(int i=0;i<amount;i++)
                {
                    x=(int)(Math.random()*800);
                    y=(int)(Math.random()*800);
                    g.fillOval(x,y,4,4);
                    g.setColor(Color.red);
                }
                for(int i=0;i<5000-amount;i++)
                {
                    a=(int)(Math.random()*800);
                    b=(int)(Math.random()*800);
                    g.fillOval(a,b,4,4);
                    g.setColor(Color.black);
                }
                Thread.sleep(100);
                time+=100;
                //这个m是用来判断概率的,当距离小于20的时候,感染概率为60%
                int m=(int)(Math.random()*10);
                int flag = 0;
                //下面这两个for循环就是用来不断地将新被感染的人记录到amount里,然后通过上面的两个for把新增患者画出来
                for(int i=0;i<5000-amount;i++)
                {
                    a=(int)(Math.random()*800);
                    b=(int)(Math.random()*800);
                    for(int j=0;j<amount;j++)
                    {
                        x=(int)(Math.random()*800);
                        y=(int)(Math.random()*800);
                        if(((x-a)*(x-a)+(y-b)*(y-b))<=900&&((x-a)*(x-a)+(y-b)*(y-b))>400)
                        {
                            if(m<=3)//m取3是因为m的取值是0到9,所以百分之四十我就取m<=3
                            {
                                flag=1;
                            }
                        }
                        if(((x-a)*(x-a)+(y-b)*(y-b))<=400)
                        {
                            if(m<=5)
                            {
                                flag=1;
                            }
                        }
                        if(flag==1)
                        {
                            amount++;
                        }
                    }

                }

            }
            catch (Exception e) {
                e.printStackTrace();//这个东西不建议用,不加也没事的,替代品好像是logger,但是我不会用,没整明白,有路过的大哥会的能教教我吗
            }
        }

    }
}

遇到的问题

1.draw画出来的图没有红点,但是用的同样的方法,后续run里面画出的图就有红点
2.要点两下按钮,才能显示后续的图像,我不太明白为什么,和time无关,就是要点两下按钮,我最开始以为是线程池的问题,但是后面发现好像不是,实在是找不到原因

路过的大佬有知道的可以解答一下吗

结果

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值