使用Graphics2D在图片上创建3x3棋盘格

时间:2012-01-23 03:37:17

标签: java awt graphics2d

我想创建一个3x3棋盘的叠加层,其中非实心方块应该是透明的。

我不想迭代像素,而只是使用Graphics2D绘制正方形以创建棋盘格。 (我是否需要for循环,if语句或两者兼而有之?) 到目前为止,这是我的代码:

Picture myPict = new Picture(myPathName);
myPict.show();
Graphics2D graphicsObj = myPict.getGraphics2D();
final int WIDTH = myPict.getWidth() / 3;
final int HEIGHT = myPict.getHeight() / 3;
for (int i = 0; i > WIDTH; i = WIDTH * 2) {
    Rectangle2D.Double shape1 = new Rectangle2D.Double(WIDTH, HEIGHT, 0, 0);
    graphicsObj.draw(shape1);
}

1 个答案:

答案 0 :(得分:3)

我使用组合(双)for loop / if语句来绘制棋盘的实体部分。在伪代码中,它可以表示为:

draw image
for each row {
    for each column {
      if 'odd' square number {
        graphics fill rectangle
      }
  }
}