-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess board draw basic
64 lines (38 loc) · 1.66 KB
/
chess board draw basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*Draw chessboard using 2 for loop basic
*/
for ( row = 0; row < 8; row++ ) { / row creation ----
for ( col = 0; col < 8; col++ ) { / column creation ----
x = 20*col;
y = 20*row;
if ( (row % 2) == (col % 2) )
g.setColor(Color.white);
else
g.setColor(Color.black);
g.fillRect(x,y,20,20);
}
}
------------Full Method--------
import java.awt.*;
import java.applet.*;
public class Checkerboard extends Applet {
/* This applet draws a white-and-black checkerboard.
It is assumed that the size of the applet is 40
by 40 pixels.
*/
public void paint(Graphics g) {
int row; // Row number, from 0 to 7
int col; // Column number, from 0 to 7
int x,y; // Top-left corner of square
for ( row = 0; row < 8; row++ ) {
for ( col = 0; col < 8; col++) {
x = col * 20;
y = row * 20;
if ( (row % 2) == (col % 2) )
g.setColor(Color.red);
else
g.setColor(Color.black);
g.fillRect(x, y, 20, 20);
}
} // end for row
} // end paint()
} // end class