gpt4 book ai didi

java - 更改基本 Java 动画中的颜色

转载 作者:行者123 更新时间:2023-11-30 05:21:59 25 4
gpt4 key购买 nike

我正在用 Java 处理图形(特别是通过 Swing 和 AWT 库),并且想要在屏幕上移动一个圆圈(制作线条)并设置 x当线条触及 Canvas 边界时,y 坐标回到 0。我对这部分代码没有任何问题。

但是,现在我尝试在坐标均为 0 时清除屏幕(使用 super.paint(Graphics))并更改颜色。屏幕清除工作正常,但我无法更改颜色。看来,当坐标均为 0 时,指定颜色的椭圆形会出现在 (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);
}
}
}

最佳答案

注意更改和注释:

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);
}

关于java - 更改基本 Java 动画中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com