用JAVA制作的扫雷游戏

来源:互联网 发布:绅士文学总是网络异常 编辑:程序博客网 时间:2024/05/21 15:04

本来学学JSP就算了的...突然又想先学JAVA...拿本书看了两个月,才学了一点点皮毛,就忍不住要做点东西才觉得算是真的在学...偏偏连连接数据库以及网络通信以及高级界面都还没看...只能做个最基本的单机小游戏...于是选择了扫雷进行制作,所有功能一目了然...不用去想功能需求...但是做到差不多的时候,过年了...停工...现在回来了又想考软件设计师...因此扫雷英雄排行榜的功能没有制作出来...将所有代码贴在这里,请大家指正...因为没有规范开发经验,做出来的东西除了能运行之外,其他的错误肯定是多如牛毛...烦请指正!

 

/**
   
@version 1.0 2008-01-05
   
@author 赵勇
*/


import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.prefs.Preferences;

import javax.swing.*;
import java.io.InputStream;



/**
   Only for start.
*/


public class Game
{
   
public static void main(String[] args)
   
{  
       GameFrame frame 
= new GameFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(
false);
      frame.setVisible(
true);
   }

}


/**
   The main class for almost all the things.
   It has a inner class and 4 methods.
*/

class GameFrame extends JFrame
{
   
public GameFrame()
   
{
      setTitle(
"Game");
      
      
// Initiative the icon class for save and get the icon informations.
      gameicon = new GameIcon();
      
      
// Initiatives all the action listener for menu.
      initlistener();
      
      
//  Initiatives the new GamePanel for all the state.
      gamepanel = new GamePanel();
      
      
//  Build and fill the menu bar.
      addmenu();
      
      
//  Display the game panel.
      add(gamepanel);
      pack();
      
   }

   
/**
   The method is just Build and fill the menu bar.
   It's not has to write as a method.
*/
   
   
private void addmenu()
   
{
       JMenu filemenu 
= new JMenu("游戏");
       
       JMenuItem newgameitem 
= new JMenuItem("新游戏");
       newgameitem.addActionListener(newgamelistener);
       filemenu.add(newgameitem);
       
       filemenu.addSeparator();
       
       JMenuItem lv1item 
= new JMenuItem("初级");
       lv1item.addActionListener(lv1listener);
       filemenu.add(lv1item);
       JMenuItem lv2item 
= new JMenuItem("中级");
       lv2item.addActionListener(lv2listener);
       filemenu.add(lv2item);
       JMenuItem lv3item 
= new JMenuItem("高级");
       lv3item.addActionListener(lv3listener);
       filemenu.add(lv3item);
       JMenuItem lv_orderitem 
= new JMenuItem("自定义");
       lv_orderitem.addActionListener(lv_orderlistener);
       filemenu.add(lv_orderitem);
       
       filemenu.addSeparator();
       
       JMenuItem exititem 
= new JMenuItem("退出");
       exititem.addActionListener(exitlistener);
       filemenu.add(exititem);
       
       
       JMenu aboutmenu 
= new JMenu("关于");
       JMenuItem aboutitem 
= new JMenuItem("关于本软件");
       aboutitem.addActionListener(aboutlistener);
       aboutmenu.add(aboutitem);
       
       JMenuBar menuBar 
= new JMenuBar();
       setJMenuBar(menuBar);

       menuBar.add(filemenu);
       menuBar.add(aboutmenu);
   }

   
/**
   The method used for initiatives a new game.
   It's has to write as a method , because the inner action listeners need it.
   It removed the old panel first , then initiatives the panel , and add the new panel at last.
*/
 
   
   
private void newgame()
   
{
       
// Remove the old panel .
       remove(gamepanel);
       
// Change the old panel become a new panel.
       gamepanel = new GamePanel();
       
// Add the new panel.
       add(gamepanel);
       
// add the menu bar. maybe it's not useful.
       addmenu();
       pack();
   }

   
/**
   The method is just Build the menu's action listener.
   It's not has to write as a method.
*/
    
   
   
private void initlistener()
   
{   
       
// New action listener for new game.
       newgamelistener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                

                newgame();
                
                }
 
             }
;
             
          
// New action listener for level1 game.
          lv1listener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                
{
                   setpanelsize(
10,8,-1);
                newgame();
                }
 
             }
;
             
          
// New action listener for level2 game.
          lv2listener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                
{
                   setpanelsize(
20,16,-1);
                newgame();
                }
 
             }
;
             
          
// New action listener for level3 game.
          lv3listener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                

                   setpanelsize(
40,32,-1);
                newgame();
                }
 
             }
;
             
          
// New action listener for level_order game.
          lv_orderlistener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                

                 
if (choosedialog == null// first time
                   choosedialog = new ChooseDialog(null);   
               choosedialog.setVisible(
true);
                }
 
             }
;
             
          
// New action listener for exit button.
          exitlistener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                

                System.exit(
0);
                }
 
             }
;
             
          
// New action listener for about button.
          aboutlistener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                

                   
if (aboutdialog == null// first time
                       aboutdialog = new AboutDialog(null);   
                aboutdialog.setVisible(
true);
                }
 
             }
;
   }


   
/**
   A modal dialog that displays a message and waits for the user to input the data.
   
*/

   
class ChooseDialog extends JDialog
   
{  
   
public ChooseDialog(JFrame owner)
   
{  
   
super(owner, "Please choose the size"true);         

   
// Initiatives panel .

   JPanel panel 
= new JPanel();
   panel.setLayout(
new GridLayout(42));

   
// labels and inputs .
   JLabel xlabel = new JLabel("水平");
   
final JFormattedTextField xfield = new JFormattedTextField(NumberFormat.getIntegerInstance());
   xfield.setValue(
new Integer(20));
   JLabel ylabel 
= new JLabel("垂直");
   
final JFormattedTextField yfield = new JFormattedTextField(NumberFormat.getIntegerInstance());
   yfield.setValue(
new Integer(16));
   JLabel bomblabel 
= new JLabel("雷数");
   
final JFormattedTextField bombfield = new JFormattedTextField(NumberFormat.getIntegerInstance());
   bombfield.setValue(
new Integer(80));


   
// YES and NO button closes the dialog

   JButton yes 
= new JButton("确定");
   ActionListener yeslistener 
= new ActionListener()
   
{
        
public void actionPerformed(ActionEvent event) 
        

            
int x = Integer.parseInt(xfield.getValue().toString());
            
int y = Integer.parseInt(yfield.getValue().toString());
            
int bomb = Integer.parseInt(bombfield.getValue().toString());
            setpanelsize(x,y,bomb);
            choosedialog.setVisible(
false);
            newgame();
        }
 
   }
;
   yes.addActionListener(yeslistener);

   JButton no 
= new JButton("取消");
   ActionListener nolistener 
= new ActionListener()
   
{
        
public void actionPerformed(ActionEvent event) 
        

            choosedialog.setVisible(
false);
        }
 
   }
;
   no.addActionListener(nolistener);

   
// add all in the panel .

   panel.add(xlabel);
   panel.add(xfield);
   panel.add(ylabel);
   panel.add(yfield);
   panel.add(bomblabel);
   panel.add(bombfield);
   panel.add(yes);
   panel.add(no);
   add(panel, BorderLayout.CENTER);

   setSize(
250150);
   }

   }

   
/**
   A modal dialog that displays a message and waits for the user to click the YES and NO button.
*/

class AboutDialog extends JDialog
{  
   
public AboutDialog(JFrame owner)
   
{  
   
super(owner, "About the game"true);
   String text 
= "<html><h3>此软件完全由本人制作,本人享有本软件的所有版权。向所有JAVA学习者提供此软件的完全源代码,如有需要,请加QQ21064173,注意注明加Q理由。</h3></html>";
   add(
new JLabel(text) , BorderLayout.CENTER);
   setSize(
250150);
   }

}




/**
A modal dialog that displays a message and waits for the user to input the data.
*/

class SignDialog extends JDialog
{  
public SignDialog(JFrame owner)
{  
super(owner, "Please enter your name"true);         

// Initiatives panel .

JPanel panel 
= new JPanel();
panel.setLayout(
new GridLayout(22));

// labels and inputs .
JLabel namelabel = new JLabel("姓名");
final JTextField namefield = new JTextField();

// YES and NO button closes the dialog

JButton yes 
= new JButton("确定");
ActionListener yeslistener 
= new ActionListener()
{
     
public void actionPerformed(ActionEvent event) 
     

        signdialog.setVisible(
false);
     }
 
}
;
yes.addActionListener(yeslistener);

JButton no 
= new JButton("取消");
ActionListener nolistener 
= new ActionListener()
{
     
public void actionPerformed(ActionEvent event) 
     

         signdialog.setVisible(
false);
     }
 
}
;
no.addActionListener(nolistener);

// add all in the panel .

panel.add(namelabel);
panel.add(namefield);
panel.add(yes);
panel.add(no);
add(panel, BorderLayout.CENTER);

setSize(
25080);
}

}



/**
   
@param x
   
@param y
   
@param bomb
    The method for reset the panel size.
    Use the x , y change the num_x and num_y;
    Write it as a method , so when the inner class want to do this thing , won't change the data by itself.
 
*/

   
private void setpanelsize(int x,int y,int bomb)
   
{
          
if(x<5) x=5;
            
if(x>50) x=50;
            
if(y<3) y=3;
            
if(y>40) y=40;
            
if(bomb>x*y) bomb=x*y;
         num_x 
= x ;
         num_y 
= y ;
         
if (bomb < 0)
             num_bomb 
= (int) (x*y*0.2);
         
else
             num_bomb 
= bomb;
   }

   

/**
    Create the class GamePanel as a inner class so that the GamePanel can used all the private data and methods.
    It used for initiatives all the game informations which is need by running the game.
*/

private class GamePanel extends JPanel
{  
       
public GamePanel()
       
{
      
          
//  Set the panel use the BorderLayout to get the same size label.
          setLayout(new BorderLayout());
          
          
// Set the BorderLayout with num_y and num_x , add the action listener for panel.
          panel = new JPanel();
          panel.setLayout(
new GridLayout(num_y, num_x));
          panel.addMouseListener(
new MouseHandler());

          
// Set the inner panel use the Box layout. 
          b = Box.createHorizontalBox();
          
          
// Initiatives a label to display total time,set the total time to 5000 seconds at the same.
          totaltime = new TimeLabel();
          totaltime.settime(
5000);
          
          
// Initiatives a button as start button . 
          
// Use the image as the icon , set the size as the image and add the action listener newgamelistener.
          ImageIcon newgameicon = gameicon.geticon("newgame.gif");
          JButton newgame 
= new JButton();
          newgame.setIcon(newgameicon);
          newgame.setBorder(
null);
          newgame.addActionListener(newgamelistener);
          
          
// Initiatives a label to display used times , set the total time to 0 seconds at the same.
          usedtime = new TimeLabel();
          usedtime.settime(
0);
          
          
// Initiatives a time listener to get the used time.
          
// If the used time is more than the max time , show game lost.
          ActionListener timelistener = new 
          ActionListener() 
             
{  
                
public void actionPerformed(ActionEvent event) 
                

                   
if(usedtime.gettime()+1 >= maxtime)
                       showlost();
                   
else
                    usedtime.settime(usedtime.gettime()
+1);
                }
 
             }
;
          
          
// Initiatives a timer , alive the time listener for every seconds.
             counttime = new Timer(1000,timelistener);    

          
// Add all the labels and buttons , use the glue for get the good look.
          b.add(totaltime);
          b.add(Box.createGlue());
          b.add(newgame);
          b.add(Box.createGlue());
          b.add(usedtime);     
         
          
// Add the panel and box into the panel used border layout.
          add(panel, BorderLayout.CENTER);
          add(b ,BorderLayout.NORTH );
          
          
// Initiatives the state[][] and choose[][].
          initstate();
          
// Display the panel.
          displayallstate();
          
// Initiatives the bomb.
          buildbomb();
          
// Reset the state[][] with bomb state.
          fillallstate();
          
//display all the state[][] state if need.mostly for debug.
          
//display state ;

       }

/**       
    Add the mode for debug . Delete it after done.
    Show all the label in panel list.
*/

       
private void displaystate()
       
{
              
int r=0;
              
for (JLabel l: panellist)
              
{  
                 l.setText(
""+state[(r-r%num_x)/num_x][r%num_x]);
                 r
++;
              }
       
       }

       
/**
    Initiatives the state of state[][] with 8 ,choose[][] with 0 , and add all labels.
    The arrays state[][],choose[][] used a larger number than the number in fact.
    When add the label , used the num_x and num_y , so the number is the same as the fact.
 
*/

       
private void initstate()
       
{
           
for(int i=0;i<100;i++)
               
for(int j=0;j<80;j++)
               
{
                   state[i][j] 
= 8;
                   choose[i][j] 
= 0;
               }

           
for(int i=0;i<num_x*num_y;i++)
           
{
                  ImageIcon buttonicon 
= gameicon.geticon("8.gif"); 
                  label 
= new JLabel(buttonicon);
                  panel.add(label);
                  panellist.add(label);
           }

       }

       
/**
    Create the bomb in the state[][] , use number -1 instead bomb.
    Count the bomb number if equals num_bomb , close the method.
    If the new bomb and the old bombs are in the same place , recreate it.
 
*/
       
       
private void buildbomb()
       
{
           
int i,r;
           
for(i=0;i<num_bomb;)
           
{
               r 
= (int) (Math.random()*(num_x*num_y));
               
if(!(state[(r-r%num_x)/num_x][r%num_x] == -1))
               
{
               state[(r
-r%num_x)/num_x][r%num_x] = -1;
               i
++;
               }

           }

       }


/**
   Reset the state[][] with bomb state.
   If the place is not bomb , reset the place state.
 
*/

       
private void fillallstate()
       
{
           
for(int i=0;i<num_y;i++)
               
for(int j=0;j<num_x;j++)
               
{
                   
if(state[i][j]!=-1)
                   state[i][j] 
= get(i,j);
               }

       }

       
/**
   
@param x
   
@param y
   
@return
   Count the all the state[][] which next to this place which want to reset.
   If the next place is in the state[][] , check it .
   Check the next place ,if it's state equals -1.
   If the state is -1 , count add 1.
 
*/
       
       
private int get(int x,int y)
       
{
           
int count = 0;
           
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,-1))
                       count
++;
           
return count;
       }


       
/**

   
@param i
   
@param j
   
@return
   Check the number i j . If is in the size ,return true.
 
*/

       
private boolean check(int i,int j)
       
{
           
if(i>=0&&i<num_y&&j>=0&&j<num_x)
               
return true;
           
else
               
return false;
       }

     
/**
 * 
 * 
@param x
 * 
@param y
 * 
@param value
 * 
@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(
250150);
}

}


/**
    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 = "匿名";
}
原创粉丝点击