-2

I want to create an overlay of a 3x3 checkerboard, where the non-solid squares should be transparent.

I don't want to iterate the pixels, but rather just draw squares using Graphics2D to create a checkerboard. (Do I need a for loop, an if statement, or both?)
Here's my code so far:

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);
}
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
Sophie
  • 73
  • 4
  • 15
  • 2
    Is this homework? If so please tag appropriately. Also - what have you tried so far? Please post the code that you're having troubles with and make your question specific to that. – Deco Jan 23 '12 at 03:39
  • I told you I don't know where to begin. Divided the height and width by 3 that's about as far as I got. – Sophie Jan 23 '12 at 03:41
  • 2
    *"I don't know where to begin"* See the excellent article [So, You Need to Write a Program but Don't Know How to Start](http://home.earthlink.net/~patricia_shanahan/beginner.html). It contains many good tips that I often review when stuck on a problem. – Andrew Thompson Jan 23 '12 at 03:41
  • It's homework practice, we learnt how to create shapes with Graphics2D, but I don't know how to apply it to a loop – Sophie Jan 23 '12 at 03:44
  • New to programming and this site. Just looking for some help on how to make this work....... – Sophie Jan 23 '12 at 03:58

1 Answers1

3

I'd use a combined (double) for loop/if statement for drawing the solid parts of the checkerboard. In pseudo-code it might be expressed as:

draw image
for each row {
    for each column {
      if 'odd' square number {
        graphics fill rectangle
      }
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433