I have a buffered image of a 4x4 checker Board (resolution 400x400) rendered on half a JPanel. Is it possible to find the coordinates of each square corner without doing it manually? I'm using absolute positioning on the JPanel and it is the only container besides the Frame
Asked
Active
Viewed 88 times
1 Answers
1
If you have a 4 x 4 checkerboard that's 400 x 400, then each square is 100 x 100 pixels.
When you construct a BufferedImage
like this, you save a Rectangle
for each square as you're doing the construction.
That way, when you do a mouse click later, you can use the contains
method of Rectangle
to determine which square was clicked.
You shouldn't use absolute positioning. If your checker board takes up half the JPanel, FlowLayout
or BoxLayout
works well.

Gilbert Le Blanc
- 50,182
- 6
- 67
- 111
-
Well, they are not perfect squares(forgot to mention) and the squares are not independent but altogether in an image. Is there an alternative way? – Anon855 Jun 26 '13 at 12:53
-
@Anon855: Sure. Put the image in Microsoft Paint or some other paint program and count the pixels. Create a Rectangle for each square. Your other alternative is to write a class that draws a checker board by constructing a BufferedImage. – Gilbert Le Blanc Jun 26 '13 at 12:57
-
1See [this answer](http://stackoverflow.com/a/10862262/418556) for an example that is ..something vaguely like what @GilbertLeBlanc is suggesting. – Andrew Thompson Jun 26 '13 at 17:58