import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; class JButtonGame extends JFrame implements ActionListener { private int nRows,nCols,nButtons; private int blankCol,blankRow,clickedRow,clickedCol; private JButton[][] buttons; private JTextField textField; public JButtonGame(){ setSize(200,200); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent event){ System.exit(0); } }); JPanel panel=new JPanel(); nRows=4;nCols=4;nButtons=nRows*nCols; panel.setLayout(new GridLayout(nRows,nCols)); buttons=new JButton[nRows][nCols]; for(int nRow=0;nRow<nRows;nRow++){ for(int nCol = 0;nCol<nCols;nCol++){ buttons[nRow][nCol]=new JButton(""); buttons[nRow][nCol].addActionListener(this); panel.add(buttons[nRow][nCol]); } } getContentPane().add("Center",panel); textField = new JTextField("",80); textField.setEditable(false); getContentPane().add("South",textField); int labelsUsed[]=new int [nButtons]; for(int i=0;i<nButtons;i++){ boolean labelUsed;int label; do{ label = random(nButtons)+1; labelUsed = false; for(int j=0;j<i;j++){ labelUsed = ((labelUsed) || (label == labelsUsed[j])); } }while(labelUsed); labelsUsed[i]=label; int nRow= i/nCols;int nCol=i-nRow*nCols; buttons[nRow][nCol].setText((new Integer(label)).toString()); } getButtonPosition((new Integer(nButtons)).toString()); blankRow = clickedRow;blankCol = clickedCol; JButton blank = buttons[clickedRow][clickedCol]; blank.setText(""); blank.setBackground(Color.green); }//end constructionJButtonGame() private int random(int k){ return (int)(k*Math.random()-0.1); } private void getButtonPosition(String label){ for(int nr=0;nr<nRows;nr++){ for(int nc=0;nc<nCols;nc++){ if(buttons[nr][nc].getText().equals(label)){ clickedRow =nr; clickedCol=nc; textField.setText("["+nr+","+nc+"]"+label); } } } }//end method getButtonPosition(String) @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub getButtonPosition(e.getActionCommand()); textField.setText("["+blankRow+","+blankCol+"]=>["+clickedRow+","+clickedCol+"]"); if(clickedRow==blankRow){ if(Math.abs(clickedCol-blankCol)==1){ moveBlank(blankRow,blankCol,clickedRow,clickedCol); }else if(clickedCol==blankCol){ if(Math.abs(clickedRow-blankRow)==1){ moveBlank(blankRow,blankCol,clickedRow,clickedCol); } } } }//end method actionPerformed public void moveBlank(int oldRow,int oldCol,int newRow,int newCol){ JButton oldBlank = buttons[oldRow][oldCol]; JButton newBlank = buttons[newRow][newCol]; String label = newBlank.getText(); newBlank.setText(""); newBlank.setBackground(Color.green); oldBlank.setText(label); oldBlank.setBackground(Color.lightGray); blankRow = newRow;blankCol=newCol; }//end method moveBlank public static void main(String[] args){ new JButtonGame().setVisible(true); }//end method main }
相关推荐
The main objective of this paper is to increase the complexity by novel schemes of mixing (reversible cellular automata) RCA and (non-uniform reversible cellular automata) NRCA. We apply set of ...
Lattice gas cellular automata and lattice Boltzmann models
The survey introduces the di erent types of cellular automata being used for modeling and the analytical methods used to predict its global behavior from its local con gurations. It further gives a ...
MATLAB CODE FOR CELLULAR AUTOMATA
LBM方法的其实来源,LBM是由格子气自动机发展而来,对于要深入研究LBM算法的研究者是学习的基本教材。
随机元胞自动机的参数校正,IJGIS 25周年收录经典文章之一。
Interactive 2D Cellular Automata in 3D is an interactive, 3-dimensional introduction to cellular automata (CA). Currently, the program allows the user to evolve a 100-cell CA grid step-by-step, ...
cellular automata generations
Basic Cellular Automata in Matlab
Conference on Cellular Automata for Research and Industry, held at the University of Karlsruhe (Germany), 4 - 6 October, 2000. The continuation of and growing interest in research on Cellular Automata...
Wolfram S Statistical Mechanics of Cellular Automata..word版
Cellular Automata Simulator for one Lane flow with traffic light
an example code of cellular automata in matlab
数学建模-Cellular Automata in Matlab.zip
元胞自动机matlab代码cellular automata Matlab code
global_dynamics_of_Cellular Automata 元胞自动机的全局动力学
Cellular_Automata_Modeling_of_Physical_Systems 物理系统的元胞自动机模拟
行业教育软件-学习软件-Five Cellular Automata 7.70 英文版.zip
整理网上matlab环境元胞自动机18个源程序,全部调试通过。对广大matlab用户研究了解元胞机有重要参考价值。