如何在 Java 中使形状“闪烁”或改变颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7226071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:11:35  来源:igfitidea点击:

How to make shapes "blink" or change colors in Java?

javagraphicscolorsdrawing

提问by tussya

I'm trying to learn how to draw a shape, and be able to a) draw it, "freeze" the process, draw it in the color of the background, and then re-draw it in the original color and b) draw a shape and change its color. All I have so far is (for blinking):

我正在尝试学习如何绘制形状,并且能够 a) 绘制它,“冻结”该过程,以背景颜色绘制它,然后以原始颜色重新绘制它,然后 b) 绘制一个形状并改变它的颜色。到目前为止,我所拥有的只是(眨眼):

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

public class Carlight extends JPanel{
    Thread th=new Thread();
    public void paintComponent (Graphics g){
        super.paintComponents(g);
        g.setColor(Color.yellow);
        g.fillOval(25, 25, 10, 10);
        try{
            th.sleep(10);
        }catch(InterruptedException e){}
        repaint();
        g.setColor(Color.yellow);
        g.fillOval(25, 25, 10, 10);
        try{
            th.sleep(10);
        }catch(InterruptedException e){}
        repaint();
        g.setColor(Color.yellow);
        g.fillOval(25, 25, 10, 10);
    }
    public Carlight(){
        JFrame frame=new JFrame();
        frame.setTitle("Carlights");
        frame.add(this);
        frame.setBackground(Color.black);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(100,150);
        frame.setVisible(true);
    }
    public static void main(String[] args){
        new Carlight();
    }
}

How do I make this code work and how do I get a shape to change color?

如何使此代码工作以及如何获得改变颜色的形状?

回答by pimaster

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

import javax.swing.JFrame;
import javax.swing.JPanel;

public class carlight extends JPanel
{
    private Color lastColor = Color.YELLOW;
    // For telling the panel to be repainted at regular intervals
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);
        if(lastColor.equals(Color.YELLOW))
        {
            lastColor = Color.GREEN;
        }
        else
        {
            lastColor = Color.YELLOW;
        }
        g.setColor(lastColor);
        g.fillOval(25, 25, 10, 10);
    }

    public carlight()
    {
        JFrame frame = new JFrame();
        frame.setTitle("Carlights");
        frame.add(this);
        frame.setBackground(Color.black);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(100, 150);
        frame.setVisible(true);
        service.scheduleAtFixedRate(new Runnable()
        {
            public void run()
            {
                repaint();
            }
        }, 0, 1, TimeUnit.SECONDS);
    }

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

Ok,

行,

  • Don't put sleep calls during a paintComponent call. It means you are forcing the UI to hang/stall. This is very bad.
  • I have created a ScheduledExecutorService for regularly calling the repaint method.
  • The paint method changes the color of the lastColor. Most of the time you would be looking at some sort of model to find out of the state of it to choose which color you should be using.
  • If the problem is you aren't meant to change the color, but instead it is an off/on situation, you should have a boolean representing state and only draw the circle if it is on.
  • 不要在paintComponent 调用期间调用sleep。这意味着您正在强制 UI 挂起/停止。这真是太糟了。
  • 我创建了一个 ScheduledExecutorService 来定期调用重绘方法。
  • Paint 方法更改 lastColor 的颜色。大多数情况下,您会查看某种模型以了解其状态以选择应使用的颜色。
  • 如果问题是您不打算更改颜色,而是关闭/打开情况,则您应该有一个布尔值表示状态,并且只有在打开时才绘制圆圈。