有更好的算法吗?

helpall helpall 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
// 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);
}
}
...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
helpall
Java SE 62,621 成员 307,251 内容