我想创建一个随机数,然后创建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);
}
}
}
答案 0 :(得分:4)
我首先要写一个辅助方法:
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个单独的变量。