📄 block.java
字号:
package screen;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.awt.Event.*;/** * Title: * Description: * Copyright: Copyright (c) 2004 * Company: * @author * @version 1.0 */public class Block extends JPanel implements MouseListener{ ///////////////////////////////////////////////////////////////////////////// /**all properties */ int posRow; // * posRow define the X position in the panel int posCol; // * posCol define the Y postiion in the panel /* @buttonStatus 0: unopened 1: opened 2: identified */ int buttonStatus=0; boolean hasMine=false; int arroundMines=0; /* the showing image in the block -1: the block is not clicked 0: blankBlock 1~8: text from 1 to 8 9: mine 10: flag 11: touch mine 12: wrongly identified */ int content = 0; MinesPanel parentPanel; boolean gameOvered ; Color backgroundColor=new Color(212,208,200); ///////////////////////////////////////////////////////////////////////////// public Block(MinesPanel panel){ parentPanel = panel; try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } ///////////////////////////////////////////////////////////////////////////// /*Mouse Event : public void mouseClicked(MouseEvent e) */ public void mouseClicked(MouseEvent e) { if(gameOvered ) return; int clickCount = e.getClickCount(); /* left mouse button is clicked */ if(e.getModifiers()==InputEvent.BUTTON1_MASK) { if (clickCount==1) //single-clicked { //System.out.println("Single-clicked"); if(this.buttonStatus == 0) { if (openArea() && this.arroundMines == 0) { this.parentPanel.openNearArea(this.posRow,this.posCol); } } } else if(clickCount==2) //double-clicked { parentPanel.openNearArea(this.posRow,this.posCol ); } } } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseReleased(MouseEvent e) { if(gameOvered ) return; if(e.getModifiers()==InputEvent.BUTTON1_MASK) { if(this.buttonStatus == 0) setBorder(BorderFactory.createRaisedBevelBorder()); } } public void mousePressed(MouseEvent e) { if(gameOvered ) return; int clickCount = e.getClickCount(); //only left mouse is pressed if(e.getModifiers()==InputEvent.BUTTON1_MASK) { if (clickCount==1) { if (this.buttonStatus ==0) setBorder(BorderFactory.createLineBorder(Color.gray)); } //else //both --------right left else if(clickCount == 2) { //parentPanel.doubleButtonPressed(this.posRow,this.posCol ); } } // only the right mouse is pressed--show the red else if(e.getModifiers()==InputEvent.BUTTON3_MASK ) { if (buttonStatus==0) { this.showFlag(); parentPanel.addIdentifiedMines(true); } else if (buttonStatus ==2) { this.showNull(); parentPanel.addIdentifiedMines(false); } } } /* public void gameOver() called by : MinesPanel */ public void gameOver() { gameOvered = true; if (this.hasMine) { if (this.buttonStatus == 0 ) { this.showMine(); } } else if (this.buttonStatus ==2) { this.showWrongIdentify(); } } public boolean openArea() { parentPanel.leftButtonClicked(posRow,posCol); this.setBorder(BorderFactory.createLineBorder(Color.gray,1)); if (this.hasMine) { ///////////////////////////////////////// //Game over this.showTouchMine(); parentPanel.gameOver(); return false; } else { this.showText(this.arroundMines); } return true; } public void setArroundMines(int mines) { this.arroundMines= mines; } public int getArroundMines() { return this.arroundMines; } public void setHasMine(boolean has) { this.hasMine = has; } public void setPosition(int row,int col) { this.posRow = row; this.posCol = col; } public void reset() { buttonStatus=0; hasMine=false; arroundMines=2; content = 0; gameOvered = false; setBackground(this.backgroundColor); setBorder(BorderFactory.createRaisedBevelBorder()); repaint(); } public boolean isFlag() { if(this.buttonStatus == 2) return true; return false; } public boolean isOpened() { if (this.buttonStatus ==1) return true; return false; } public boolean untouched() { return (this.buttonStatus ==0); } /* identified mine correctly*/ public boolean correctlyIdentified() { if(!this.hasMine && this.buttonStatus ==2) return false; //System.out.println(String.valueOf(this.posRow )+","+String.valueOf(posCol)); //System.out.println(" hasMine "+String.valueOf(hasMine)+" button Status:"+String.valueOf(buttonStatus)); return true; } public void paint(Graphics g) { super.paint(g); switch(this.content) { case 0: this.drawNull(g);break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: this.drawText(g,content); break; case 9: this.drawMine(g); break; case 10: this.drawFlag(g); break; case 11: this.drawTouchMine(g); break; } } //**************************************************************************** //private method private void showNull() { this.content =0; this.buttonStatus = 0; this.repaint(); } private void showText(int text) { this.content = text; this.buttonStatus = 1; this.repaint() ; } private void showMine() { this.content = 9; this.buttonStatus = 1; repaint(); } private void showFlag() { this.content = 10; this.buttonStatus = 2; //right mouse is clicked repaint(); } private void showTouchMine() { this.content =11; this.buttonStatus = 1; repaint(); } private void showWrongIdentify() { this.content = 12; repaint(); } private void drawNull(Graphics g) { //this.setBackground(Color.lightGray); g.clearRect(2,2,17,17); } private void drawText(Graphics g, int text) { g.clearRect(2,2,17,17); switch(text) { case 1: this.setForeground(Color.blue); break; case 2: this.setForeground(new Color(0,80,0)); break; case 3: this.setForeground(Color.red); break; case 4: this.setForeground(new Color(0,0,100)); break; case 5: this.setForeground(new Color(100,50,0)); break; case 6: this.setForeground(Color.magenta); break; case 7: this.setForeground(Color.yellow); break; case 8: this.setForeground(Color.black); break; } g.setFont(new Font("SansSerif",1,13)); g.drawString(String.valueOf(text),6,15); } private void drawMine(Graphics g) { g.setColor(Color.black); g.drawLine(4,10,16,10); g.drawLine(10,4,10,16); g.fillOval(6,6,9,9); g.setColor(Color.white); g.fillRect(8,8,2,2); } private void drawFlag(Graphics g) { g.setColor(Color.black); g.drawLine(10,4,10,14); g.drawLine(7,13,12,13); g.fillRect(5,14,10,2); g.setColor(Color.red); int xp[]; int yp[]; xp = new int[3]; yp = new int[3]; xp[0]=11; yp[0]=3; xp[1]=11; yp[1]=10; xp[2]=3; yp[2]=7; g.fillPolygon(xp,yp,3); } private void drawTouchMine(Graphics g) { this.setBackground(Color.red); this.drawMine(g); } private void drawWrongIdentify(Graphics g) { this.drawMine(g); g.setColor(Color.red); g.drawLine(5,5,15,15); g.drawLine(6,5,15,14); g.drawLine(5,6,14,15); g.drawLine(5,15,15,5); g.drawLine(5,14,14,5); g.drawLine(6,15,15,6); } void jbInit() throws Exception { buttonStatus=0; hasMine=false; arroundMines=2; content = 0; gameOvered = false; setBackground(this.backgroundColor); setBorder(BorderFactory.createRaisedBevelBorder()); addMouseListener(this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -