两个ScheduledExecutorService会互相覆盖?

时间:2014-10-26 00:25:40

标签: java swing graphics executor scheduledexecutorservice

我正试图制作两个以不同速率闪烁的闪烁圆圈。我在Circle类中使用ScheduledExecutorService来调节闪烁,其持续时间由每个Circle中的ms(毫秒)变量设置。

当我单独制作一辆汽车时,它们以正确的速率闪烁(我将黑色设置为1000毫秒,红色设置为10毫秒)。但是,当我创建它们并将它们添加到我的JLayeredPane时,它们都会在较短的时间内闪烁。

我对ScheduledExecutorService的使用不太熟悉,所以如果有人可以帮助我解决出错的问题,我们将不胜感激!

import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

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

public class blinker extends JFrame
{
    JLayeredPane lp = new JLayeredPane();
    public carlight()
    {
        lp.setLayout(new BorderLayout());
        lp.setPreferredSize(new Dimension(450, 450));

        car c1 = new car(new Color(0, 0, 0), "1", 10, 0, 0);
        c1.setOpaque(false);
        car c2 = new car(new Color(255, 0, 0), "2", 1000, 100, 100);
        c2.setOpaque(false);
        c1.setBounds(0, 0, 450, 450);
        c2.setBounds(0, 0, 450, 450);

        lp.add(c2);
        lp.add(c1);

        add(lp);

        setTitle("Carlights");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        carlight cl = new carlight();
    }
}

class Circle extends JPanel
{
    private Color color;
    private String name;
    private long ms;
    private int x, y;
    private boolean on = true;
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);
        if(on)
        {
            g.setColor(color);
            int r = 50;
            g.fillOval(x, y, r, r);
            on = false;
        }
        else
        {
            on = true;
        }
    }

    public car(Color c, String s, long l, int x, int y)
    {
        color = c;
        name = s;
        ms = l;
        this.x = x;
        this.y = y;

        this.service.scheduleAtFixedRate(new Runnable()
        {
            public void run()
            {
                repaint();
            }
        }, 0, ms, TimeUnit.MILLISECONDS);
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题是您在paintComponent方法中有程序逻辑,您可以在其中更改布尔变量的状态。您无法完全控制何时或甚至是否会调用此方法,实际上两者将在调用重绘时调用paintComponents,这就是为什么您的闪烁灯不工作的原因。解决方案:通过在其他位置更改布尔字段的状态来获取paintComponent方法的逻辑。此外,您还需要使用Swing Timer来获得更好的Swing线程。

您还需要修复布局的使用,包括避免使用setBounds。这在您的设置中特别危险且不可预测,与BorderLayout一起使用它。我自己,我不会让Circle类扩展JPanel而是使它成为逻辑类,而不是组件类,然后我会有绘图组件,一个扩展JPanel的类,保存Circle类的实例然后在paintComponent中绘制它们。例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class BlinkerEg extends JPanel {
   private static final int PREF_W = 450;
   private static final int PREF_H = PREF_W;
   private List<Circle> circles = new ArrayList<>();

   public BlinkerEg() {
      circles.add(new Circle(Color.red, 1000, 0, 0, 450, this));
      circles.add(new Circle(Color.black, 60, 0, 0, 450, this));
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      for (Circle circle : circles) {
         circle.paint(g2);
      }
   }

   private static void createAndShowGui() {
      BlinkerEg mainPanel = new BlinkerEg();

      JFrame frame = new JFrame("BlinkerEg");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Circle {
   private Color color;
   private int x, y;
   private int diam;
   private JComponent component;
   private boolean on = true;

   public Circle(Color color, int ms, int x, int y, int diam, JComponent component) {
      this.color = color;
      this.x = x;
      this.y = y;
      this.diam = diam;
      this.component = component;

      new Timer(ms, new TimerListener()).start();
   }

   public void paint(Graphics g) {
      if (on) {
         g.setColor(color);
         g.fillOval(x, y, diam, diam);
      }
   }

   public boolean isOn() {
      return on;
   }

   public void setOn(boolean on) {
      this.on = on;
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         setOn(!isOn());
         component.repaint();
      }
   }
}