📄 game.java
字号:
* @return
* Check the number i j . If is in the size and this state[x][y] equals value ,return true.
*/
private boolean check(int x,int y,int value)
{
if(check(x,y))
if(state[x][y]==value)
return true;
return false;
}
/**
Initiatives the MouseHandler as action listener to treat the mouse action.
Rebuild the methods mousePressed() and mouseClicked() .
*/
private class MouseHandler extends MouseAdapter
{
// when mouse pressed . get the place of mouse and Calculate the corresponding label.
public void mousePressed(MouseEvent event)
{
// get the mouse place .
getmouse(event);
}
// When mouse clicked , treat it.
public void mouseReleased(MouseEvent event)
{
if(!gameover)
{
// Set the timer begin work.
counttime.start();
// get the mouse place .
getmouse(event);
// If clicked with left button , do it .
if(event.getButton() == MouseEvent.BUTTON1)
{
// If is more than double click , display the next state.
// If not , display the label .
// If the label state is 0 , display all the next label which state is 0 too.
if(event.getClickCount() >= 2 && choose[y][x] == 1)
{
if(hasbomb(y,x))
{
displayallstate();
counttime.stop();
showlost();
}
else
displaynext(y,x);
}
else
if(choose[y][x] == 0)
{
displaythis(i);
if(state[y][x] == 0)
displayallnext(y,x);
}
// Check is win the game.
iswin();
}
// If clicked not with left button , do it .
else
{
// set the choose[][] state.
// if the old state is 0 ,change it to -1 , if if the old state is -1 ,change it to 0.
// display the choose[][] state with icon.
if(choose[y][x] == 0)
{
choose[y][x] = -1;
label = find(i);
ImageIcon stateicon = gameicon.geticon("x.gif");
label.setIcon(stateicon);
}
else
if(choose[y][x] == -1)
{
choose[y][x] = 0;
label = find(i);
ImageIcon stateicon = gameicon.geticon("8.gif");
label.setIcon(stateicon);
}
}
}
}
private void getmouse(MouseEvent event)
{
int now_x = (int) event.getPoint().getX();
int now_y = (int) (event.getPoint().getY());
int label_x = label.getWidth();
int label_y = label.getHeight();
x = (now_x-(now_x%label_x))/label_x;
y = (now_y-(now_y%label_y))/label_y;
i = x+y*num_x+1;
}
// x for the label number in Horizon .
// y for the label number in Vertical.
// i is the sort number from 1 to higher.
private int x;
private int y;
private int i;
}
/**
@param i
@return
Return the label which is the (i)th.
*/
private JLabel find(int i)
{
int count = 0 ;
for (JLabel r : panellist)
{
count++;
if (count == i) return r;
}
return null;
}
/**
Count the chose label number .
If the number equals all label number which is not bomb , show win.
*/
private void iswin()
{
int count = 0;
for(int i=0;i<num_y;i++)
for(int j=0;j<num_x;j++)
if(choose[i][j] == 1)
count++;
if(count == num_x*num_y-num_bomb)
{
counttime.stop();
showwin();
}
}
/**
Display all the states of the state[][] . Display it with icon.
*/
private void displayallstate()
{
for(int i=0;i<num_y;i++)
for(int j=0;j<num_x;j++)
{
label = find(i*num_x+j+1);
ImageIcon stateicon = gameicon.geticon(state[i][j]+".gif");
label.setIcon(stateicon);
}
}
/**
Display the state (i) of the state[][] . Display it with icon.
*/
private void displaythis(int i)
{
if(!gameover)
{
label = find(i);
i=i-1;
int x = (i-i%num_x)/num_x;
int y = i%num_x;
ImageIcon stateicon = gameicon.geticon(state[x][y]+".gif");
label.setIcon(stateicon);
choose[x][y] = 1;
if(state[x][y] == -1)
{
displayallstate();
counttime.stop();
showlost();
}
}
}
/**
@param x
@param y
Display all the next states which equals 0.
*/
private void displayallnext(int x,int y)
{
displaynext(x,y);
for(int i=-1;i<2;i++)
for(int j=-1;j<2;j++)
{
if(!(i==0&&j==0))
if(check(x+i,y+j,0))
if(choose[x+i][y+j] == 0)
{
displaythis((y+j)+(x+i)*num_x+1);
displayallnext(x+i,y+j);
displaynext(x+i,y+j);
}
}
}
/**
@param x
@param y
Display the next label state.
If the next label is bomb , show lost and break this.
*/
private void displaynext(int x,int y)
{
for(int i=-1;i<2;i++)
for(int j=-1;j<2;j++)
{
if(!(i==0&&j==0))
if(check(x+i,y+j))
if(choose[x+i][y+j] == 0)
{
displaythis((y+j)+(x+i)*num_x+1);
if(state[x+i][y+j]==0)
displayallnext(x+i,y+j);
}
}
}
/**
@param x
@param y
@return
Check the next state .
If has bomb , return true , else return false.
*/
private boolean hasbomb(int x,int y)
{
for(int i=-1;i<2;i++)
for(int j=-1;j<2;j++)
{
if(!(i==0&&j==0))
if(check(x+i,y+j))
if(choose[x+i][y+j] == 0 && state[x+i][y+j] == -1)
{
return true;
}
}
return false;
}
/**
Create and show a win dialog.
*/
private void showwin()
{
gameover = true;
String name = "You Win";
String text = "You won the Game , do you want to sign here ?";
ActionListener yeslistener = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
if (signdialog == null) // first time
signdialog = new SignDialog(null);
signdialog.setVisible(true);
}
};
ActionListener nolistener = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
}
};
if (dialog == null) // first time
dialog = new GameDialog(null,name,text,yeslistener,nolistener);
dialog.setVisible(true);
}
/**
Create and show a lost dialog.
*/
private void showlost()
{
gameover = true;
String name = "You Lost";
String text = "You have lost the game , do you want one more ?";
ActionListener yeslistener = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
newgame();
}
};
ActionListener nolistener = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
}
};
if (dialog == null) // first time
dialog = new GameDialog(null,name,text,yeslistener,nolistener);
dialog.setVisible(true);
}
// This panel for add
private JPanel panel;
private JLabel label;
private Box b;
private ArrayList<JLabel> panellist = new ArrayList<JLabel>();
private int[][] state = new int[100][80];
private int[][] choose = new int[100][80];
private GameDialog dialog;
private TimeLabel usedtime;
private TimeLabel totaltime;
private int maxtime = 5000;
private boolean gameover = false;
}
// This dialogs for show choose dialog and about dialog and sign dialog.
private ChooseDialog choosedialog ;
private AboutDialog aboutdialog ;
private SignDialog signdialog ;
// This timer for count used times .
private Timer counttime;
// This icon save all the icon informations.
private GameIcon gameicon;
// This panel used for display the game.
private GamePanel gamepanel;
// This action listeners used for menu.
private ActionListener newgamelistener;
private ActionListener lv1listener;
private ActionListener lv2listener;
private ActionListener lv3listener;
private ActionListener lv_orderlistener;
private ActionListener exitlistener;
private ActionListener aboutlistener;
// This data used for create labels and bombs.
private int num_x = 20;
private int num_y = 16;
private int num_bomb = 64;
}
/**
A modal dialog that displays a message and waits for the user to click the YES and NO button.
*/
class GameDialog extends JDialog
{
public GameDialog(JFrame owner,String name,String text,ActionListener yeslistener,ActionListener nolistener)
{
super(owner, name, true);
// add HTML label to center
add(new JLabel(
"<html><h3><i>"+text+"</i></h3></html>"),
BorderLayout.CENTER);
// YES and NO button closes the dialog
JButton yes = new JButton("YES");
yes.addActionListener(yeslistener);
JButton no = new JButton("NO");
no.addActionListener(nolistener);
// add YES and NO button to southern border
JPanel panel = new JPanel();
panel.add(yes);
panel.add(no);
add(panel, BorderLayout.SOUTH);
setSize(250, 150);
}
}
/**
This class initiatives an time label for set and get the time , and display it when set the time.
*/
class TimeLabel extends JLabel
{
public TimeLabel()
{
setText(int2string(time));
}
// Supply a method for set the time , display the time after set it.
public void settime(int t)
{
if(t<0) t = -t ;
if(t > 9999) t = t % 10000 ;
time = t ;
setText(int2string(time));
}
//Supply a method for get the time.
public int gettime()
{
return time;
}
//Supply a method check the time from integer to string .
private String int2string(int i)
{
int i4 = i%10;
int i3 = ((i-i4)/10)%10;
int i2 = ((i-i4-i3*10))/100%10;
int i1 = ((i-i4-i3*10-i2*100)/1000)%10;
String s = ""+i1+i2+i3+i4+" 秒";
return s;
}
private int time = 0;
}
class GameIcon extends ImageIcon
{
public GameIcon()
{
icon_bomb = changesize(init("-1.gif"));
icon_0 = changesize(init("0.gif"));
icon_1 = changesize(init("1.gif"));
icon_2 = changesize(init("2.gif"));
icon_3 = changesize(init("3.gif"));
icon_4 = changesize(init("4.gif"));
icon_5 = changesize(init("5.gif"));
icon_6 = changesize(init("6.gif"));
icon_8 = changesize(init("8.gif"));
icon_x = changesize(init("x.gif"));
icon_newgame = changesize(init("newgame.gif"));
}
public ImageIcon geticon(String name)
{
if(name.equals("-1.gif"))
return icon_bomb;
if(name.equals("0.gif"))
return icon_0;
if(name.equals("1.gif"))
return icon_1;
if(name.equals("2.gif"))
return icon_2;
if(name.equals("3.gif"))
return icon_3;
if(name.equals("4.gif"))
return icon_4;
if(name.equals("5.gif"))
return icon_5;
if(name.equals("6.gif"))
return icon_6;
if(name.equals("8.gif"))
return icon_8;
if(name.equals("x.gif"))
return icon_x;
if(name.equals("newgame.gif"))
return icon_newgame;
return null;
}
/**
@param name
@return
Use the name to find the image file in jar , if failed , find in file , if also failed ,return null.
Use the image file to create icon and return .
*/
public ImageIcon init(String name)
{
Image m_image;
try {
InputStream in = getClass().getResourceAsStream(name);
if (in == null)
{
return new ImageIcon(name);
}
byte[] buffer = new byte[in.available()];
in.read(buffer);
m_image = Toolkit.getDefaultToolkit().createImage(buffer);
return new ImageIcon(m_image);
}
catch (java.io.IOException e) {
System.err.println("Unable to read image.");
e.printStackTrace();
}
return null;
}
/**
@param icon
@return
Check the icon size . if not equals the button size reset it to the button size .
*/
private ImageIcon changesize(ImageIcon icon)
{
if(icon.getIconWidth() == buttonsize && icon.getIconHeight() == buttonsize )
return icon;
else
return new ImageIcon(icon.getImage().getScaledInstance(buttonsize, buttonsize, Image.SCALE_DEFAULT));
}
private ImageIcon icon_bomb;
private ImageIcon icon_0;
private ImageIcon icon_1;
private ImageIcon icon_2;
private ImageIcon icon_3;
private ImageIcon icon_4;
private ImageIcon icon_5;
private ImageIcon icon_6;
private ImageIcon icon_8;
private ImageIcon icon_x;
private ImageIcon icon_newgame;
private int buttonsize = 20;
}
class Info
{
Info()
{
// Get position, size from preferences
Preferences root = Preferences.userRoot();
node = root.node("/cn/yfsjx/game");
}
// Return the name.
public String getname(int i)
{
return node.get("name"+i , name);
}
// Return the time.
public int gettime(int i)
{
return node.getInt("time"+i,maxtime);
}
// Set the name.
public void setname(int i , String name)
{
node.put("name"+i, name);
}
// Set the time.
public void settime(int i , int time)
{
node.putInt("time"+i, time);
}
private final Preferences node;
private int maxtime = 5000;
private String name = "匿名";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -