我的多线程只能绘出来一个圆,是哪里错了吗,怎么才能让它一直浮现圆

osc_08711245 发布于 05/05 16:02
阅读 1K+
收藏 0

开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>

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();
        }
    }
}

加载中
1
tcxu
tcxu

将代码作以下修改,就能正常运行:

1.   为了保证用户按 Frame 右上角的按钮,结束整个程序运行, 特将 class closeWin extends WindowAdapter { ...} 删去,在主方法 main(String[] args)的 最后,添加一行代码

mt.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                    System.exit(0);     
            }
        });

在 方法 actionPerformed(ActionEvent e) 的代码中添加一行代码:

D.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                    System.exit(0);     
            }
        });

2.    在 mycir 的构造方法的最后,调用方法 init(), 以便获得 画笔 Graphics g

修改后的代码如下:

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);
        init(); //此方法在启动线程时,获得 画笔 Graphics g
    }
    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);
            D.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                    System.exit(0);     
            }
        });

        }
    }
    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();
        mt.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                    System.exit(0);     
            }
        });
        
// 删除  class closeWin
    }
}

 

1
f
freezingsky

你就是只能画一个圆。光paint,但是又不把已经paint的圆的信息保存一起来,再一起paint。这代码写得,就算照着课本抄也不至少漏吧。

o
osc_89909472
哈哈哈哈
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部
返回顶部
顶部