gpt4 book ai didi

java - 随机和图形代码太长

转载 作者:行者123 更新时间:2023-12-01 18:20:08 26 4
gpt4 key购买 nike

我想创建一个随机数,然后创建9个矩形,但其中一个将是不同的颜色,不同颜色的将根据随机数出来,所以我已经做到了,但是代码太长了,怎样才能缩短呢?

public void paint(Graphics g) {
rand = new Random();
n = rand.nextInt(9) + 1;
if (n == 1) {
g.setColor(Color.LIGHT_GRAY);
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
g.setColor(Color.DARK_GRAY);
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 2) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
g.setColor(Color.DARK_GRAY);
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 3) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
g.setColor(Color.DARK_GRAY);
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 4) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
g.setColor(Color.DARK_GRAY);
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 5) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
g.setColor(Color.DARK_GRAY);
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 6) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
g.setColor(Color.DARK_GRAY);
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 7) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
g.setColor(Color.DARK_GRAY);
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else if (n == 8) {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
g.setColor(Color.DARK_GRAY);
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
} else {
rec1 = new Rectangle(10, 130, 50, 50);
g.fillRect((int) rec1.getX(), (int) rec1.getY(),
(int) rec1.getWidth(), (int) rec1.getHeight());
rec2 = new Rectangle(70, 130, 50, 50);
g.fillRect((int) rec2.getX(), (int) rec2.getY(),
(int) rec2.getWidth(), (int) rec2.getHeight());
rec3 = new Rectangle(130, 130, 50, 50);
g.fillRect((int) rec3.getX(), (int) rec3.getY(),
(int) rec3.getWidth(), (int) rec3.getHeight());
rec4 = new Rectangle(10, 70, 50, 50);
g.fillRect((int) rec4.getX(), (int) rec4.getY(),
(int) rec4.getWidth(), (int) rec4.getHeight());
rec5 = new Rectangle(70, 70, 50, 50);
g.fillRect((int) rec5.getX(), (int) rec5.getY(),
(int) rec5.getWidth(), (int) rec5.getHeight());
rec6 = new Rectangle(130, 70, 50, 50);
g.fillRect((int) rec6.getX(), (int) rec6.getY(),
(int) rec6.getWidth(), (int) rec6.getHeight());
rec7 = new Rectangle(10, 10, 50, 50);
g.fillRect((int) rec7.getX(), (int) rec7.getY(),
(int) rec7.getWidth(), (int) rec7.getHeight());
rec8 = new Rectangle(70, 10, 50, 50);
g.fillRect((int) rec8.getX(), (int) rec8.getY(),
(int) rec8.getWidth(), (int) rec8.getHeight());
g.setColor(Color.LIGHT_GRAY);
rec9 = new Rectangle(130, 10, 50, 50);
g.fillRect((int) rec9.getX(), (int) rec9.getY(),
(int) rec9.getWidth(), (int) rec9.getHeight());
}

}

新代码::

private static void drawRectangle(Graphics g, int column, int row,
Color color) {
g.setColor(color);
g.fillRect(column * 60 + 10, row * 60 + 10, 50, 50);
n = rand.nextInt(9);
for (row = 0; row < 3; row++) {
for (column = 0; column < 3; column++) {
drawRectangle(g, column, row,
n == row * 3 + column ? Color.LIGHT_GRAY
: Color.DARK_GRAY);
}
}
}

最佳答案

我将从编写一个辅助方法开始:

private static void drawRectangle(Graphics g, int column, int row, Color color) {
// TODO: Remember the previous color of g and reset it afterwards?
g.setColor(color);
g.fillRect(column * 60 + 10, row * 60 + 10, 50, 50);
}

然后在需要绘制矩形的代码中(例如您问题中的代码所在的位置)使用嵌套循环来绘制所有 9 个矩形,根据 n 的值计算出每个矩形的颜色:

// It's simpler to use n being 0 to 8, if that's okay with the rest of your code.
n = rand.nextInt(9);
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
drawRectangle(g, column, row,
n == row * 3 + column ? Color.LIGHT_GRAY : Color.DARK_GRAY);
}
}

假设您实际上并不需要 Rectangle根本没有对象。如果这样做,您将更改 drawRectangle方法创建并返回 Rectangle ,然后要么有 Rectangle[] ,一个List<Rectangle>或者可能是Rectangle[][]取决于你想用它们做什么。我强烈建议您不要只使用 9 个单独的变量。

关于java - 随机和图形代码太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838183/

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