在RainBow的每个弧之间增加1秒的延迟 [英] Add delay of 1 second between each arc of a RainBow

查看:98
本文介绍了在RainBow的每个弧之间增加1秒的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用Thread.delay()在Rainbow的每个弧的形成之间添加延迟,以使其看起来像动画.当我使用Thread.delay()时,它会延迟整个过程.还有其他方法吗,或者我做错了.请帮助我解决问题

Hi I am here add delay BETWEEN the formation of each arc of a Rainbow using Thread.delay() so that it look like an animation. When I am using Thread.delay() it delays the whole process. Is there any other method or I am doing it wrong. Please help me solve the problem

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;


    public class RainBow2{
        static int x,y,z;
        static RainBowPanel rainBow = new RainBowPanel();
        static StdRainBow stdRainBow = new StdRainBow();
        static JTextField xCo = new JTextField(4);
        static JTextField yCo = new JTextField(4);
        static JTextField angle = new JTextField(4);
        static JFrame frame;

        public static void main(String[] args){
            Color color = new Color(135,206,250);
            frame = new JFrame("Rainbow");
            JPanel panel = new JPanel();
            JButton draw = new JButton("Draw RainBow");
            draw.addActionListener(new ListenButton());
            JLabel lab1 = new JLabel("X-Coordinate");
            JLabel spaceLab = new JLabel("    ");
            JLabel lab2 = new JLabel("Y-Coordinate");
            JLabel angleLab = new JLabel("Initial Angle");
            JButton chButton = new JButton("Change Color");
            chButton.addActionListener(new ListenButton());
            panel.setBackground(color);
            panel.add(angleLab);
            panel.add(angle);
            panel.add(lab1);
            panel.add(xCo);
            panel.add(spaceLab);
            panel.add(lab2);
            panel.add(yCo);
            panel.add(draw);
            panel.add(spaceLab);
            panel.add(chButton);
            frame.getContentPane().add(BorderLayout.SOUTH,panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1366,740);
            frame.setVisible(true);
            rainBow.addMouseListener(new RainBowList());
            frame.getContentPane().add(rainBow);


        }

        static class RainBowList extends MouseAdapter implements MouseMotionListener{
            public void mouseClicked(MouseEvent e){
                x = e.getX();
                y = e.getY();
                rainBow.drawing(x, y, 0);

            }

        }

        static class ListenButton implements ActionListener{
            public void actionPerformed(ActionEvent a){
                if(a.getActionCommand().equals("Draw RainBow")){
                    x = Integer.parseInt(xCo.getText());
                    y = Integer.parseInt(yCo.getText());
                    z = Integer.parseInt(angle.getText());
                    rainBow.drawing(x, y,z);
                }
                if(a.getActionCommand().equals("Change Color")){
                    rainBow.drawing(x, y,z);
                }
                            }

        }




import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class RainBowPanel extends JPanel{

    int x,y,z;

    public void drawing(int xx, int yy, int zz){
        Color color = new Color(135,206,250);
        setBackground(color);
        z = zz;
        x = xx;
        y = yy;
        repaint();
    }


    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int length = 300;
        int width = 300;
        x = x-length/2;
        y = y-width/2;

        for(int i =0 ; i< 7;i++){
            Color color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
            g.setColor(color);
            g.fillArc(x,y,length ,width ,z,180 );

            x=x+15;
            y=y+15;
            length = (length-30);
            width = (width-30);
            try{
                Thread.sleep(200);
                }catch(Exception e){

                }
        }

        }

}





    }

推荐答案

当我使用Thread.delay()时,它会延迟整个过程.

When I am using Thread.delay() it delays the whole process.

一种绘画方法仅用于绘画.您不应该导致线程延迟.这样可以防止GUI重新绘制自身,直到完成整个循环为止.

A painting method is for painting only. You should not be causing the Thread to delay. This will prevent the GUI from repainting itself until the entire loop is finished executing.

相反,您可以使用Swing Timer安排重新粉刷.

Instead you can use a Swing Timer to schedule the repainting.

与其在paintComponent()中进行绘画,不如将其绘画为BufferedImage.然后,您可以在JLabel上的ImageIcon中显示该图像.

Instead of doing the painting in the paintComponent() you should maybe paint to a BufferedImage. Then you can display the Image in an ImageIcon on a JLabel.

因此,在生成Timer事件时,您将绘制彩虹色.将所有7种颜色都涂完后,您就可以停止计时器了.

So when the Timer event is generated you paint a color of the rainbow. After all 7 colors are painted you stop the Timer.

如何使用Swing计时器了解更多信息和示例.

Read the section from the Swing tutorial on How to Use Swing Timers for more information and examples.

这篇关于在RainBow的每个弧之间增加1秒的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