共设计了两个
1.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random;//http://blog.sina.com.cn/s/blog_5e94b1030100qjyv.html import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class chessBoard extends JFrame implements ActionListener { private int tr, tc, dr, dc, size;// 定义各成员变量 int tile = 1, X, Y; float red, green, blue; JPanel centerPanel,northPanel; JButton[][] button; JTextField TrText, TcText, DrText, DcText, SizeText; JLabel TrLabel, TcLabel, DrLabel, DcLabel, SizeLabel,label; JButton OKButton; JButton CancelButton; chessBoard() { super("棋盘覆盖"); init(); this.setResizable(false); TrText.setEnabled(false); TcText.setEnabled(false); setVisible(true); OKButton.addActionListener(this); CancelButton.addActionListener(this); add(northPanel, BorderLayout.NORTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(centerPanel, BorderLayout.CENTER); } private void init() { centerPanel = new JPanel(); northPanel = new JPanel(); OKButton = new JButton("开始"); CancelButton = new JButton("清除"); TrText = new JTextField("0", 2);// 定义各组件 TcText = new JTextField("0", 2); DrText = new JTextField("0", 2); DcText = new JTextField("0", 2); SizeText = new JTextField("4", 2); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension screen = tk.getScreenSize(); X = screen.width; Y = screen.height; setBounds((X - 800) / 2, (Y - 650) / 2, 800, 650);// 设置窗口大小与位置 TrLabel = new JLabel("起始方格坐标x:"); TcLabel = new JLabel("起始方格坐标y:"); DrLabel = new JLabel("特殊方格坐标x:"); DcLabel = new JLabel("特殊方格坐标y:"); SizeLabel = new JLabel("棋盘规模size:"); label=new JLabel(); northPanel.add(OKButton);// 添加各组件到窗体 northPanel.add(CancelButton); northPanel.add(TrLabel); northPanel.add(TrText); northPanel.add(TcLabel); northPanel.add(TcText); northPanel.add(DrLabel); northPanel.add(DrText); northPanel.add(DcLabel); northPanel.add(DcText); northPanel.add(SizeLabel); northPanel.add(SizeText); } public void actionPerformed(ActionEvent e) { if (e.getSource() == OKButton) { tile=1; centerPanel.removeAll(); centerPanel.updateUI();//更新用户界面 int tR = Integer.parseInt(TrText.getText()); int tC = Integer.parseInt(TcText.getText()); int dR = Integer.parseInt(DrText.getText()); int dC = Integer.parseInt(DcText.getText()); int Size = 1; for (int i = 0; i < Integer.parseInt(SizeText.getText()); i++) Size *= 2; tr = tR; tc = tC; dr = dR; dc = dC; size = Size; gridLayout grid = new gridLayout(); grid.ChessBoard(tr, tc, dr, dc, size); OKButton.setEnabled(false); } if (e.getSource() == CancelButton) {// 当你点“清除”按钮时的事件响应 centerPanel.removeAll(); centerPanel.updateUI(); label.setText("<html><font size=‘5‘>你清除了前一个棋盘.......</font></html>"); centerPanel.add(label); OKButton.setEnabled(true); } } class gridLayout {// 创建Size*size方格,初始化棋子方格 public gridLayout() { centerPanel.setLayout(new GridLayout(size, size)); button = new JButton[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { button[i][j] = new JButton(); button[i][j].setEnabled(false); if (i == dr && j == dc) { button[i][j].setBackground(Color.black); button[i][j].setText("<html><font size=‘3‘ color=‘white‘>棋子</font></html>"); } centerPanel.add(button[i][j]); } } } public void ChessBoard(int tr, int tc, int dr, int dc, int size) {// 算法实现 if (size == 1) // 棋盘方格大小为1,说明递归到最里层 return; int t = tile++;// 每次递增1 Random rd = new Random(); red = rd.nextFloat(); green = rd.nextFloat(); blue = rd.nextFloat(); Color col = new Color(red, green, blue); int s = size / 2; // 棋盘中间的行、列号(相等的) // 检查特殊方块是否在左上角子棋盘中 if (dr < tr + s && dc < tc + s) // 在 ChessBoard(tr, tc, dr, dc, s); else // 不在,将该子棋盘右下角的方块视为特殊方块 { button[tr + s - 1][tc + s - 1].setBackground(col); button[tr + s - 1][tc + s - 1].setText("<html><Font size=‘4‘,color=‘white‘>" +t+ "</Font></html>"); 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 // 不在,将该子棋盘左下角的方块视为特殊方块 { button[tr + s - 1][tc + s].setBackground(col); button[tr + s - 1][tc + s].setText("<html><Font size=‘4‘,color=‘white‘>" +t+ "</Font></html>"); 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 // 不在,将该子棋盘右上角的方块视为特殊方块 { button[tr + s][tc + s - 1].setBackground(col); button[tr + s][tc + s - 1].setText("<html><Font size=‘4‘,color=‘white‘>" + t+ "</Font></html>"); 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 // 不在,将该子棋盘左上角的方块视为特殊方块 { button[tr + s][tc + s].setBackground(col); button[tr + s][tc + s].setText("<html><Font size=‘4‘,color=‘white‘>" + t+ "</Font></html>"); ChessBoard(tr + s, tc + s, tr + s, tc + s, s); } } } public static void main(String[] args) { new chessBoard(); Runtime.getRuntime().gc();// 清除数据垃圾 } }
2.
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class ChessBoard_demo2 extends JFrame implements ActionListener{ ChessBoardPanel chessBoardPanel; JMenuBar jMenuBar; JMenu jMenu_color,jMenu; JMenuItem jMenuItem,jMenuItem2,jMenuItem3,jMenuItem0,jMenuItem6,jMenuItem1; static Color [] colors={Color.red,Color.green,Color.orange,Color.pink,Color.cyan}; int size=3; int dr=0,dc=1; public ChessBoard_demo2(){ setTitle("棋盘覆盖——静态显示"); chessBoardPanel=new ChessBoardPanel(size,dr,dc,colors); jMenuBar=new JMenuBar(); jMenu=new JMenu("菜单"); jMenu_color=new JMenu("改变绘图颜色"); jMenuItem1=new JMenuItem("左上L型颜色"); jMenuItem2=new JMenuItem("左下L型颜色"); jMenuItem3=new JMenuItem("右上L型颜色"); jMenuItem6=new JMenuItem("右下L型颜色"); jMenuItem0=new JMenuItem("初始覆盖点颜色"); jMenuItem=new JMenuItem("设定"); jMenu.setForeground(Color.blue); jMenu_color.setForeground(Color.blue); jMenu_color.add(jMenuItem0); jMenu_color.add(jMenuItem1); jMenu_color.add(jMenuItem2); jMenu_color.add(jMenuItem3); jMenu_color.add(jMenuItem6); jMenu.add(jMenuItem); jMenuBar.add(jMenu); jMenuBar.add(jMenu_color); jMenuItem.addActionListener(this); jMenuItem0.addActionListener(this); jMenuItem1.addActionListener(this); jMenuItem2.addActionListener(this); jMenuItem3.addActionListener(this); jMenuItem6.addActionListener(this); this.setJMenuBar(jMenuBar); add(chessBoardPanel); setSize(768+17,512+65); setLocation(300,50); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); chessBoardPanel.board(0, 0, dr, dc, chessBoardPanel.size); } public static void main(String args[]){ ChessBoard_demo2 chessBoard=new ChessBoard_demo2(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource()==jMenuItem) { System.out.println("xuanzhong"); String regex="[ ]+"; String arraysString[]; String string=new JOptionPane().showInputDialog("请输入2的指数k,即2的k次方作为size\n(size理论值要小于int型表示范围/3)\n和初始覆盖点坐标(x,y),\n所有参数用空格隔开:"); arraysString=string.split(regex); size=Integer.parseInt(arraysString[0]); dr=Integer.parseInt(arraysString[1]); dc=Integer.parseInt(arraysString[2]); System.out.println(size); }else if (e.getSource()==jMenuItem0) { colors[0]=JColorChooser.showDialog(this, "选择初始覆盖点颜色", Color.red); }else if (e.getSource()==jMenuItem1) { colors[1]=JColorChooser.showDialog(this, "选择左上L型颜色", Color.red); }else if (e.getSource()==jMenuItem2) { colors[2]=JColorChooser.showDialog(this, "选择左下L型颜色", Color.red); }else if (e.getSource()==jMenuItem3) { colors[3]=JColorChooser.showDialog(this, "选择右上L型颜色", Color.red); }else if (e.getSource()==jMenuItem6) { colors[4]=JColorChooser.showDialog(this, "选择右下L型颜色", Color.red); } remove(chessBoardPanel); chessBoardPanel = new ChessBoardPanel(size, dr, dc, colors); add(chessBoardPanel); chessBoardPanel.board(0, 0, dr, dc, chessBoardPanel.size); chessBoardPanel.repaint(); setVisible(true); } } class ChessBoardPanel extends JPanel{ int [][] chessmap; int size; int length; static int x=1; int dr,dc; Color [] colors; public ChessBoardPanel(int k,int dr,int dc, Color [] colors) { // TODO Auto-generated constructor stub size = (int) Math.pow(2, k); length = 512 / size; chessmap = new int[size][size]; //initmap(); this.colors=colors; this.dr=dr; this.dc=dc; } public void initmap(){ chessmap = new int[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j <size; j++) { chessmap[i][j]=0; } } } public void board(int mr,int mc,int dr,int dc,int size){ if (size==1) { return; } int s=size/2; int d=x++; //左上 if (dr<mr+s&&dc<mc+s) { board(mr, mc, dr, dc, s); }else { chessmap[mr+s-1][mc+s-1]=d; board(mr, mc, mr+s-1, mc+s-1, s); } //右上 if (dr<mr+s&&dc>=mc+s) { board(mr, mc+s, dr, dc, s); }else { chessmap[mr+s-1][mc+s]=d; board(mr, mc+s, mr+s-1, mc+s, s); } //左下 if (dr>=mr+s&&dc<mc+s) { board(mr+s, mc, dr, dc, s); }else { chessmap[mr+s][mc+s-1]=d; board(mr+s, mc, mr+s, mc+s-1, s); } //右下 if (dr>=mr+s&&dc>=mc+s) { board(mr+s, mc+s, dr, dc, s); }else { chessmap[mr+s][mc+s]=d; board(mr+s, mc+s, mr+s, mc+s, s); } //System.out.println(String.valueOf(color_flag)); } // public void printTheChess() { // for (int i = 0; i < size; i++) { // for (int j = 0; j < size; j++) { // System.out.print(chessmap[i][j]+" "); // } // System.out.println(); // } // } public void paint(Graphics g){ //System.out.println("paint"); // printTheChess(); g.setColor(Color.white); g.fillRect(0, 0, 512, 512); g.setColor(Color.lightGray); g.fillRect(512, 0, 256, 512); g.setColor(Color.black); //画边框 g.drawLine(0+1, 0+1, 512, 0+1); g.drawLine(0+1, 0+1, 0+1, 512); g.drawLine(512, 0, 512, 512); g.drawLine(0, 512, 512, 512); //最右侧分割线 g.drawLine(760, 0, 760, 512); int sub=(int)(length*0.1); for (int i = 0; i < size-1; i++) { for (int j = 0; j <size-1; j++) { if (chessmap[i][j]==0) {//如果该点是初始覆盖点,画出该点,并画出它右下角的L型 g.setColor(colors[0]); g.fill3DRect(j*length, i*length, length, length,true); if(chessmap[i+1][j+1]==chessmap[i+1][j]&&chessmap[i+1][j+1]==chessmap[i][j+1]){ g.setColor(colors[4]); g.fill3DRect((j+1)*length, (i+1)*length, length, length,true); g.fill3DRect((j+1)*length, i*length, length, length,true); g.fill3DRect(j*length, (i+1)*length, length, length,true); //去掉l图形中的划分 g.fillRect((j+1)*length, i*length, length-1, length+sub); g.fillRect(j*length, (i+1)*length, length+sub, length-1); /*//写上标号 g.setColor(Color.red); g.drawString("A",(int)((j+1+0.5)*length), (int)((i+1+0.5)*length));*/ } }else if (chessmap[i][j]==chessmap[i+1][j]&&chessmap[i][j]==chessmap[i][j+1]) {//画出左上L型 g.setColor(colors[1]); g.fill3DRect(j*length, i*length, length, length,true); g.fill3DRect((j+1)*length, i*length, length, length,true); g.fill3DRect(j*length, (i+1)*length, length, length,true); //去掉l图形中的划分 g.fillRect(j*length, i*length, length+sub, length); g.fillRect(j*length, i*length, length, length+sub); /*//写上标号 g.setColor(Color.black); g.drawString("B",(int)((j+0.5)*length), (int)((i+0.5)*length));*/ }else if (chessmap[i][j]==chessmap[i+1][j]&&chessmap[i][j]==chessmap[i+1][j+1]) {//画出左下L型 g.setColor(colors[2]); g.fill3DRect(j*length, i*length, length, length,true); g.fill3DRect((j+1)*length, (i+1)*length, length, length,true); g.fill3DRect(j*length, (i+1)*length, length, length,true); //去掉l图形中的划分 g.fillRect(j*length, (i+1)*length-sub, length, length); g.fillRect(j*length+sub, (i+1)*length, length, length); /*//写上标号 g.setColor(Color.blue); g.drawString("C",(int)((j+0.5)*length), (int)((i+1+0.5)*length));*/ }else if (chessmap[i][j]==chessmap[i][j+1]&&chessmap[i][j]==chessmap[i+1][j+1]) {//画出右上L型 g.setColor(colors[3]); g.fill3DRect(j*length, i*length, length, length,true); g.fill3DRect((j+1)*length, i*length, length, length,true); g.fill3DRect((j+1)*length, (i+1)*length, length, length,true); //去掉l图形中的划分 g.fillRect((j+1)*length-sub, i*length, length-1, length-1); g.fillRect((j+1)*length, i*length+sub, length-1, length-1); /*//写上标号 g.setColor(Color.green); g.drawString("D",(int)((j+1+0.5)*length), (int)((i+0.5)*length));*/ j++;//可以跳过下个扫描点 }else if (chessmap[i+1][j]==chessmap[i][j+1]&&chessmap[i+1][j]==chessmap[i+1][j+1]) {//画出右下L型 g.setColor(colors[4]); g.fill3DRect((j+1)*length, (i+1)*length, length, length,true); g.fill3DRect((j+1)*length, i*length, length, length,true); g.fill3DRect(j*length, (i+1)*length, length, length,true); //去掉l图形中的划分 g.fillRect((j+1)*length, i*length, length-1, length+sub); g.fillRect(j*length, (i+1)*length, length+sub, length-1); /*//写上标号 g.setColor(Color.red); g.drawString("A",(int)((j+1+0.5)*length), (int)((i+1+0.5)*length));*/ j++;//可以跳过下个扫描点 } } } //扫描最后一行是否有初始覆盖点 for (int i = 0; i < size; i++) { if(chessmap[size-1][i]==0){ g.setColor(colors[0]); g.fill3DRect(i*length, (size-1)*length, length, length,true); } } //扫描最后一列是否有初始覆盖点 for (int i = 0; i < size; i++) { if(chessmap[i][size-1]==0){ g.setColor(colors[0]); g.fill3DRect((size-1)*length, i*length, length, length,true); } } //画提示信息 g.setColor(Color.blue); x=512+2; String sizeString="当前规模是"+String.valueOf(size)+"×"+String.valueOf(size); String sizeString1="初始覆盖点坐标: ("+String.valueOf(dr)+","+String.valueOf(dc)+")"; g.drawString("这是棋盘覆盖的静态显示通过递归分治算法:", x, 60); g.drawString(" 红色区域表示初始覆盖点,绿色区域表示左", x, 90); g.drawString("上L型,粉红色区域表示右上L型,橘黄色区域表", x, 120); g.drawString("示左下L型,青色区域表示左上L型。(默认)", x, 150); g.setColor(new Color(111,1,1)); g.drawString(sizeString, x, 200); g.drawString(sizeString1, x, 250); g.setColor(Color.red); g.drawString(" 注意当规模大于512×512之后,图形将无",x,300); g.drawString("法显示出来,但后台程序仍可以继续运行!",x,330); } }
时间: 2024-10-07 21:16:01