Add delay of 1 second between each arc of a RainBo

2019-09-19 08:15发布

问题:

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){

                }
        }

        }

}





    }

回答1:

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

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.

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

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.

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

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