首页 新闻 搜索 专区 学院

多线程浮现圆的 我的圆点只出现一个

0
悬赏园豆:50 [待解决问题]
package test3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

public class mycir extends Frame implements ActionListener,Runnable{
    Graphics g;
    Thread circle=null; //声明一个线程对象
    public void init(){

        circle = new Thread(this);

        g = getGraphics();

        circle.start(); // 启动线程

    }
    public void setG(Graphics g) {
        this.g = g;

    }
    public mycir()
    {
        super("画图");
        setSize(800,600);
        setLocationRelativeTo(null);//画布居中显示
        setLayout(new FlowLayout());
        Button btn = new Button("清空");
        add(btn);
        btn.addActionListener(this);
        setVisible(true);
    }
    public void paint(Graphics g)//画图对象
    {
        super.paint(g);
        Random rand =new Random();
        int x0=(int)(Math.random()*800);
        int y0=(int)(Math.random()*600);//圆心
        int r=rand.nextInt(5)+25;
        g.drawOval(x0,y0,r,r);
        g.fillOval(x0,y0,r,r); //填充圆
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("清空"))
        {
            dispose();
            mycir  D = new  mycir();
            D.setBackground(Color.white);

        }
    }
    public void run() {
        while(true) {
            try {

                Thread.sleep(100);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            paint(g);
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Frame f = new mycir();
        mycir mt=new mycir();
        Thread thr=new Thread(mt);
        thr.start();

    }
    class closeWin extends WindowAdapter {
        public void windowClosing(WindowEvent e)
        {
            Window w=e.getWindow();
            w.dispose();
        }
    }
}

我的清空也显示不出来,repaint不太会用,选择新建一个窗口重绘,但并没有关闭之前的窗口

花火已不在线的主页 花火已不在线 | 初学一级 | 园豆:152
提问于:2022-05-05 16:17
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册