java swing 图形界面 棋盘覆盖算法 动画演示
棋盘覆盖算法动画演示
写的demo,后面还可加上手动控制放置以及标序号等功能
效果如下:
java swing GUI 编程 棋盘覆盖算法动画演示
16*16
工程结构
代码:
StartChessBoard.java
package chessboard;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class StartChessBoard {
public static void main(String[] args) {
// TODO 自动生成的方法存根
JFrame jf=new JFrame("棋盘覆盖");
jf.add(new ChessBoard());
jf.setBounds(580,50,800,900);
jf.setResizable(false);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
ChessBoard.java
package chessboard;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class ChessBoard extends JPanel implements ActionListener{
//棋盘大小2^k,特殊方格位置,速度调节
int k;int tx,ty;int speed;
//中间变量
int [][] board;
int tile=0;
Boolean isstart=false;
int i=0;int width;int unit;int jj=0;
//存放
ArrayList list[]=null;
Rgbpoint rgb[]=null;
JComboBox jComboBox;
JTextField jtf1,jtf2,jtf3;
JDialog jd=new JDialog();
Timer timer;
//素材
URL head=ChessBoard.class.getResource("statics/mheader2.png");
ImageIcon headicon= new ImageIcon(head);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
headicon.paintIcon(this, g, 80, 50);
g.setColor(Color.white);
g.drawString("2021.7.19 by M.", 560, 150);
if(isstart==false)return;
if(i<=tile)i++;
//g.setFont(new Font("", Font.PLAIN, 20));
if(i!=1)g.drawString("正在放置第"+(++jj)+"个L块", 320, 80);
if(i>tile)g.drawString("运行完毕!", 320, 110);
//g.setColor(new Color(104,197,107));
int r,gg,b;
r=rgb[0].r;
gg=rgb[0].g;
b=rgb[0].b;
g.setColor(new Color(r,gg,b));
System.out.println("正在放置第"+(jj)+"个L块");
g.fillRect(80+tx*unit, 180+ty*unit, width, width);
for(int j=1;j<i;++j){
r=rgb[j].r;
gg=rgb[j].g;
b=rgb[j].b;
g.setColor(new Color(r,gg,b));
for(int k=0;k<3;++k){
Point ttt=(Point) list[j].get(k);
int x=ttt.x;int y=ttt.y;
g.fillRect(80+x*unit, 180+y*unit,width,width );
}
}
if(i>tile){
//
System.out.println("完犊子");
isstart=false;
}
}
public ChessBoard(){
this.setVisible(true);
this.setLayout(null);
board=new int[200][200];
timer=new Timer(500, this);timer.setActionCommand("timer");
JLabel jLabel = new JLabel("请选择棋盘的阶:");
JLabel jLabel2 = new JLabel("特殊方格位置:");
JLabel jLabel3 = new JLabel("间隔 /ms:");
jLabel.setBounds(25, 0, 100, 50);
jLabel2.setBounds(250,0,100,50);
jLabel3.setBounds(540,0,60,50);
jComboBox = new JComboBox();
jComboBox.addItem(1);
jComboBox.addItem(2);
jComboBox.addItem(3);
jComboBox.addItem(4);
jComboBox.addItem(5);
jComboBox.addItem(6);//64*64
jComboBox.setBounds(125, 10, 70, 30);
jComboBox.addActionListener(this);
jComboBox.setActionCommand("jcbox");
jtf1=new JTextField();jtf2=new JTextField();jtf3=new JTextField();
jtf1.setBounds(360, 10, 70, 30);jtf2.setBounds(440, 10, 70, 30);jtf3.setBounds(600, 10, 70, 30);
jtf1.setText("0");jtf2.setText("0");jtf3.setText("300");
JButton jb=new JButton("start");
jb.setBounds(700, 10, 80, 30);
this.add(jLabel);this.add(jLabel2);this.add(jLabel3);
this.add(jComboBox);
this.add(jtf1);this.add(jtf2);this.add(jtf3);
this.add(jb);
jb.addActionListener(this);
timer.start();
}
public void start(){
timer.setDelay(speed);
Random rd=new Random();
int r,gg,b;
rgb=new Rgbpoint [3000];
for(int i=0;i<3000;++i)
{
r=rd.nextInt(256);
gg=rd.nextInt(256);
b=rd.nextInt(256);
rgb[i]=new Rgbpoint(r, gg, b);
}
i=0;
tile=0;
list=new ArrayList [3000];
for(int i=0;i<3000;++i)list[i]=new ArrayList();
int N=1<<k;
unit=640/N;width=unit/10*9;
chessBoard(0, 0, tx, ty, 1<<k);
isstart=true;
}
void chessBoard(int tr,int tc,int dr,int dc,int size)//tr,tc是当前棋盘的左上角坐标,dr dc是特殊棋子坐标,size棋盘边长
{
if(size==1)return;
int t=++tile,s=size/2;// t为L块顺序放置的编号, s半边长
if(dr<tr+s&&dc<tc+s)chessBoard(tr,tc,dr,dc,s);//左上
else {
board[tr+s-1][tc+s-1]=t;
list[t].add(new Point(tr+s-1,tc+s-1));
chessBoard(tr,tc,tr+s-1,tc+s-1,s);
}
if(dr<tr+s&&dc>=tc+s)chessBoard(tr,tc+s,dr,dc,s);//右上
else {
board[tr+s-1][tc+s]=t;
list[t].add(new Point(tr+s-1,tc+s));
chessBoard(tr,tc+s,tr+s-1,tc+s,s);
}
if(dr>=tr+s&&dc<tc+s)chessBoard(tr+s,tc,dr,dc,s);//左下
else {
board[tr+s][tc+s-1]=t;
list[t].add(new Point(tr+s,tc+s-1));
chessBoard(tr+s,tc,tr+s,tc+s-1,s);
}
if(dr>=tr+s&&dc>=tc+s)chessBoard(tr+s,tc+s,dr,dc,s);//右下
else {
board[tr+s][tc+s]=t;
list[t].add(new Point(tr+s,tc+s));
chessBoard(tr+s,tc+s,tr+s,tc+s,s);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="start"){
if(isstart==true){
System.out.println("正在绘制");
return ;
}
k=(int) jComboBox.getSelectedItem();
tx=Integer.parseInt(jtf1.getText());
ty=Integer.parseInt(jtf2.getText());
speed=Integer.parseInt(jtf3.getText());
jj=0;
start();
}
else if(e.getActionCommand()=="timer"){
if(isstart==true)
{
repaint();
//if(i==tile)JOptionPane.showMessageDialog(null, "运行完毕!");
}
}
else if(e.getActionCommand()=="jcbox"){
repaint();
}
}
}
Rgbpoint.java
package chessboard;
public class Rgbpoint {
int r,g,b;
public Rgbpoint(int r,int g,int b)
{
this.r=r;
this.g=g;
this.b=b;
}
}