📄 minespanel.java
字号:
package screen;import java.awt.*;import javax.swing.*;import java.util.ArrayList;import com.borland.jbcl.layout.*;import findmines.SettingParameters;/** * Title: * Description: * Copyright: Copyright (c) 2004 * Company: * @author * @version 1.0 */public class MinesPanel extends JPanel { MainScreen parentPanel; ArrayList blocks = new ArrayList(); int panelWidth; int panelHeight; SettingParameters param; boolean firstDown =false; //@ property begin --when the first block is clicked by left mouse button, // we can arrang all the mines in the panel int flagCount = 0; public MinesPanel(MainScreen screen,SettingParameters pa){ parentPanel = screen; param = pa; try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } setAllBlock(param.getParamWidth(),param.getParamHeight()); } public void gameReady() { this.firstDown = false; this.flagCount = 0; int i=0; while(i<param.getParamHeight()*param.getParamWidth()) { ((Block)blocks.get(i)).reset(); i++; } } /* public void gameOver() called by : one block which has mine is touched inform MainScreen that game has been over */ public void gameOver() { parentPanel.gameOver(); int i=0; while(i<param.getParamHeight()*param.getParamWidth()) { ((Block)blocks.get(i)).gameOver(); i++; } } /* the method should be called by blocks */ public void leftButtonClicked(int posRow,int posCol) { if (!firstDown) { firstDown = true; this.arrageMines(posRow,posCol); this.parentPanel.gameStart(); } } /* methord: one right Button Clicked the identified mines should be add or delete called by: class Block @param add : true, add a flag false, remove a flag */ public void addIdentifiedMines(boolean add) { if(add) { flagCount++; } else { flagCount--; } this.parentPanel.flagChanged(flagCount); } public void setAllBlock(int width,int height) { int i=0; blocks.clear(); this.removeAll(); this.setLayout(new GridLayout(height,width)); while(i<width*height) { Block block = new Block(this); int row = i/width; int col = i%width; block.setPosition(row,col); blocks.add(i,block); this.add(block,"null"); i=i+1; } panelHeight = height*param.mineWidth+2*2; panelWidth = width*param.mineWidth+2*2; Dimension d = new Dimension(panelWidth,panelHeight); this.setMaximumSize(d); this.setMinimumSize(d); this.setPreferredSize(d); this.repaint(); /* arrangeMines is canceled here it will happen when the first block is clicked by left mouse button */ //arrageMines(); } /* public void doubleButtonPressed(int posRow, int posCol) { int index = posRow*param.getParamWidth()+posCol; int testBlockIndex=-1; int columns = param.getParamWidth() ; int rows = param.getParamHeight() ; // left-top Block if(posRow>0 && posCol>0) { testBlockIndex = (posRow-1)*columns+(posCol-1); ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // top Block if (posRow>0) { testBlockIndex = (posRow-1)*columns+ posCol ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // right-top Block if (posRow>0 && posCol<columns-1) { testBlockIndex = (posRow-1)*columns+ (posCol+1) ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // left Block if (posCol>0) { testBlockIndex = posRow*columns +posCol-1 ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // right Block if (posCol<columns-1) { testBlockIndex = posRow*columns + (posCol+1) ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // left-bottom Block if(posRow<rows-1 && posCol>0) { testBlockIndex = (posRow+1)*columns+(posCol-1); ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // bottom Block if(posRow<rows-1) { testBlockIndex = (posRow+1)*columns+ posCol; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } // right-bottom Block if(posRow<rows-1 && posCol<columns-1) { testBlockIndex = (posRow+1)*columns+(posCol+1); ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray)); } } */ /* public void doubleButtonRelease(int posRow, int posCol) { int index = posRow*param.getParamWidth()+posCol; int testBlockIndex=-1; int columns = param.getParamWidth() ; int rows = param.getParamHeight() ; // left-top Block if(posRow>0 && posCol>0) { testBlockIndex = (posRow-1)*columns+(posCol-1); ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // top Block if (posRow>0) { testBlockIndex = (posRow-1)*columns+ posCol ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // right-top Block if (posRow>0 && posCol<columns-1) { testBlockIndex = (posRow-1)*columns+ (posCol+1) ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // left Block if (posCol>0) { testBlockIndex = posRow*columns +posCol-1 ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // right Block if (posCol<columns-1) { testBlockIndex = posRow*columns + (posCol+1) ; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // left-bottom Block if(posRow<rows-1 && posCol>0) { testBlockIndex = (posRow+1)*columns+(posCol-1); ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // bottom Block // if(posRow<rows-1) { testBlockIndex = (posRow+1)*columns+ posCol; ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } // right-bottom Block // if(posRow<rows-1 && posCol<columns-1) { testBlockIndex = (posRow+1)*columns+(posCol+1); ((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder()); } } */ public boolean openNearArea(int posRow, int posCol) { if(!wrongPosition(posRow,posCol)) return false; int flags = this.getFlags(posRow,posCol); //System.out.println(" Row "+String.valueOf(posRow)+" col:"+String.valueOf(posCol)+" flags"+String.valueOf(flags)); if (flags == -1) { this.gameOver() ; return false; } int index = posRow*param.getParamWidth()+posCol; int existMines = ((Block)blocks.get(index)).getArroundMines() ; if(flags != existMines && existMines !=0 ) return false; /////////////////////////////////////////// /* when flags are equal to existed Mines */ int testBlockIndex=-1; int testBlockArroundMines = 0; int columns = param.getParamWidth() ; int rows = param.getParamHeight() ; /* left-top Block */ if(posRow>0 && posCol>0) { testBlockIndex = (posRow-1)*columns+(posCol-1); Block testBlock = ((Block)this.blocks.get(testBlockIndex)); if(testBlock.untouched() ) { testBlock.openArea(); testBlockArroundMines = testBlock.getArroundMines(); if (testBlockArroundMines==0) openNearArea(posRow-1,posCol-1); } } /* top Block*/ if (posRow>0) { testBlockIndex = (posRow-1)*columns+ posCol ; Block testBlock = ((Block)this.blocks.get(testBlockIndex)); if(testBlock.untouched() ) { testBlock.openArea(); testBlockArroundMines = testBlock.getArroundMines(); if (testBlockArroundMines==0) openNearArea(posRow-1,posCol); } } /* right-top Block*/ if (posRow>0 && posCol<columns-1) { testBlockIndex = (posRow-1)*columns+ (posCol+1) ; Block testBlock = ((Block)this.blocks.get(testBlockIndex));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -