大概的意思,主要是(i*j)%8==0&&!(i%4==0&&j%4==0)&&!(i==0||j==0)&&(i%2==0&&j%2==0)这个条件,没想好怎样确定,现在这个程序边上画得不对,而且条件太复杂了也不好看。代码倒是比较清楚。 public void drawAxis(Graphics g) { int W = getWidth(); int H = getHeight(); g.setColor(Color.gray); for(int i = 0; i < SIZE; i++){ int h = (int)(i*H/SIZE); int w = (int)(i*W/SIZE); if( i%2 == 1 ){ g.fillRect((int)(W/SIZE),h,W-(int)(W/SIZE),(int)(H/SIZE)); } } g.setColor(Color.lightGray); for(int i = 0; i < SIZE; i++){ int h = (int)(i*H/SIZE); int w = (int)(i*W/SIZE); if( i%2 == 1 ){ g.fillRect(w,(int)(H/SIZE),(int)(H/SIZE),W-(int)(W/SIZE)); } } g.setColor(Color.gray); for(int i=0;i<SIZE;i++){ int w = (int)((i+1)*W/SIZE); for(int j=0;j<SIZE;j++){ int h = (int)((j+1)*H/SIZE); if((i*j)%8==0&&!(i%4==0&&j%4==0)&&!(i==0||j==0)&&(i%2==0&&j%2==0)) g.fillRect(w,h,(int)(W/SIZE),(int)(H/SIZE)); } } }
public void drawAxis(Graphics g) {
int W = getWidth(); int H = getHeight();
g.setColor(Color.gray);
for(int i = 0; i < SIZE; i++){
int h = (int)(i*H/SIZE);
int w = (int)(i*W/SIZE);
if( i%2 == 1 ){
g.fillRect((int)(W/SIZE),h,W-(int)(W/SIZE),(int)(H/SIZE));
}
} g.setColor(Color.lightGray);
for(int i = 0; i < SIZE; i++){
int h = (int)(i*H/SIZE);
int w = (int)(i*W/SIZE);
if( i%2 == 1 ){
g.fillRect(w,(int)(H/SIZE),(int)(H/SIZE),W-(int)(W/SIZE));
}
} g.setColor(Color.gray);
for(int i=0;i<SIZE;i++){
int w = (int)((i+1)*W/SIZE);
for(int j=0;j<SIZE;j++){
int h = (int)((j+1)*H/SIZE);
if((i*j)%8==0&&!(i%4==0&&j%4==0)&&!(i==0||j==0)&&(i%2==0&&j%2==0))
g.fillRect(w,h,(int)(W/SIZE),(int)(H/SIZE));
}
}
}
if((i*j)%8==0&&(i+j+2)%4==0)
我就觉得不应该这么复杂嘛
全部代码:
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; i++){
int h = (int)(i*H/SIZE);
if( i%2 == 1 ){
g.fillRect((int)(W/SIZE),h,W-(int)(W/SIZE),(int)(H/SIZE));
}
} //画竖线
g.setColor(Color.lightGray);
for(int i = 0; i < SIZE; i++){
int w = (int)(i*W/SIZE);
if( i%2 == 1 ){
g.fillRect(w,(int)(H/SIZE),(int)(H/SIZE),W-(int)(W/SIZE));
}
} //处理交叉点
g.setColor(Color.gray);
for(int i=0;i<SIZE;i++){
int w = (int)((i+1)*W/SIZE);
for(int j=0;j<SIZE;j++){
int h = (int)((j+1)*H/SIZE);
if((i*j)%8==0&&(i+j+2)%4==0)
g.fillRect(w,h,(int)(W/SIZE),(int)(H/SIZE));
}
}
} public void paint(Graphics g){
super.paint(g);
drawAxis(g);
}
}