⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame1.java

📁 用java实现的汉诺塔游戏 用java实现的汉诺塔游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        movY = 330 - 15 * (Cdishremain);
        movW = TowC[Cdishremain];
        movH = 10;
        number=number.valueOf( Number[(100-movW)/8+1]);
        TowX = 540;
        TowY = 330 - 15 * (Cdishremain);
        Cdishremain--;
        canmove = true;
        n = 6;
        repaint();}
    }
  }
}



  void contentPane_mouseReleased(MouseEvent e) { //释放鼠标
  if(hand)
  {     x = e.getX();
        y = e.getY();

    if (x <220&& canmove) {  //A塔画盘
      if(from==1)  //盘来自A塔
      {canmove = false;
       Adishremain++;
       TowA[Adishremain] = movW;fromX=210;
       n=8;repaint();}
      else{
        if (Adishremain == 0) {
        n = 7;
        canmove = false;
        Adishremain++;
        TowA[Adishremain] = movW;
        step++;S=S.valueOf(step);jLabel3.setText(S);
        repaint();}
      else {
        if (TowA[Adishremain] < movW)
          {if(movX>=220&&movX<440)
             {canmove = false;
              Bdishremain++;
              TowB[Bdishremain] = movW;fromX=650;}
            if(movX<830&&movX>=440)
            {canmove = false;
              Cdishremain++;
              TowC[Cdishremain] = movW; fromX=1090;}
              n=8;repaint();}
      else {
          n = 7;
          canmove = false;
          Adishremain++;
          TowA[Adishremain] = movW;
          step++;S=S.valueOf(step);jLabel3.setText(S);
          repaint();}
      }
    }
  }
    if (x < 440 && x >= 220 && canmove) { //B塔画盘
      if (from == 2) { //盘来自B塔
        canmove = false;
        Bdishremain++;
        TowB[Bdishremain] = movW;fromX=650;
        n = 8;
        repaint();}
      else {
        if (Bdishremain == 0) {
          canmove = false;
          Bdishremain++;
          TowB[Bdishremain] = movW;
          step++;
          S = S.valueOf(step);
          jLabel3.setText(S);
          n = 3;
          repaint();}
        else {
          if (TowB[Bdishremain] < movW) {
            if (movX >= 0 && movX < 220) {
              canmove = false;
              Adishremain++;
              TowA[Adishremain] = movW;fromX=210;}
            if (movX < 830 && movX >= 440) {
              canmove = false;
              Cdishremain++;
              TowC[Cdishremain] = movW;fromX=1090;}
             n = 8;
            repaint();}
          else {
            canmove = false;
            Bdishremain++;
            TowB[Bdishremain] = movW;
            step++;
            S = S.valueOf(step);
            jLabel3.setText(S);
            n = 3;
            repaint();}
        }
      }
    }

    if (x < 830 && x >= 440 && canmove) { //C塔画盘
      if (from == 3) { //盘来自C塔
        canmove = false;
        Cdishremain++;
        TowC[Cdishremain] = movW;fromX=1090;
        n = 8;
        repaint();}
      else {
        if (Cdishremain == 0) {
          n = 4;
          canmove = false;
          Cdishremain++;
          TowC[Cdishremain] = movW;
          step++;
          S = S.valueOf(step);
          jLabel3.setText(S);
          repaint();}
        else {
          if (TowC[Cdishremain] < movW) {
            if (movX >= 0 && movX <= 220) {
              canmove = false;
              Adishremain++;
              TowA[Adishremain] = movW;fromX=210;}
            if(movX>=220&&movX<440)
            {canmove = false;
             Bdishremain++;
             TowB[Bdishremain] = movW;fromX=650;}
            n = 8;
            repaint();}
          else {
            n = 4;
            canmove = false;
            Cdishremain++;
            TowC[Cdishremain] = movW;
            step++;
            S = S.valueOf(step);
            jLabel3.setText(S);
            repaint();
            if (Cdishremain == disnum) {
              jOptionPane1.showMessageDialog(this, "Well Done!",
                                             "Game Complete",
                                             jOptionPane1.WARNING_MESSAGE);
              n = 0;
              repaint();
              return;}
          }
        }
      }
    }
   }
  }


  void jButton2_actionPerformed(ActionEvent e) {//动画演示
if(MOV){
if(move)
    { ok=false;hand=false;move=false;clean=false;text=false;suspend=true;//动画演示过程除停止动画按钮之外所有按钮无效
      wait=true;HAND=false;
      n=1;repaint();
      jButton2.setText("动画结束");
      if(animate == null || !animate.isAlive())//创建一个线程
           { animate = new Thread(this);
             animate.start();}


         }

else
     {if(animate.isAlive());
             {animate.stop();}
             jButton2.setText("动画演示");
             jButton7.setText("动画暂停");
             ok=true;move=false;text=false;hand=false;
             jTextArea1.setText("");jLabel3.setText("0");jLabel4.setText("");
             num=1;step=0;stepcount=0;
             n=0;
            repaint();}
   }
}

 public void run()
{hanio(disnum,1,2,3);clean=true;}

