【问题标题】:Add delay of 1 second between each arc of a RainBow在 RainBow 的每个弧线之间添加 1 秒的延迟
【发布时间】:2017-03-26 19:46:57
【问题描述】:

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

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

                }
        }

        }

}





    }

【问题讨论】:

    标签: java multithreading swing animation


    【解决方案1】:

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

    绘画方法仅用于绘画。您不应该导致线程延迟。这将防止 GUI 重新绘制自身,直到整个循环执行完毕。

    相反,您可以使用Swing Timer 来安排重绘。

    与其在paintComponent() 中进行绘制,不如将其绘制到一个BufferedImage。然后您可以在 JLabel 上的 ImageIcon 中显示图像。

    因此,当生成 Timer 事件时,您可以绘制彩虹的颜色。绘制完所有 7 种颜色后,您将停止计时器。

    阅读 How to Use Swing Timers 上的 Swing 教程部分,了解更多信息和示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多