📄 sweepminebuttonmanager.java
字号:
/*
* @(#)SweepMineButtonManager.java 1.0 03/08/22
* Copyright 2003 Entao Zhang, All rights reserved.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
/**
* 扫雷的mainPane管理类.此类可直接挂接在AbstractUnitPane类下,并可
* 独立成一个游戏组,现在挂接在ImageRevertUnitPane下测试.
*/
public class SweepMineButtonManager extends ComponentsManager
implements ActionListener{
private JPanel panel=new JPanel();
public JButton[] button;
/**
* sum为已被确认无雷的grids数,sum为此局总雷数,flagNum为除被标记之外
* 剩余的雷数, time为已用时间.
*/
public int num=0,sum=0,flagNum=0,time=0;
private int temp,swap;
//borderA设置正常的grid风格,borderB设置被按下时的grid风格.
private Border borderA,borderB;
//计时器
public Timer timer=new Timer(1000,this);
//两个提示区
private JLabel flagNumLabel=new JLabel("000"),timeLabel=new JLabel("000");
//一个grid周围的8个gris的偏移(真对grid本身).
public int aroundOffset[];
//已翻开后的数字表示.
public final int OPENED=-2, MINE=-1, ERROR_FLAG=-3;
//周围雷的个数数字显示.
public String[] str={" ", "1", "2", "3", "4", "5", "6", "7", "8"};
/**
* strMine为雷的字符,strError为标记错误的字符,strMark为标记为雷的字符
* strBlank为没有被处理时的空白符.
*/
public String strMine="*",strError="#",strMark="?",strFlag="~",strBlank=" ";
//下边为各种字符对应的颜色.
private Color[] strColors={null,Color.BLUE,new Color(0,128,0),
Color.RED,new Color(0,0,128),new Color(128,0,0),
new Color(0,128,128),new Color(128,0,128),Color.BLACK};
private Color strMineColor=Color.BLACK,strErrorColor=Color.RED;
public Color strMarkColor=Color.BLUE,strFlagColor=Color.RED;
//chain在播开的grid为0时,对其周围进行递归搬开处理.
private boolean chain;
public SweepMineButtonManager(ImageRevertUnitPane ir){
super(ir);
setConfuse(new SweepMineConfuse());
setEventListener(new SweepMineButtonEventListener(ir,this));
Font font=new Font("Dialog",Font.BOLD,18);
flagNumLabel.setFont(font);
timeLabel.setFont(font);
flagNumLabel.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
timeLabel.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
}
public Component buildMainPanel(){
int x=ir.x, y=ir.y;
int gridLength=ir.gridLength;
int aroundOffset[]={-x-1,-x,-x+1,-1,1,x-1,x,x+1};
this.aroundOffset=aroundOffset;
panel.removeAll();
panel.setLayout(new GridBagLayout());
button=new JButton[gridLength];
Border border1=BorderFactory.createBevelBorder(0,
new Color(255,255,255),new Color(200,200,200),
new Color(0,0,0),new Color(160,160,160));
Border border2=BorderFactory.createEmptyBorder(-3,2,-3,2);
borderA=BorderFactory.createCompoundBorder(border1,border2);
borderB=BorderFactory.createEmptyBorder(-1,4,-1,5);
GridBagConstraints ct = new GridBagConstraints();
Insets insets=new Insets(-1,-1,-1,-1);
Insets insets2=new Insets(6,6,6,6);
//ct.insets=insets;
ct.fill=GridBagConstraints.BOTH;
Font f=new Font("Dialog",Font.BOLD,12);
Color color=new Color(192,192,192);
for (int i=0; i<gridLength; i++){
ct.gridx = i%x;
ct.gridy = i/x;
panel.add((button[i]=new JButton(strBlank)),ct);
button[i].setBorder(borderA);
button[i].setMargin(insets2);
button[i].setFont(f);
button[i].setBackground(color);
button[i].setFocusable(false);
el.addListener(button[i]);
button[i].setName(String.valueOf(i));
}
//雷区上边的提示及开始按钮组装.
JPanel msgPanel=new JPanel(new BorderLayout());
JButton startButton=new JButton("开始");
startButton.setFont(new Font("Dialog",Font.PLAIN,12));
startButton.setMargin(new Insets(0,0,0,0));
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
ir.beConfused();
}
});
msgPanel.add(flagNumLabel,BorderLayout.WEST);
msgPanel.add(startButton);
msgPanel.add(timeLabel,BorderLayout.EAST);
Border border3=BorderFactory.createBevelBorder(0,
new Color(0,0,0),new Color(160,160,160),
new Color(255,255,255),new Color(200,200,200));
Border border4=BorderFactory.createEmptyBorder(0,0,5,0);
msgPanel.setBorder(BorderFactory.createCompoundBorder(border4,border3));
JPanel mainPanel=new JPanel(new BorderLayout());
mainPanel.setBorder(BorderFactory.createTitledBorder("爱你每一天"));
mainPanel.add(msgPanel,BorderLayout.NORTH);
mainPanel.add(panel,BorderLayout.CENTER);
ir.setBuilded(true);
ir.beConfused();
return mainPanel;
}
public void componentStateChange(int where){
if (ir.isConfused()){
button[where].setBorder(borderA);
button[where].setText(strBlank);//digitalToString(ir.grids[where]));
}else{
//button[where].setText(strBlank);//digitalToString(where));
}
}
public void componentStateChange(int where,boolean state){
if (state){
button[where].setText(strBlank);
button[where].setBorder(borderA);
}else{
button[where].setText(str[0]);
button[where].setBorder(borderB);
}
}
public void swapStateChange(boolean state){
swap=ir.swap;
temp=ir.grids[swap];
if (!state){
button[swap].setBorder(borderB);
if (temp>=0){
button[swap].setForeground(strColors[temp]);
button[swap].setText(str[temp]);
}else if(temp==-1){
button[swap].setForeground(strMineColor);
button[swap].setText(strMine);
}else if(temp==-3){
button[swap].setForeground(strErrorColor);
button[swap].setText(strError);
}
}else{
//button[swap].setText(strBlank);
//button[swap].setBorder(borderA);
}
}
//播开grid,为雷时结束游戏,为0时进行递归.
public void chainOpen(int loc){
if (ir.grids[loc] >= 0){
ir.swap=loc;
swapStateChange(false);
sum++;
chain=ir.grids[loc]==0;
ir.grids[loc]=OPENED;
if (chain){
boolean around[]=checkAround(loc);
for (int i=0; i<around.length; i++){
if(around[i]){
swap=loc+aroundOffset[i];
if (ir.grids[swap]>=0 && button[swap].getText()
!=strFlag)
chainOpen(swap);
}
}
}
}else if (ir.grids[loc]==MINE){
timer.stop();
for (int i=0; i<ir.gridLength; i++){
if(button[i].getText()==strFlag){
if(ir.grids[i]!=MINE){
ir.grids[i]=ERROR_FLAG;
ir.swap=i;
swapStateChange(false);
}
}else if (ir.grids[i]==MINE){
ir.swap=i;
swapStateChange(false);
}
}
ir.setConfused(false);
}
}
//grid的标记方法.
public void markButton(int loc){
if (button[loc].getText()==strFlag){
button[loc].setForeground(strMarkColor);
button[loc].setText(strMark);
flagNum++;
}else if (button[loc].getText()==strMark) {
button[loc].setText(strBlank);
}else{
button[loc].setForeground(strFlagColor);
button[loc].setText(strFlag);
flagNum--;
}
flagNumLabel.setText(digitalToString(flagNum));
}
//记时事件的处理方法.
public void actionPerformed(ActionEvent e){
time++;
timeLabel.setText(digitalToString(time));
}
//提示数字的格式化.
private String digitalToString(int i){
String str="000"+String.valueOf(i);
return str.substring(str.length()-3);
}
//重载父类此方法以适应专用的confuse.
public int runConfuse(int[] grids, int x, int y){
if (confuse==null){
return -2;
}
if(ir.gridLength <320)
num=ir.gridLength/6;
else num=ir.gridLength/5;
return ((SweepMineConfuse)confuse).runConfuse(grids,x,y,num);
}
//重载父类的此方法以完成每局的初始化.
public void beConfused(){
sum=0; flagNum=num; time=0;
timer.start();
flagNumLabel.setText(digitalToString(flagNum));
timeLabel.setText(digitalToString(time));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -