黑马程序员JAVA 五子棋

来源:互联网 发布:use女装知乎 编辑:程序博客网 时间:2024/04/29 10:28

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.PrintStream;
import javax.swing.JComponent;
import javax.swing.JPanel;


public class FiveChessAppletDemo {
    publicstatic void main(String args[]){
      ChessFrame cf = new ChessFrame();
      cf.show();
    }
}


class ChessFrame extends JFrame implements ActionListener {
    privateString[] strsize={"20x15","30x20","40x30"};
    privateString[] strmode={"ÈË»ú¶ÔÞÄ","ÈËÈ˶ÔÞÄ"};
    publicstatic boolean iscomputer=true,checkcomputer=true;
    private intwidth,height;
    privateChessModel cm;
    privateMainPanel mp;
    
   //¹¹ÔìÎå×ÓÆåÓÎÏ·µÄÖ÷Ž°Ìå
    publicChessFrame() {
      this.setTitle("Îå×ÓÆåÓÎÏ·");
      cm=new ChessModel(1);
      mp=new MainPanel(cm);
      Container con=this.getContentPane();
      con.add(mp,"Center");
      this.setResizable(false);
      this.addWindowListener(newChessWindowEvent());
      MapSize(20,15);
      JMenuBar mbar = new JMenuBar();
      this.setJMenuBar(mbar);
      JMenu gameMenu = new JMenu("ÓÎÏ·");
      mbar.add(makeMenu(gameMenu, new Object[] {
         "¿ªŸÖ", "ÆåÅÌ","ģʜ", null, "Í˳ö"
         }, this));
      JMenu lookMenu =new JMenu("ÊÓÍŒ");
      mbar.add(makeMenu(lookMenu,new Object[] {
         "Metal","Motif","Windows"
         },this));
      JMenu helpMenu = new JMenu("°ïÖú");
      mbar.add(makeMenu(helpMenu, new Object[] {
         "¹ØÓÚ"
      }, this));
    }

   //¹¹ÔìÎå×ÓÆåÓÎÏ·µÄÖ÷²Ëµ¥
   public  JMenu makeMenu(Objectparent, Object items[], Object target){
      JMenu m = null;
      if(parent instanceof JMenu)
         m = (JMenu)parent;
      else if(parent instanceof String)
         m = new JMenu((String)parent);
      else
         return null;
      for(int i = 0; i < items.length;i++)
         if(items[i] == null)
            m.addSeparator();
         else if(items[i] == "ÆåÅÌ"){
            JMenu jm = new JMenu("ÆåÅÌ");
            ButtonGroup group=new ButtonGroup();
            JRadioButtonMenuItem rmenu;
            for (intj=0;j<strsize.length;j++){
               rmenu=makeRadioButtonMenuItem(strsize[j],target);
               if (j==0)
                  rmenu.setSelected(true);
               jm.add(rmenu);
               group.add(rmenu);
            }
            m.add(jm);
         }else if(items[i] == "ģʜ"){
            JMenu jm = new JMenu("ģʜ");
            ButtonGroup group=new ButtonGroup();
            JRadioButtonMenuItem rmenu;
            for (inth=0;h<strmode.length;h++){
               rmenu=makeRadioButtonMenuItem(strmode[h],target);
               if(h==0)
                  rmenu.setSelected(true);
               jm.add(rmenu);
               group.add(rmenu);
            }
            m.add(jm);
         }else
            m.add(makeMenuItem(items[i], target));
      return m;
    }
    
   //¹¹ÔìÎå×ÓÆåÓÎÏ·µÄ²Ëµ¥Ïî
   public  JMenuItemmakeMenuItem(Object item, Object target){
      JMenuItem r = null;
      if(item instanceof String)
         r = new JMenuItem((String)item);
      else if(item instanceof JMenuItem)
         r = (JMenuItem)item;
      else
         return null;
      if(target instanceof ActionListener)
         r.addActionListener((ActionListener)target);
      return r;
    }
    
   //¹¹ÔìÎå×ÓÆåÓÎÏ·µÄµ¥Ñ¡°ŽÅ¥Êœ²Ëµ¥Ïî
   public  JRadioButtonMenuItemmakeRadioButtonMenuItem(
      Object item, Object target){
      JRadioButtonMenuItem r = null;
      if(item instanceof String)
         r = new JRadioButtonMenuItem((String)item);
      else if(item instanceofJRadioButtonMenuItem)
         r = (JRadioButtonMenuItem)item;
      else
         return null;
      if(target instanceof ActionListener)
         r.addActionListener((ActionListener)target);
      return r;
    }
    
    public voidMapSize(int w,int h){
      setSize(w * 20+50 , h * 20+100 );
      if(this.checkcomputer)
         this.iscomputer=true;
      else
         this.iscomputer=false;
      mp.setModel(cm);
      mp.repaint();
    }
    
    publicboolean getiscomputer(){
      return this.iscomputer;
    }
    
    public voidrestart(){
      int modeChess = cm.getModeChess();
      if(modeChess <= 3&& modeChess >=1){
         cm = new ChessModel(modeChess);
         MapSize(cm.getWidth(),cm.getHeight());
      }else{
         System.out.println("\u81EA\u5B9A\u4E49");
      }
    }
    
    public voidactionPerformed(ActionEvent e){
      String arg=e.getActionCommand();
      try{
         if (arg.equals("Windows"))
            UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
         else if(arg.equals("Motif"))
            UIManager.setLookAndFeel(
               "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
         else
            UIManager.setLookAndFeel(
               "javax.swing.plaf.metal.MetalLookAndFeel");
         SwingUtilities.updateComponentTreeUI(this);
      }catch(Exception ee){}
      if(arg.equals("20x15")){
         this.width=20;
         this.height=15;
         cm=new ChessModel(1);
         MapSize(this.width,this.height);
         SwingUtilities.updateComponentTreeUI(this);
      }
      if(arg.equals("30x20")){
         this.width=30;
         this.height=20;
         cm=new ChessModel(2);
         MapSize(this.width,this.height);
         SwingUtilities.updateComponentTreeUI(this);
      }
      if(arg.equals("40x30")){
         this.width=40;
         this.height=30;
         cm=new ChessModel(3);
         MapSize(this.width,this.height);
         SwingUtilities.updateComponentTreeUI(this);
      }
      if(arg.equals("ÈË»ú¶ÔÞÄ")){
         this.checkcomputer=true;
         this.iscomputer=true;
         cm=new ChessModel(cm.getModeChess());
         MapSize(cm.getWidth(),cm.getHeight());
         SwingUtilities.updateComponentTreeUI(this);
      }
      if(arg.equals("ÈËÈ˶ÔÞÄ")){
         this.checkcomputer=false;
         this.iscomputer=false;
         cm=new ChessModel(cm.getModeChess());
         MapSize(cm.getWidth(),cm.getHeight());
         SwingUtilities.updateComponentTreeUI(this);
      }
      if(arg.equals("¿ªŸÖ")){
         restart();
      }
      if(arg.equals("¹ØÓÚ"))
         JOptionPane.showMessageDialog(this,"Îå×ÓÆåÓÎÏ·²âÊ԰江", "¹ØÓÚ", 0);
      if(arg.equals("Í˳ö"))
         System.exit(0);
    }
}


class ChessModel {
   //ÆåÅ̵Ŀí¶È¡¢žß¶È¡¢ÆåÅ̵Äģʜ£šÈç20¡Á15£©
    private intwidth,height,modeChess;
   //ÆåÅÌ·œžñµÄºáÏò¡¢×ÝÏò×ø±ê
    private intx=0,y=0;
   //ÆåÅÌ·œžñµÄºáÏò¡¢×ÝÏò×ø±êËù¶ÔÓŠµÄÆå×ÓÑÕÉ«£¬
   //Êý×éarrMapShowÖ»ÓÐ3žöÖµ£º1£¬2£¬3£¬-5£¬
   //ÆäÖÐ1Žú±ížÃÆåÅÌ·œžñÉÏϵÄÆå×ÓΪºÚ×Ó£¬
   //2Žú±ížÃÆåÅÌ·œžñÉÏϵÄÆå×ÓΪ°××Ó£¬
   //3Žú±íΪžÃÆåÅÌ·œžñÉÏûÓÐÆå×Ó£¬
   //-5Žú±ížÃÆåÅÌ·œžñ²»Äܹ»ÏÂÆå×Ó
    privateint[][] arrMapShow;
   //œ»»»ÆåÊֵıêʶ£¬ÆåÅÌ·œžñÉÏÊÇ·ñÓÐÆå×ӵıêʶ·û
    privateboolean isOdd,isExist;

    publicChessModel() {}
    
   //žÃ¹¹Ôì·œ·šžùŸÝ²»Í¬µÄÆåÅÌģʜ£šmodeChess£©ÀŽ¹¹œš¶ÔÓŠŽóСµÄÆåÅÌ
    publicChessModel(int modeChess){
      this.isOdd=true;
      if(modeChess == 1){
         PanelInit(20, 15, modeChess);
      }
      if(modeChess == 2){
         PanelInit(30, 20, modeChess);
      }
      if(modeChess == 3){
         PanelInit(40, 30, modeChess);
      }
    }
    
   //°ŽÕÕÆåÅÌģʜ¹¹œšÆåÅÌŽóС
    private voidPanelInit(int width, int height, int modeChess){
      this.width = width;
      this.height = height;
      this.modeChess = modeChess;
      arrMapShow = new int[width+1][height+1];
      for(int i = 0; i <= width;i++){
         for(int j = 0; j <= height;j++){
            arrMapShow[i][j] = -5;
         }
      }
    }
    
   //»ñÈ¡ÊÇ·ñœ»»»ÆåÊֵıêʶ·û
    publicboolean getisOdd(){
      return this.isOdd;
    }

   //ÉèÖÃœ»»»ÆåÊֵıêʶ·û
    public voidsetisOdd(boolean isodd){
      if(isodd)
         this.isOdd=true;
      else
         this.isOdd=false;
    }
    
   //»ñȡijÆåÅÌ·œžñÊÇ·ñÓÐÆå×ӵıêʶֵ
    publicboolean getisExist(){
      return this.isExist;
    }
    
   //»ñÈ¡ÆåÅÌ¿í¶È
    public intgetWidth(){
      return this.width;
    }
    
   //»ñÈ¡ÆåÅ̞߶È
    public intgetHeight(){
      return this.height;
    }
    
   //»ñÈ¡ÆåÅÌģʜ
    public intgetModeChess(){
      return this.modeChess;
    }

   //»ñÈ¡ÆåÅÌ·œžñÉÏÆå×ÓµÄÐÅÏ¢
    publicint[][] getarrMapShow(){
      return arrMapShow;
    }

   //ÅжÏÏÂ×ӵĺáÏò¡¢×ÝÏò×ø±êÊÇ·ñÔœœç
    privateboolean badxy(int x, int y){
      if(x >= width+20 || x< 0)
         return true;
      return y >= height+20 || y< 0;
    }

   //ŒÆËãÆåÅÌÉÏijһ·œžñÉÏ°Ëžö·œÏòÆå×ÓµÄ×îŽóÖµ£¬
   //Õâ°Ëžö·œÏò·Ö±ðÊÇ£º×ó¡¢ÓÒ¡¢ÉÏ¡¢Ï¡¢×óÉÏ¡¢×óÏ¡¢ÓÒÉÏ¡¢ÓÒÏÂ
    publicboolean chessExist(int i,int j){
      if(this.arrMapShow[i][j]==1 ||this.arrMapShow[i][j]==2)
         return true;
      return false;
    }

   //ÅжϞÃ×ø±êλÖÃÊÇ·ñ¿ÉÏÂÆå×Ó
    public voidreadyplay(int x,int y){
      if(badxy(x,y))
         return;
      if (chessExist(x,y))
         return;
      this.arrMapShow[x][y]=3;
    }

   //ÔÚžÃ×ø±êλÖÃÏÂÆå×Ó
    public voidplay(int x,int y){
      if(badxy(x,y))
         return;
      if(chessExist(x,y)){
         this.isExist=true;
         return;
      }else
         this.isExist=false;
      if(getisOdd()){
         setisOdd(false);
      this.arrMapShow[x][y]=1;
      }else{
         setisOdd(true);
         this.arrMapShow[x][y]=2;
      }
    }

   //ŒÆËã»ú×ßÆå
    
    public voidcomputerDo(int width,int height){
      int max_black,max_white,max_temp,max=0;
      setisOdd(true);
      System.out.println("ŒÆËã»ú×ßÆå ...");
      for(int i = 0; i <= width;i++){
         for(int j = 0; j <= height;j++){
            if(!chessExist(i,j)){//Ëã·šÅжÏÊÇ·ñÏÂ×Ó
               max_white=checkMax(i,j,2);//Åжϰ××ÓµÄ×îŽóÖµ
               max_black=checkMax(i,j,1);//ÅжϺÚ×ÓµÄ×îŽóÖµ
               max_temp=Math.max(max_white,max_black);
               if(max_temp>max){
                  max=max_temp;
                  this.x=i;
                  this.y=j;
               }
            }
         }
      }
      setX(this.x);
      setY(this.y);
      this.arrMapShow[this.x][this.y]=2;
    }
    
   //ŒÇÂŒµçÄÔÏÂ×ÓºóµÄºáÏò×ø±ê
    public voidsetX(int x){
      this.x=x;
    }
    
   //ŒÇÂŒµçÄÔÏÂ×ÓºóµÄ×ÝÏò×ø±ê
    public voidsetY(int y){
      this.y=y;
    }
    
   //»ñÈ¡µçÄÔÏÂ×ӵĺáÏò×ø±ê
    public intgetX(){
      return this.x;
    }
    
   //»ñÈ¡µçÄÔÏÂ×ÓµÄ×ÝÏò×ø±ê
    public intgetY(){
      return this.y;
    }

   //ŒÆËãÆåÅÌÉÏijһ·œžñÉÏ°Ëžö·œÏòÆå×ÓµÄ×îŽóÖµ£¬
   //Õâ°Ëžö·œÏò·Ö±ðÊÇ£º×ó¡¢ÓÒ¡¢ÉÏ¡¢Ï¡¢×óÉÏ¡¢×óÏ¡¢ÓÒÉÏ¡¢ÓÒÏÂ
    public intcheckMax(int x, int y,int black_or_white){
      int num=0,max_num,max_temp=0;
      int x_temp=x,y_temp=y;
      int x_temp1=x_temp,y_temp1=y_temp;
      //judge right
      for(int i=1;i<5;i++){
         x_temp1+=1;
         if(x_temp1>this.width)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      //judge left
      x_temp1=x_temp;
      for(int i=1;i<5;i++){
         x_temp1-=1;
         if(x_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      if(num<5)
         max_temp=num;

      //judge up
      x_temp1=x_temp;
      y_temp1=y_temp;
      num=0;
      for(int i=1;i<5;i++){
         y_temp1-=1;
         if(y_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      //judge down
      y_temp1=y_temp;
      for(int i=1;i<5;i++){
         y_temp1+=1;
         if(y_temp1>this.height)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      if(num>max_temp&&num<5)
         max_temp=num;

      //judge left_up
      x_temp1=x_temp;
      y_temp1=y_temp;
      num=0;
      for(int i=1;i<5;i++){
         x_temp1-=1;
         y_temp1-=1;
         if(y_temp1<0 ||x_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      //judge right_down
      x_temp1=x_temp;
      y_temp1=y_temp;
      for(int i=1;i<5;i++){
         x_temp1+=1;
         y_temp1+=1;
         if(y_temp1>this.height ||x_temp1>this.width)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      if(num>max_temp&&num<5)
         max_temp=num;

      //judge right_up
      x_temp1=x_temp;
      y_temp1=y_temp;
      num=0;
      for(int i=1;i<5;i++){
         x_temp1+=1;
         y_temp1-=1;
         if(y_temp1<0 ||x_temp1>this.width)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      //judge left_down
      x_temp1=x_temp;
      y_temp1=y_temp;
      for(int i=1;i<5;i++){
         x_temp1-=1;
         y_temp1+=1;
         if(y_temp1>this.height ||x_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
            num++;
         else
            break;
      }
      if(num>max_temp&&num<5)
         max_temp=num;
      max_num=max_temp;
      return max_num;
    }

   //ÅжÏÊ€žº
    publicboolean judgeSuccess(int x,int y,boolean isodd){
      int num=1;
      int arrvalue;
      int x_temp=x,y_temp=y;
      if(isodd)
         arrvalue=2;
      else
         arrvalue=1;
      int x_temp1=x_temp,y_temp1=y_temp;
      //ÅжÏÓÒ±ß
      for(int i=1;i<6;i++){
         x_temp1+=1;
         if(x_temp1>this.width)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      //ÅжÏ×ó±ß
      x_temp1=x_temp;
      for(int i=1;i<6;i++){
         x_temp1-=1;
         if(x_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      if(num==5)
         return true;

      //ÅжÏÉÏ·œ
      x_temp1=x_temp;
      y_temp1=y_temp;
      num=1;
      for(int i=1;i<6;i++){
         y_temp1-=1;
         if(y_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      //ÅжÏÏ·œ
      y_temp1=y_temp;
      for(int i=1;i<6;i++){
         y_temp1+=1;
         if(y_temp1>this.height)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      if(num==5)
         return true;

      //ÅжÏ×óÉÏ
      x_temp1=x_temp;
      y_temp1=y_temp;
      num=1;
      for(int i=1;i<6;i++){
         x_temp1-=1;
         y_temp1-=1;
         if(y_temp1<0 ||x_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      //ÅжÏÓÒÏÂ
      x_temp1=x_temp;
      y_temp1=y_temp;
      for(int i=1;i<6;i++){
      x_temp1+=1;
      y_temp1+=1;
      if(y_temp1>this.height ||x_temp1>this.width)
         break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      if(num==5)
         return true;

      //ÅжÏÓÒÉÏ
      x_temp1=x_temp;
      y_temp1=y_temp;
      num=1;
      for(int i=1;i<6;i++){
         x_temp1+=1;
         y_temp1-=1;
         if(y_temp1<0 ||x_temp1>this.width)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      //ÅжÏ×óÏÂ
      x_temp1=x_temp;
      y_temp1=y_temp;
      for(int i=1;i<6;i++){
         x_temp1-=1;
         y_temp1+=1;
         if(y_temp1>this.height ||x_temp1<0)
            break;
         if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)
            num++;
         else
            break;
      }
      if(num==5)
         return true;
      return false;
    }

   //Ó®ÆåºóµÄÌáÊŸ
    public voidshowSuccess(JPanel jp){
      JOptionPane.showMessageDialog(jp,"ÄãÓ®ÁË£¬ºÃÀ÷ºŠ!","win",
         JOptionPane.INFORMATION_MESSAGE);
    }
    
   //ÊäÆåºóµÄÌáÊŸ
    public voidshowDefeat(JPanel jp){
      JOptionPane.showMessageDialog(jp,"ÄãÊäÁË£¬ÇëÖØпªÊŒ!","lost",
         JOptionPane.INFORMATION_MESSAGE);
    }
}


class MainPanel extends JPanel
    implementsMouseListener,MouseMotionListener{
    private intwidth,height;//ÆåÅ̵Ŀí¶ÈºÍžß¶È
    privateChessModel cm;
    
   //žùŸÝÆåÅÌģʜÉ趚Ãæ°åµÄŽóС
   MainPanel(ChessModel mm){
      cm=mm;
      width=cm.getWidth();
      height=cm.getHeight();
      addMouseListener(this);
    }
    
   //žùŸÝÆåÅÌģʜÉ趚ÆåÅ̵Ŀí¶ÈºÍžß¶È
    public voidsetModel(ChessModel mm){
      cm = mm;
      width = cm.getWidth();
      height = cm.getHeight();
    }

   //žùŸÝ×ø±êŒÆËã³öÆåÅÌ·œžñÆå×ÓµÄÐÅÏ¢£šÈç°××Ó»¹ÊǺÚ×Ó£©£¬
   //È»ºóµ÷ÓÃdraw·œ·šÔÚÆåÅÌÉÏ»­³öÏàÓŠµÄÆå×Ó
    public voidpaintComponent(Graphics g){
      super.paintComponent(g);
      for(int j = 0; j <= height;j++){
         for(int i = 0; i <= width;i++){
            int v = cm.getarrMapShow()[i][j];
            draw(g, i, j, v);
         }
      }
    }

   //žùŸÝÌṩµÄÆå×ÓÐÅÏ¢£šÑÕÉ«¡¢×ø±ê£©»­Æå×Ó
    public voiddraw(Graphics g, int i, int j, int v){
      int x = 20 * i+20;
      int y = 20 * j+20;
      //»­ÆåÅÌ
      if(i!=width &&j!=height){
         g.setColor(Color.white);
         g.drawRect(x,y,20,20);
      }
      //»­ºÚÉ«Æå×Ó
      if(v == 1 ){
         g.setColor(Color.gray);
         g.drawOval(x-8,y-8,16,16);
         g.setColor(Color.black);
         g.fillOval(x-8,y-8,16,16);
      }
      //»­°×É«Æå×Ó
      if(v == 2 ){
         g.setColor(Color.gray);
         g.drawOval(x-8,y-8,16,16);
         g.setColor(Color.white);
         g.fillOval(x-8,y-8,16,16);
      }
      if(v ==3){
         g.setColor(Color.cyan);
         g.drawOval(x-8,y-8,16,16);
      }
    }

   //ÏìÓŠÊó±êµÄµã»÷ÊÂŒþ£¬žùŸÝÊó±êµÄµã»÷ÀŽÏÂÆ壬
   //žùŸÝÏÂÆåÅжÏÊ€žºµÈ
    public voidmousePressed(MouseEvent evt){
      int x = (evt.getX()-10) / 20;
      int y = (evt.getY()-10) / 20;
      System.out.println(x+" "+y);
      if(evt.getModifiers()==MouseEvent.BUTTON1_MASK){
         cm.play(x,y);
         System.out.println(cm.getisOdd()+""+cm.getarrMapShow()[x][y]);
         repaint();
         if(cm.judgeSuccess(x,y,cm.getisOdd())){
            cm.showSuccess(this);
            evt.consume();
            ChessFrame.iscomputer=false;
         }
         //ÅжÏÊÇ·ñΪÈË»ú¶ÔÞÄ
         if(ChessFrame.iscomputer&&!cm.getisExist()){
            cm.computerDo(cm.getWidth(),cm.getHeight());
            repaint();
            if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){
               cm.showDefeat(this);
               evt.consume();
            }
         }
      }
    }

    public voidmouseClicked(MouseEvent evt){}
    public voidmouseReleased(MouseEvent evt){}
    public voidmouseEntered(MouseEvent mouseevt){}
    public voidmouseExited(MouseEvent mouseevent){}
    public voidmouseDragged(MouseEvent evt){}
    
   //ÏìÓŠÊó±êµÄÍ϶¯ÊÂŒþ
    public voidmouseMoved(MouseEvent moveevt){
      int x = (moveevt.getX()-10) / 20;
      int y = (moveevt.getY()-10) / 20;
      cm.readyplay(x,y);
      repaint();
    }
}

class ChessWindowEvent extends WindowAdapter{
    public voidwindowClosing(WindowEvent e){
      System.exit(0);
    }

   ChessWindowEvent(){}
}黑马程序员JAVA <wbr>五子棋

原创粉丝点击