标题:关于repaint方法
只看楼主
不落夕阳
Rank: 2
等 级:论坛游民
帖 子:64
专家分:30
注 册:2016-10-13
结帖率:69.57%
已结贴  问题点数:2 回复次数:1 
关于repaint方法
import java.awt.Color;
import java.awt.Graphics;

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


public class TestRepaint extends JFrame implements Runnable{
    int x,y;
    Graphics g;
    public TestRepaint(){
        this.setTitle("画图");
        this.setSize(1000,1000);
        this.setVisible(true);
        
        JPanel  panel = new JPanel();
        
        this.add(panel);
    }
    @Override
    public void paint(Graphics g){
        g.setColor(Color.RED);
        g.fillOval((int)(Math.random()*1000), (int)(Math.random()*1000),20, 20);
    }
    public static void main(String[] args) {
        TestRepaint tr = new TestRepaint();
        new Thread(tr).start();
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        
        while(true){
            repaint();
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}如何做到:当生成一个实心圆的时候上一个院被清除。也就是说画布上只有一个随机圆在跳动
搜索更多相关主题的帖子: 方法 import public int this 
2018-07-12 17:41
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:2 
程序代码:
public class Test extends JFrame implements Runnable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int x,y;
    Graphics g;
    public Test(){
        setTitle("画图");
        setSize(1000,1000);
        setVisible(true);
        setDefaultCloseOperation(3);
    }
    @Override
    public void paint(Graphics g){
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 1000, 1000);
        g.setColor(Color.RED);
        g.fillOval((int)(Math.random()*1000), (int)(Math.random()*1000),20, 20);
    }
    public static void main(String[] args) {
        Test tr = new Test();
        new Thread(tr).start();
    }
    @Override
    public void run() {
        
        while(true){
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

剑栈风樯各苦辛,别时冰雪到时春
2018-07-14 15:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-488544-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.274438 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved