有更好的算法吗?
// please use jbuilder to run it.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Chess extends JPanel {
static final int SIZE=19;
public Chess(){
super();
setBackground(Color.pink);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().setLayout(new XYLayout());
f.getContentPane().add(new Chess(),new XYConstraints(25, 25, 350, 350));
f.setSize(400,400);
f.pack();
f.setVisible(true);
}
public void drawAxis(Graphics g) {
int W = getWidth(); int H = getHeight();
g.setColor(Color.gray);
for(int i = 0; i <= SIZE+1; i++){
int h = (int)(i*H/(SIZE+1));
int w = (int)(i*W/(SIZE+1));
if( i%2 == 1 ){
g.fillRect((int)(W/(SIZE+1)),h,W-(int)(W/(SIZE+1)),(int)(H/(SIZE+1)));
}
}
boolean up = true;
int w0 = (int)(W/(SIZE+1));
g.setColor(Color.lightGray);
for(int i = 0; i <= SIZE+1; i++){
int w = (int)(i*W/(SIZE+1));
if( i%2 == 1 ){
up = (i/2) % 2 == 0;
int h = 1;
int l = up?2:3;
while(h < SIZE+1) {
if(up) {
g.fillRect(w,(int)(h*H/(SIZE+1)),
w0,(int)(l*H/(SIZE+1)));
h +=l;
l = 3;
}else
h ++;
up = !up;
}
}
}
}
public void paint(Graphics g){
super.paint(g);
drawAxis(g);
}
}