【问题标题】:Stop infinite loop paint() method? Add JButton with paint()?停止无限循环paint()方法?用paint()添加JButton?
【发布时间】:2012-11-24 05:24:47
【问题描述】:

我编写了程序来绘制轮子(有 6 段的圆圈)和不同颜色的每个段。并为轮子设置动画..

代码如下:

    public class ExamWheel extends JFrame implements ActionListener{
        JButton b_start = new JButton("Start");
        JButton b_stop = new JButton("Stop");
        Thread th;
        Boolean doDraw = true;

        public ExamWheel(){
            setSize(400,400);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setTitle("Wheel..");
            //add(b_start);
            this.setLayout (new FlowLayout());
            this.add(b_stop);
            b_start.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==b_start)
                doDraw=true;
        }

        public void paint(Graphics graphics) {
        if (doDraw){
            super.paint(graphics);
            Graphics2D g = (Graphics2D) graphics;
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            try{
            // draw the circle
            for(int i=0; ; i=i+1){
                g.setColor(Color.CYAN);
                g.fillArc(50, 50, 300, 300, i+0, 60);
                th.sleep(1);
                g.setColor(Color.red);
                g.fillArc(50, 50, 300, 300, i+60, 60);
                th.sleep(1);
                g.setColor(Color.green);
                g.fillArc(50, 50, 300, 300, i+120, 60);
                th.sleep(1);
                g.setColor(Color.blue);
                g.fillArc(50, 50, 300, 300, i+180, 60);
                th.sleep(1);
                g.setColor(Color.gray);
                g.fillArc(50, 50, 300, 300, i+240, 60);
                th.sleep(1);
                g.setColor(Color.pink);
                g.fillArc(50, 50, 300, 300, i+300, 60);
                th.sleep(1);
            }
            }
            catch(InterruptedException e){
                Thread.currentThread().interrupt();
            }
        }
        }

        public static void main(String[] args) {
            ExamWheel f = new ExamWheel();
        }

    }

问题:它是无限循环,我无法停止它或关闭框架。

所以我想到了:

我创建了值为 true 的布尔变量 (doDraw),并添加了 JButton,当单击 Button 时该变量将变为 false,并且在 paint() 方法中,我将首先使用 paint() 的 if 条件

问题:我无法使用paint() 将JButton 添加到Frame,我该怎么办?

注意:我尝试使用paintComponent(),但循环(for with thread)不起作用。

解决了 感谢皮特·科勒姆

我添加了 Timer 并用paintComponent() 替换了paint()

public class ExamWheel extends JPanel implements ActionListener {
    int i=0;
    Timer tm = new Timer(10, this);
    public void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g = (Graphics2D) graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.CYAN);
        g.fillArc(50, 50, 300, 300, i+0, 60);
        g.setColor(Color.red);
        g.fillArc(50, 50, 300, 300, i+60, 60);
        g.setColor(Color.green);
        g.fillArc(50, 50, 300, 300, i+120, 60);
        g.setColor(Color.blue);
        g.fillArc(50, 50, 300, 300, i+180, 60);
        g.setColor(Color.gray);
        g.fillArc(50, 50, 300, 300, i+240, 60);
        g.setColor(Color.pink);
        g.fillArc(50, 50, 300, 300, i+300, 60);
        tm.start();
    }
    public void actionPerformed(ActionEvent e) {
        i++;
        repaint();
    }
    public static void main(String[] args) {
        ExamWheel wh = new ExamWheel();
        JFrame jf = new JFrame();
        jf.setSize(500,500);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setTitle("Wheel..");
        jf.add(wh);
    }

【问题讨论】:

  • 为什么你在paint() 中有无限循环,gui 会在某个时间间隔调用paint()。你永远不会给th 赋值。要休眠一个线程,您应该调用Thread.sleep();

标签: java paint infinite-loop


【解决方案1】:

您需要从 paintComponent 返回以允许运行 gui 的线程执行其他操作,例如响应按钮事件或实际将图形内容放到屏幕上。

使用计时器使组件无效 - 请参阅 http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html 并根据当前时间更新动画,例如 current milliseconds 模 5000 将给出一个每五秒重复一次的循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 2019-09-01
    • 2016-03-06
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多