这学期的算法课后作业,也是用java实现了动态的覆盖
代码不多
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
public class chessBoard extends JFrame {
private JPanel contentPane, panel;
private JTextField textField;
private Label[][] labels;
private int size;
public int r,c;
private JTextField rText;
private JTextField cText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chessBoard frame = new chessBoard();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public static Color randomColor() {
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
return new Color(r, g, b);
}
public chessBoard() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(77, 10, 60, 31);
contentPane.add(textField);
textField.setColumns(10);
JLabel label = new JLabel("\u89C4\u683C");
label.setFont(new Font("仿宋", Font.BOLD, 24));
label.setBounds(10, 10, 70, 31);
contentPane.add(label);
JButton btnReady = new JButton("Ready");
btnReady.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
size = Integer.valueOf(textField.getText());
if ((size & size - 1) == 0) {
r = Integer.valueOf(rText.getText());
c = Integer.valueOf(cText.getText());
if (r > size || r <= 0 || c > size || c <= 0) {
JOptionPane.showMessageDialog(null, "输入行列值不合法!", "错误", JOptionPane.ERROR_MESSAGE);
textField.setText("");
rText.setText("");
cText.setText("");
} else {
panel.removeAll();// 清空以前的
panel.setLayout(new GridLayout(size, size, 4, 4));
labels = new Label[size][];
for (int i = 0; i < size; i++)
labels[i] = new Label[size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
labels[i][j] = new Label();
labels[i][j].setBackground(Color.LIGHT_GRAY);
panel.add(labels[i][j]);
}
}
labels[r-1][c-1].setBackground(Color.DARK_GRAY);
panel.updateUI();
}
} else {
JOptionPane.showMessageDialog(null, "输入大小不合法!", "错误", JOptionPane.ERROR_MESSAGE);
textField.setText("");
rText.setText("");
cText.setText("");
}
}
});
btnReady.setFont(new Font("仿宋", Font.BOLD, 24));
btnReady.setBounds(347, 10, 102, 31);
contentPane.add(btnReady);
panel = new JPanel();
panel.setBounds(10, 51, 566, 502);
contentPane.add(panel);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JLabel lblNewLabel = new JLabel("\u884C");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 24));
lblNewLabel.setBounds(144, 10, 31, 31);
contentPane.add(lblNewLabel);
rText = new JTextField();
rText.setBounds(185, 10, 50, 31);
contentPane.add(rText);
rText.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u5217");
lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 24));
lblNewLabel_1.setBounds(245, 10, 31, 31);
contentPane.add(lblNewLabel_1);
cText = new JTextField();
cText.setBounds(286, 10, 50, 31);
contentPane.add(cText);
cText.setColumns(10);
JButton btnNewButton = new JButton("Go");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Board(0,0,r-1,c-1,size);
new Thread(
new Runnable() {
public void run() {
panel.repaint();
panel.updateUI();
}
}).start();
}
});
btnNewButton.setFont(new Font("宋体", Font.BOLD, 24));
btnNewButton.setBounds(459, 10, 70, 31);
contentPane.add(btnNewButton);
}
void Board(int tr, int tc, int dr, int dc, int size) {//dr dc 特殊
if (size == 1)
return;
int s = size / 2;
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
Color c = randomColor();
while (c == Color.LIGHT_GRAY || c == Color.DARK_GRAY) {
c = randomColor();
}
if(dr<tr+s) {
if(dc<tc+s) {//左上
labels[tr+s-1][tc+s].setBackground(c);
labels[tr+s][tc+s].setBackground(c);
labels[tr+s][tc+s-1].setBackground(c);
Board(tr,tc,dr,dc,s);
Board(tr,tc+s,tr+s-1,tc+s,s);
Board(tr+s,tc,tr+s,tc+s-1,s);
Board(tr+s,tc+s,tr+s,tc+s,s);
}
else {//右上
labels[tr+s-1][tc+s-1].setBackground(c);
labels[tr+s][tc+s-1].setBackground(c);
labels[tr+s][tc+s].setBackground(c);
Board(tr,tc,tr+s-1,tc+s-1,s);
Board(tr,tc+s,dr,dc,s);
Board(tr+s,tc,tr+s,tc+s-1,s);
Board(tr+s,tc+s,tr+s,tc+s,s);
}
}
else {
if(dc<tc+s) {//左下
labels[tr+s-1][tc+s-1].setBackground(c);
labels[tr+s-1][tc+s].setBackground(c);
labels[tr+s][tc+s].setBackground(c);
Board(tr,tc,tr+s-1,tc+s-1,s);
Board(tr,tc+s,tr+s-1,tc+s,s);
Board(tr+s,tc,dr,dc,s);
Board(tr+s,tc+s,tr+s,tc+s,s);
}
else {//右下
labels[tr+s-1][tc+s-1].setBackground(c);
labels[tr+s-1][tc+s].setBackground(c);
labels[tr+s][tc+s-1].setBackground(c);
Board(tr,tc,tr+s-1,tc+s-1,s);
Board(tr,tc+s,tr+s-1,tc+s,s);
Board(tr+s,tc,tr+s,tc+s-1,s);
Board(tr+s,tc+s,dr,dc,s);
}
}
}
}