public void stop()
 {animate=null;}

 public  void movedish(int S,int D)//S为来源塔,D为目标塔
  {switch (S){
      case 1:try{Thread.sleep(600);}
             catch(InterruptedException e){}
            if (Adishremain == 0)
                 return;
                 else {
                n = 2;
                movX = (210 - TowA[Adishremain]) / 2;
                movY = 330 - 15 * (Adishremain);
                movW = TowA[Adishremain];
                movH = 10;
                number=number.valueOf( Number[(100-movW)/8+1]);
                TowX = 100;
                TowY = 330 - 15 * (Adishremain);
                Adishremain--;
                repaint();
                try{Thread.sleep(50);}
                catch(InterruptedException e){}
                n=12;repaint();
                try{Thread.sleep(450);}
                catch(InterruptedException e){}
                if(D==2){n=10;repaint();}
                if(D==3){n=11;repaint();}
              }break;

     case 2:try{Thread.sleep(600);}
                 catch(InterruptedException e){}
                 if (Bdishremain == 0)
                     return;
                 else {
                  movX = (650 - TowB[Bdishremain]) / 2;
                  movY = 330 - 15 * ( Bdishremain);
                  movW = TowB[Bdishremain];
                  movH = 10;
                  number=number.valueOf(Number[(100-movW)/8+1]);
                  TowX = 320;
                  TowY = 330 - 15 * (Bdishremain);
                  Bdishremain--;
                  n = 5;
                  repaint();
                  try{Thread.sleep(50);}
                  catch(InterruptedException e){}
                  n=12;repaint();
                  try{Thread.sleep(450);}
                   catch(InterruptedException e){}
                  if(D==1){n=9;repaint();}
                  if(D==3){n=11;repaint();}
                }break;
     case 3: try{Thread.sleep(600);}
             catch(InterruptedException e){}
            if (Cdishremain == 0)
            return;
            else {
             movX =  (1090 - TowC[Cdishremain]) / 2;
             movY = 330 - 15 * (Cdishremain);
             movW = TowC[Cdishremain];
             movH = 10;
             number=number.valueOf( Number[(100-movW)/8+1]);
             TowX = 540;
             TowY = 330 - 15 * (Cdishremain);
             Cdishremain--;
             n = 6;
             repaint();
             try{Thread.sleep(400);}
             catch(InterruptedException e){}
             n=12;repaint();
             try{Thread.sleep(450);}
             catch(InterruptedException e){}
             if(D==2){n=10;repaint();}
             if(D==1){n=9;repaint();}
                 }
             break;
            }

     switch(D){
      case 1:
                    try{Thread.sleep(500);}
                    catch(InterruptedException e){}
                    n=12;repaint();
                    try{Thread.sleep(200);}
                       catch(InterruptedException e){}
                       n = 7;
                       Adishremain++;
                      TowA[Adishremain] = movW;
                      repaint();
                      break;
      case 2:
                   try{Thread.sleep(500);}
                   catch(InterruptedException e){}
                   n=12;repaint();
                   try{Thread.sleep(200);}
                   catch(InterruptedException e){}
                          n = 3;
                          Bdishremain++;
                          TowB[Bdishremain] = movW;
                          repaint();
                          break;
         case 3:
                  try{Thread.sleep(500);}
                  catch(InterruptedException e){}
                  n=12;repaint();
                  try{Thread.sleep(200);}
                  catch(InterruptedException e){}
                  n = 4;
                  Cdishremain++;
                  TowC[Cdishremain] = movW;
                  repaint();
                  break;
               }
         stepcount++; STEP=STEP.valueOf(stepcount);jLabel3.setText(STEP);
          }

 public void hanio(int x,int a,int b,int c)
 {if(x==1){movedish(a,c);}
     else{hanio(x-1,a,c,b);
          movedish(a,c);
          hanio(x-1,b,a,c);}
 }


public void haniotext(int x,String a,String b,String c)
 {if(x==1){text(a,c);}
     else{haniotext(x-1,a,c,b);
          text(a,c);
          haniotext(x-1,b,a,c);}
   }


 public void text(String a,String b)
 { c=c.valueOf(num);
   jTextArea1.append(c+":"+a+"--->"+b+" ");
   num++;}

  void jButton4_actionPerformed(ActionEvent e) {
  if(clean)
  {
    jButton2.setText("动画演示");
    jButton7.setText("动画暂停");
    ok=true;move=false;text=false;hand=false;
    jTextArea1.setText("");jLabel3.setText("0");jLabel4.setText("");
    num=1;step=0;stepcount=0;
    n=0;
    repaint();}
  }

  void jButton5_actionPerformed(ActionEvent e) {
  if(HAND)
  {ok=false;hand=true;move=false;clean=true;text=true;MOV=false;}
 }

  void jButton3_actionPerformed(ActionEvent e) {//文本输出
  if(text){text=false;haniotext(disnum,"TowA","TowB","TowC");}
     }


  void jButton7_actionPerformed(ActionEvent e) {//动画暂停/继续
if(suspend){
   if(wait)//可暂停
   {wait=false;
    if(animate.isAlive())animate.suspend();jButton7.setText("动画继续");HAND=false;clean=false;ok=false;hand=false;text=true;}
   else//可继续
   {wait=true;
     if(animate.isAlive())animate.resume();jButton7.setText("动画暂停");HAND=false;clean=false;ok=false;hand=false;}
  }
}
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class Frame1_contentPane_mouseAdapter extends java.awt.event.MouseAdapter {
  Frame1 adaptee;

  Frame1_contentPane_mouseAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.contentPane_mousePressed(e);
  }
  public void mouseReleased(MouseEvent e) {
    adaptee.contentPane_mouseReleased(e);
  }
}

class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class Frame1_jButton4_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton4_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton4_actionPerformed(e);
  }
}

class Frame1_jButton5_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton5_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton5_actionPerformed(e);
  }
}

class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton3_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class Frame1_contentPane_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter {
  Frame1 adaptee;

  Frame1_contentPane_mouseMotionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }


}

class Frame1_jButton7_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton7_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton7_actionPerformed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -