开始分享自己的学习之路
JAVA
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CheckerBoard {
public static void main(String[] args) {
JFrame mywin = new JFrame("窗口");
mywin.setSize(900, 900);
mywin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mywin.setLocationRelativeTo(null);
Rect r1 = new Rect();
mywin.add(r1);
mywin.setVisible(true);
}
}
class Rect extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i + 1) % 2 != 0 && (j + 1) % 2 != 0)
g.drawRect(i * 100, j * 100, 100, 100);
else if ((i + 1) % 2 == 0 && (j + 1) % 2 == 0)
g.drawRect(i * 100, j * 100, 100, 100);
else
g.fillRect(i * 100, j * 100, 100, 100);
}
}
}
}