⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 Java扫雷游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	/*
	 *1.setup grid panel
	 *2.restart game
	 */
	private void resetPane() {
		
		Container container = getContentPane();
		container.setLayout(null);
		container.removeAll();
		container.setBackground(Color.LIGHT_GRAY);
		
		setGridPanel();
		
		JPanel tempPanel = new JPanel(new BorderLayout());
		tempPanel.setBounds(margin, margin, gpd.width, spd.height);
		tempPanel.add(statusPanel,BorderLayout.CENTER);
		
		container.add(tempPanel,null);
		
		tempPanel = new JPanel(new BorderLayout());
		tempPanel.setBounds(margin,margin*2+spd.height,gpd.width,gpd.height);
		tempPanel.add(gridPanel,BorderLayout.CENTER);
		tempPanel.setBorder(BorderFactory.createLoweredBevelBorder());
		
		container.add(tempPanel,null);
		
		int X = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - (gpd.width+3*margin-1)) / 2;
		int Y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - (gpd.height+spd.height+4*margin+titleh)) / 2;

		setLocation(X, Y);
		setSize(gpd.width+3*margin-1, gpd.height+spd.height+4*margin+titleh);
		show();
		restartGame();
	}
	
	private void restartGame() {
		timepassed = 0;
		timeThread = null;
		firstClick = true;
		resetGridPanel();
		resetStatusPanel();
	}
	
	//Method labelMine
	private void labelMine(MineGrid g) {
		if(markCheck) {
			//being labeled then to marked
			if(g.isLabeled()) {
				g.setMarked(true);
				g.setStatus(MineGrid.NORMAL);
				g.setIcon(ImageFactory.getInstance().getImageicon(13));
				leftCount++;
			} else {
				//normal but marked then to normal
				if(g.isMarked()) {
					g.setMarked(false);
					g.setIcon(ImageFactory.getInstance().getImageicon(9));
				} else {
					//normal and not marked then to labeled
					g.setIcon(ImageFactory.getInstance().getImageicon(10));
					g.setStatus(MineGrid.LABELED);
					leftCount--;
				}
			}
		} else {
			//being labeled
			if(g.isLabeled()) {
				g.setIcon(ImageFactory.getInstance().getImageicon(9));
				g.setStatus(MineGrid.NORMAL);
				leftCount++;
			} else {
				//not being labeled
				g.setIcon(ImageFactory.getInstance().getImageicon(10));
				g.setStatus(MineGrid.LABELED);
				leftCount--;
			}
		}
		//upgrade mineLabel
		mineLabel.setIcon(new ImageIcon(led.getLedImage(leftCount,3)));
	}

	//when grid[i] been clicked(cl indicate the botton).
	private void clickGrid(int x, int y, int cl) {
		
		int count=0;
		int lcount=0;
		
		//change status to clicked
		grid[x][y].setStatus(MineGrid.CLICKED);
		
		//mine is clicked		
		if(mode[x][y]) {						
			grid[x][y].setIcon(ImageFactory.getInstance().getImageicon(12));
			endGame(0);
			return;
		}
		//not mine
		//count mines and labeled grids around grid[x][y]
		for(int i=grid[x][y].xlow;i<=grid[x][y].xhigh;i++) {
			for(int j=grid[x][y].ylow;j<=grid[x][y].yhigh;j++) {
				if(mode[i][j])
					count++;
				if(grid[i][j].isLabeled())
					lcount++;
			}
		}//end count
		
		//click by left button
		if(cl==1) {
			grid[x][y].setIcon(ImageFactory.getInstance().getImageicon(count));
			showCount++;
			if( showCount == xBound*yBound - mineCount) {
				endGame(1);
				return;
			}
		}
			//no mine around
		if((cl==1&&count==0)||cl==2&&count==lcount) {
			for(int i=grid[x][y].xlow;i<=grid[x][y].xhigh;i++) {
				for(int j=grid[x][y].ylow;j<=grid[x][y].yhigh;j++) {
					if(i==x&&j==y) continue;
					else if(grid[i][j].isNormal())
						clickGrid(i,j,1);
				}
			}
		}
	}

	//execute on winning or losing
	private void endGame(int status) {
		//stop counting time
		timeThread=null;
		
		//win
		if(status==1) {
			statusButton.setIcon(ImageFactory.getInstance().getImageicon(19));
			for(int x = 0; x < xBound; x++) {
				for(int y = 0; y < yBound; y++) {
					//show mines not labeled
					if(mode[x][y]&&grid[x][y].isNormal())
						grid[x][y].setIcon(ImageFactory.getInstance().getImageicon(10));
					grid[x][y].setStatus(MineGrid.CLICKED);
				}
			}
			leftCount = 0;
			mineLabel.setIcon(new ImageIcon(led.getLedImage(0,3)));
			//show user name input
			if(currentLevel<3&&timepassed<log[currentLevel].getRecord()) {
				log[currentLevel].setRecord(timepassed);
				log[currentLevel].setUserName(
					UserDialog.showInputNameDialog(
						this,currentLevel,log[currentLevel].getUserName()));
				LogDialog dialog = new LogDialog(this, log);
			}
		//lose
		} else {
			statusButton.setIcon(ImageFactory.getInstance().getImageicon(20));
			for(int x = 0; x < xBound; x++) {
				for(int y = 0; y < yBound; y++) {
					//show mines not labeled
					if(mode[x][y]&&grid[x][y].isNormal())
						grid[x][y].setIcon(ImageFactory.getInstance().getImageicon(11));
					//show grid wrong labeled
					else if(!mode[x][y]&&grid[x][y].isLabeled())
						grid[x][y].setIcon(ImageFactory.getInstance().getImageicon(15));
					//forbid any actions
					grid[x][y].setStatus(MineGrid.CLICKED);
				}
			}
		}
	}
	
	private class StatusMouseAdapter extends MouseAdapter {
		
		private boolean mouseIn;
		private boolean mouseDown;
		private Icon icon;
		
		public StatusMouseAdapter() {
			super();
		}
		
		public void mouseEntered(MouseEvent me) {
			mouseIn = true;
			if(mouseDown) {
				statusButton.setBorder(BorderFactory.createLoweredBevelBorder());
				icon = statusButton.getIcon();
				statusButton.setIcon(ImageFactory.getInstance().getImageicon(18));
			}
		}
		
		public void mousePressed(MouseEvent me) {
			mouseDown = true;
			statusButton.setBorder(BorderFactory.createLoweredBevelBorder());
			icon = statusButton.getIcon();
			statusButton.setIcon(ImageFactory.getInstance().getImageicon(18));
		}
		
		public void mouseReleased(MouseEvent me) {
			mouseDown = false;
			statusButton.setIcon(icon);
			statusButton.setBorder(BorderFactory.createRaisedBevelBorder());
			if(mouseIn)	restartGame();
		}
		
		public void mouseExited(MouseEvent me) {
			mouseIn = false;
			if(mouseDown) {
				statusButton.setIcon(icon);
				statusButton.setBorder(BorderFactory.createRaisedBevelBorder());
			}
		}
	}
	
	private class GridMouseAdapter extends MouseAdapter {
		
		private MineGrid current;
		private boolean leftDown;
		private boolean rightDown;
		private boolean middle;
		
		public GridMouseAdapter() {
			super();
		}
		
		public void mousePressed(MouseEvent me) {
			current = (MineGrid)me.getSource();
			//as soon as right button down, label happen
			//when not released, wait for next event
			if(me.getButton()==3) {
				rightDown = true;
				if(!current.isClicked()&&!leftDown) labelMine(current);
			}else if(me.getButton()==2) {
				rightDown = true;
				leftDown = true;
				middle = true;
			}else {
				//click and double click not happen until release button
				leftDown = true;
				if(current.isNormal())
					statusButton.setIcon(ImageFactory.getInstance().getImageicon(18));
				pressGrid(current);
			}
			if(rightDown&&leftDown) {
				//double
				pressAround(current);
			}
		}
		
		public void mouseEntered(MouseEvent me) {
			current = (MineGrid)me.getSource();
			if(leftDown&&rightDown) {
				pressAround(current);
			} else if(leftDown) {
				pressGrid(current);
			}
		}
		
		public void mouseReleased(MouseEvent me) {
			if(current.isNormal())
				statusButton.setIcon(ImageFactory.getInstance().getImageicon(17));
			int x = current.getXpos();
			int y = current.getYpos();
			if(leftDown) {
				leftDown = false;
				if(firstClick) {
					timeThread = new TimeThread();
					timeThread.start();
					firstClick = false;
					//changeMine
					if(mode[x][y]) {
						int i,j;
						do {
							i = (int)(Math.random()*xBound);
							j = (int)(Math.random()*yBound);
						} while(mode[i][j]);
						mode[x][y] = false;
						mode[i][j] = true;
					}
				}
				if(rightDown) {
					releaseAround(current);
					rightDown = false;
					if(middle) {
						middle = false;
					}
					if(current.isClicked()) clickGrid(x,y,2);
				} else {
					if(current.isNormal()) clickGrid(x,y,1);
				}
			} else {
				rightDown = false;
			}
		}
		
		public void mouseExited(MouseEvent me) {
			current = (MineGrid)me.getSource();
			if(leftDown&&rightDown) {
				releaseAround(current);
			} else if(leftDown) {
				releaseGrid(current);
			}
		}

		private void pressGrid(MineGrid g) {
			if(!g.isNormal()) return;
			g.setIcon(ImageFactory.getInstance().getImageicon(0));
		}
		
		private void releaseGrid(MineGrid g) {
			if(!g.isNormal()) return;
			g.setIcon(ImageFactory.getInstance().getImageicon(9));
		}
		
		private void pressAround(MineGrid g) {
			
			int x = g.getXpos();
			int y = g.getYpos();
			for(int i=grid[x][y].xlow;i<=grid[x][y].xhigh;i++) {
				for(int j=grid[x][y].ylow;j<=grid[x][y].yhigh;j++) {
					pressGrid(grid[i][j]);
				}
			}
		}
		
		private void releaseAround(MineGrid g) {
			
			int x = g.getXpos();
			int y = g.getYpos();
			for(int i=grid[x][y].xlow;i<=grid[x][y].xhigh;i++) {
				for(int j=grid[x][y].ylow;j<=grid[x][y].yhigh;j++) {
					releaseGrid(grid[i][j]);
				}
			}
		}
	}
	
	//class TimeThread to count time
	private class TimeThread extends Thread	{
		
		public TimeThread() {}
		
		public void run() {
			
			final Thread currentThread = Thread.currentThread();
			
			while(timepassed<1000&&currentThread==timeThread) {
				//change timeLabel
				SwingUtilities.invokeLater(
					//inner class Runnable
					new Runnable() {
						public void run() {
							timeLabel.setIcon(new ImageIcon(led.getLedImage(timepassed,3)));
						}
					}
				);
				try	{
					Thread.sleep(999);
				} catch(InterruptedException i) {
					System.err.println("sleep interrupted");
				}
				timepassed++;
			}
		}//end of method run
	}//end of class TimeThread	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -