Triomino问题的动态演示程序。
源代码:
/**
* 此程序用于解决的问题是:用一个L形的瓦片(有三个小正方形组成)覆盖一个缺少了一个方块(可以是棋盘上的
* 任何位置)的2^n X 2^n棋盘
*/
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.event.*;
public class Triomino extends JApplet implements ActionListener , Runnable
{
private JLabel lx;
private JLabel ly;
private JTextField tx;
private JTextField ty;
private JButton begin;
private JButton stop;
private Image image0;
private Image image1;
private Thread demo;
private int x,y;
int chessBoard[][] = new int [9][9];//定义一个8 x 8的棋盘
//private int x,y;
public void init()
{
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
chessBoard[i][j] = 0;
Container content = getContentPane();
content.setLayout(new BorderLayout());
JPanel cotrlPane = new JPanel();
cotrlPane.setLayout(new FlowLayout());
//setLayout(null);
lx = new JLabel("x:");
cotrlPane.add(lx);
tx = new JTextField(10);
cotrlPane.add(tx);
ly = new JLabel("y:");
cotrlPane.add(ly);
ty = new JTextField(10);
cotrlPane.add(ty);
begin = new JButton("开始");
begin.addActionListener(this);
cotrlPane.add(begin);
stop = new JButton("清除");
stop.addActionListener(this);
cotrlPane.add(stop);
content.add(cotrlPane, BorderLayout.SOUTH);
}
/**
* 0:表示用蓝色方块覆盖棋盘
* 1:表示用红色方块覆盖棋盘
* 2:表示用绿色方块覆盖棋盘
* 3:表示用黑色方块覆盖棋盘,棋盘上被标记为黄色的方块,表示棋盘被分为四部分以后,最中间的四个方块
* 中,将其中三个所在的部分不包含缺少方块的方块,标记为黑色
* 4:表示用黄色方块覆盖棋盘,棋盘上被标记为黄色的方块,表示缺少的那个方块
*/
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
Rectangle2D.Float rectangle;
for(int i=1; i<=8; i++)
for(int j =1; j<=8; j++)
{
switch(chessBoard[i][j])
{
case 0:
{
g2D.setPaint(Color.blue);
break;
}
case 1:
{
g2D.setPaint(Color.red);
break;
}
case 2: