hi there,
i really need your help , i haven't got a clue how to draw a chessboard in
java. can u help me?
the question is:
Write a method whose heading is
static void drawBoard(Graphics g, int x,int y, int edge, int size, Color k1,
Color k2)
Which draws a chess board whose top left hand corner is at x,y.
The size of each liitle square is edge
The number of squares per row is size
and the colours of the squares are k1 and k2.
The whole board has a black border.
i think i have to use something like,: for(int i=1; i<8; i+=2 ) {
but i dont know how.
Please help, i would be sooo grateful
thanks
Chris Smith - 09 Dec 2004 18:23 GMT
> i really need your help , i haven't got a clue how to draw a chessboard in
> java. can u help me?
A little, perhaps. Are you familiar with the Java Standard API
Documentation? It's available online at
http://java.sun.com/j2se/1.5.0/docs/api/, and contains the information
you need to know about using the Graphics class. Graphics is in the
java.awt package.
The rest is just math. If you're confused, you might try posting more
about what you tried (even with source code), and what went wrong when
you tried it.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Fahd Shariff - 09 Dec 2004 22:38 GMT
A simple looping exercise. Note how I use the mod (%) operator to
switch colors alternately.
public void drawBoard(Graphics g, int x,int y, int edge, int size,
Color k1,
Color k2) {
for(int j = 0; j <size ; j++) {
for(int i = 0 ; i < size ; i++) {
g.setColor(i%2==(j%2==0?1:0)?k1:k2);
g.fillRect(x+i*edge, y+j*edge, edge, edge);
}
}
g.setColor(Color.black);
g.drawRect(x,y,edge*size,edge*size) ;
}
--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "