棋盘覆盖算法JAVA实现

下面是我写的一个关于棋盘覆盖问题的算法,用四种颜色来实现四个图形对其进行的覆盖:

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class BoardFrame extends JFrame {
	private int Fx = 100, Fy = 100;
	private int n = 0;
	private char size[][] = new char[100][100];
	private int i = 0;
	private JButton button;
	private int n1, n2, n3;
	private JTextField textfield1, textfield2;
	private JTextField text1, text2, text3, text4, text5;

	public BoardFrame() {
		super("ChessBoard");
		Container container = getContentPane();
		container.setLayout(null);
		textfield1 = new JTextField("Enter the Size:");
		textfield1.setEditable(false);
		textfield1.setBounds(0, 35, 100, 20);
		container.add(textfield1);
		text1 = new JTextField("输入特殊点坐标:");
		text1.setEditable(false);
		text1.setBounds(0, 10, 100, 20);
		container.add(text1);
		text2 = new JTextField("x0 (100~540):");
		text2.setEditable(false);
		text2.setBounds(120, 10, 100, 20);
		container.add(text2);
		text3 = new JTextField("200");
		text3.setBounds(230, 10, 40, 20);
		container.add(text3);
		text4 = new JTextField("y0 (100~540):");
		text4.setEditable(false);
		text4.setBounds(300, 10, 100, 20);
		container.add(text4);
		text5 = new JTextField("300");
		text5.setBounds(410, 10, 40, 20);
		container.add(text5);
		textfield2 = new JTextField("8");
		textfield2.setBounds(130, 35, 50, 20);
		container.add(textfield2);
		button = new JButton("确定");
		// button.setLayout(new GridLayout(1,2));
		button.setBounds(300, 35, 90, 20);
		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				n = Integer.parseInt(textfield2.getText());
				Fx = Integer.parseInt(text3.getText());
				Fy = Integer.parseInt(text5.getText());
				i++;
				n1 = 450 / n;
				n2 = ((Fx - 100) / n1) * n1 + 100;
				n3 = ((Fy - 100) / n1) * n1 + 100;
				ChessBoard(100, 100, n2, n3, n);
				repaint();
			}
		}

		);
		container.add(button);
		this.setSize(600, 600);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.white);
		g.fillRect(this.getX(), this.getY(), 600, 600);
		if (i != 0) {
			int x = 100;
			int y = 100;
			g.setColor(Color.black);
			g.fillRect(n2, n3, n1, n1);
			// size[3][0]='b';
			// ChessBoard(100,100,100,100,8);
			for (int i = 0; i < n; i++)
				for (int j = 0; j < n; j++) {
					if (size[j] == 'r') {
						g.setColor(Color.red);
						g.fillRect(i * n1 + 100, j * n1 + 100, n1, n1);
					}
					if (size[j] == 'g') {
						g.setColor(Color.green);
						g.fillRect(i * n1 + 100, j * n1 + 100, n1, n1);
					}
					if (size[j] == 'l') {
						g.setColor(Color.blue);
						g.fillRect(i * n1 + 100, j * n1 + 100, n1, n1);
					}
					if (size[j] == 'y') {
						g.setColor(Color.yellow);
						g.fillRect(i * n1 + 100, j * n1 + 100, n1, n1);
					}
				}
			g.setColor(Color.black);
			g.fillRect(n2, n3, n1, n1);
			g.setColor(Color.BLACK);
			for (; y <= n * n1 + 100; y = y + n1) {
				g.drawLine(x, y, n * n1 + 100, y);
			}
			for (x = 100, y = 100; x <= n * n1 + 100; x = x + n1) {
				g.drawLine(x, y, x, n * n1 + 100);
			}
		}
	}

	void ChessBoard(int tr, int tc, int dr, int dc, int sizes) {
		int s = 0;
		if (sizes == 1)
			return;
		s = sizes / 2;
		if (dr < tr + s * n1 && dc < tc + s * n1) {
			size[s + (tr - 100) / n1 - 1][s + (tc - 100) / n1] = 'g';
			size[s + (tr - 100) / n1][s + (tc - 100) / n1 - 1] = 'g';
			size[s + (tr - 100) / n1][s + (tc - 100) / n1] = 'g';
			ChessBoard(tr, tc, dr, dc, s);
			ChessBoard(tr + s * n1, tc, tr + s * n1, tc + (s - 1) * n1, s);
			ChessBoard(tr, tc + s * n1, tr + (s - 1) * n1, tc + s * n1, s);
			ChessBoard(tr + s * n1, tc + s * n1, tr + s * n1, tc + n1 * s, s);
		}

		else {

			if (dr >= tr + s * n1 && dc < tc + s * n1) {
				size[s + (tr - 100) / n1 - 1][s + (tc - 100) / n1 - 1] = 'l';
				size[s + (tr - 100) / n1 - 1][s + (tc - 100) / n1] = 'l';
				size[s + (tr - 100) / n1][s + (tc - 100) / n1] = 'l';
				ChessBoard(tr, tc, tr + (s - 1) * n1, tc + (s - 1) * n1, s);
				ChessBoard(tr + s * n1, tc, dr, dc, s);
				ChessBoard(tr, tc + s * n1, tr + (s - 1) * n1, tc + s * n1, s);
				ChessBoard(tr + s * n1, tc + s * n1, tr + s * n1, tc + n1 * s,
						s);
			} else {
				if (dr < tr + s * n1 && dc >= tc + s * n1) {
					size[s + (tr - 100) / n1 - 1][s + (tc - 100) / n1 - 1] = 'r';
					size[s + (tr - 100) / n1][s + (tc - 100) / n1 - 1] = 'r';
					size[s + (tr - 100) / n1][s + (tc - 100) / n1] = 'r';
					ChessBoard(tr, tc, tr + (s - 1) * n1, tc + (s - 1) * n1, s);
					ChessBoard(tr + s * n1, tc, tr + s * n1, tc + (s - 1) * n1,
							s);
					ChessBoard(tr, tc + s * n1, dr, dc, s);
					ChessBoard(tr + s * n1, tc + s * n1, tr + s * n1, tc + n1
							* s, s);
				} else {
					if (dr >= tr + s * n1 && dc >= tc + s * n1) {
						size[s + (tr - 100) / n1 - 1][s + (tc - 100) / n1 - 1] = 'y';
						size[s + (tr - 100) / n1][s + (tc - 100) / n1 - 1] = 'y';
						size[s + (tr - 100) / n1 - 1][s + (tc - 100) / n1] = 'y';
						ChessBoard(tr, tc, tr + (s - 1) * n1,
								tc + (s - 1) * n1, s);
						ChessBoard(tr + s * n1, tc, tr + s * n1, tc + (s - 1)
								* n1, s);
						ChessBoard(tr, tc + s * n1, tr + (s - 1) * n1, tc + s
								* n1, s);
						ChessBoard(tr + s * n1, tc + s * n1, dr, dc, s);
					}
				}
			}
		}

	}

}

 

猜你喜欢

转载自fengfu222.iteye.com/blog/2026477