【问题标题】:Changing the color in basic Java animation更改基本 Java 动画中的颜色
【发布时间】:2020-04-12 12:38:31
【问题描述】:

我正在处理 Java 中的图形(特别是通过 Swing 和 AWT 库),并且想要在屏幕上移动一个圆圈(制作线条)并将 xy 坐标设置回 0 时这条线到达画布的边界。我对这部分代码没有问题。

但是,现在我尝试在坐标均为 0 时清除屏幕(使用 super.paint(Graphics))并更改颜色。屏幕清除工作正常,但我无法更改颜色。似乎,当坐标都为 0 时,指定颜色的椭圆会出现在 under 在 (0, 0) 位置的条纹。但是,我希望条纹的颜色能够改变颜色,直到坐标重置为 (0, 0)。

这是我的代码:

@SuppressWarnings("serial")
public class Game extends JPanel
{
    static final int SCREEN_X = 300;
    static final int SCREEN_Y = 400;
    int x = 0;
    int y = 0;

    private void moveBall()
    {
        x += 1;
        y += 1;

        if (x >= SCREEN_X) x = 0;
        if (y >= SCREEN_Y) y = 0;
    }

    @Override
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (x == 0 && y == 0)
        {
            super.paint(g);

            // Generate random value in [0, 5)
            int rand_val = (new Random()).nextInt(5);

            // Init random color variable
            Color rand_color = Color.black;

            // Pick a random color
            switch (rand_val)
            {
            case 0:
                rand_color = Color.black;
                break;

            case 1:
                rand_color = Color.red;
                break;

            case 2:
                rand_color = Color.green;
                break;

            case 3:
                rand_color = Color.blue;
                break;

            case 4:
                rand_color = Color.gray;
                break;
            }

            // I'm assuming I need to do something more here?
            // This is likely where my mistake is...
            g2d.setColor(rand_color);
        }

        g2d.fillOval(x, y, 30, 30);
    }

    public static void main(String[] args) throws InterruptedException
    {
        JFrame frame = new JFrame("Mini Tennis");
        Game game = new Game();

        frame.add(game);
        frame.setSize(SCREEN_X, SCREEN_Y);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true)
        {
            game.moveBall();
            game.repaint();
            Thread.sleep(10);
        }
    }
}

【问题讨论】:

  • 上面的例子是关于动画(1)和自定义绘画的错误。我建议在专注于其他任何事情之前将其修改为久经考验的方法。 1) 动画应该从 Swing Timer 调用,这样 EDT 就不会被阻塞。 2) 在所有案例中使用自定义绘画,一个常量应该是对super 方法的调用。没有ifs 或对此表示反对。
  • @AndrewThompson 代码来自一个游戏教程,所以代码不是专门针对动画的。它开始设置游戏。在继续下一步之前,我只是在玩弄它。
  • “代码来自游戏教程..” GI/GO “在继续下一步之前我只是在玩弄它。” 你的下一步应该是扔掉这些渣滓,并从 Java 教程中获得的代码重新开始。
  • 绘画方法不应该改变组件的状态。它应该只绘制组件的当前状态。请参阅:stackoverflow.com/questions/54028090/…,了解使用 Swing Timer 为多个“球”对象设置动画的工作示例。

标签: java swing loops animation awt


【解决方案1】:

注意变化和cmets:

public class Game extends JPanel
{
    static final int SCREEN_X = 300;
    static final int SCREEN_Y = 400;
    int x = 0;
    int y = 0;

    private void moveBall()
    {
        x += 1;
        y += 1;

        if (x >= SCREEN_X) {
            x = 0;
        }
        if (y >= SCREEN_Y) {
            y = 0;
        }
    }

    @Override
    public void paintComponent(Graphics g) { //for custom painting override paint componenet

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (x == 0 && y == 0)
        {
            // Generate random value in [0, 5)
            int rand_val = new Random().nextInt(5);

            // Init random color variable
            Color rand_color = Color.black;

            // Pick a random color
            switch (rand_val)
            {
            case 0:
                rand_color = Color.black;
                break;

            case 1:
                rand_color = Color.red;
                break;

            case 2:
                rand_color = Color.green;
                break;

            case 3:
                rand_color = Color.blue;
                break;

            case 4:
                rand_color = Color.gray;
                break;
            }

            g2d.setColor(rand_color);
            g2d.fillRect(0, 0, getWidth(), getHeight()); //clear screen 
        }

        g2d.fillOval(x, y, 30, 30);
    }


旁注:您可以定义一个颜色数组:
private final Color[] colors = {Color.black, Color.red, Color.green, Color.blue, Color.gray};

为了让代码更简洁:

@Override
public void paintComponent(Graphics g) { //for custom painting override paint componenet

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    if (x == 0 && y == 0)
    {
        int rand_val = new Random().nextInt(colors.length);
        Color rand_color = colors[rand_val];
        g2d.setColor(rand_color);
        g2d.fillRect(0, 0, getWidth(), getHeight()); //clear screen
    }

    g2d.fillOval(x, y, 30, 30);
}

【讨论】:

    【解决方案2】:
    // I'm assuming I need to do something more here?
    // This is likely where my mistake is...
    g2d.setColor(rand_color);
    

    用于此绘制的Graphics2D 实例即将被丢弃。现在换颜色不好。

    改为更改组件(JPanel)本身的 BG 或 FG 颜色,然后在每次绘制之前设置图形对象的颜色。

    【讨论】:

      猜你喜欢
      • 2013-09-10
      • 2015-12-25
      • 1970-01-01
      • 2017-09-04
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2013-05-22
      相关资源
      最近更新 更多