コード例 #1
0
ファイル: HannoiTower.java プロジェクト: Eric-aihua/practise
 public void mousePressed(MouseEvent e) {
   Disk disk = null;
   Rectangle rect = null;
   if (e.getSource() == this) {
     move = false;
   }
   if (e.getSource() instanceof Disk) {
     disk = (Disk) e.getSource();
     startX = (int) disk.getBounds().getX();
     startY = (int) disk.getBounds().getY();
     rect = disk.getBounds(); /* get startX startY and strarTowerPoint */
     for (int i = 0; i < 3 * diskNum; i++) {
       if (rect.contains(points[i].getX(), points[i].getY())) {
         strarTowerPoint = i;
         break;
       }
     }
   }
 }
コード例 #2
0
ファイル: HannoiTower.java プロジェクト: Eric-aihua/practise
 public void mouseReleased(MouseEvent e) {
   Disk disk = null;
   move = false;
   Rectangle rect = null;
   int x = 0, y = 0;
   boolean containPoint = false;
   int px = 0, py = 0;
   int endI = 0;
   if (e.getSource() instanceof Disk) {
     /* contain point */
     disk = (Disk) e.getSource();
     e = SwingUtilities.convertMouseEvent(disk, e, this);
     x = e.getX();
     y = e.getY();
     rect = disk.getBounds();
     for (int i = 0; i < points.length; i++) {
       px = points[i].getX();
       py = points[i].getY();
       if (rect.contains(px, py)) {
         endI = i;
         containPoint = true;
       }
     }
     if (disk != null && containPoint) {
       /* contain point */
       if ((px != startX) && (py != startY)) {
         System.out.println("px  py" + px + "  " + py + "  x,  y" + x + "  " + y);
         if (points[endI].isHavaDisk()) {
           /* point hava disk */
           System.out.println("------------------startI" + strarTowerPoint);
           System.out.println("------------------endI" + endI);
           System.out.println(endI + "---------point hava disk----------");
           disk.setLocation(startX, startY);
         } else {
           if (endI == diskNum - 1 || endI == diskNum * 2 - 1 || endI == diskNum * 3 - 1) {
             /* to botton */
             points[endI].putDisk(disk, this);
             System.out.println("------------------startI" + strarTowerPoint);
             System.out.println("------------------endI" + endI);
             System.out.println("********to botton put**********************");
             points[strarTowerPoint].setHavaDisk(false);
             if (strarTowerPoint != diskNum - 1
                 || strarTowerPoint != diskNum * 2 - 1
                 || strarTowerPoint != diskNum * 3 - 1) {
               /*
                * not
                * from
                * botton
                */
               (points[strarTowerPoint + 1].getDisk()).setPressed(false);
               points[strarTowerPoint].setHavaDisk(false);
             } else {
               points[strarTowerPoint].setHavaDisk(false);
             }
           } else {
             /* not to botton */
             if (points[endI + 1].isHavaDisk()) {
               /*
                * next have
                * disk
                */
               Disk tempDisk = points[endI + 1].getDisk();
               if ((tempDisk.getNum() - disk.getNum()) >= 1) {
                 /*
                  * bigger
                  * than
                  * points[EndI+1]
                  */
                 points[endI].putDisk(disk, this);
                 System.out.println("------------------startI" + strarTowerPoint);
                 System.out.println("------------------endI" + endI);
                 System.out.println(
                     "********not to botton and next hava disk put**********************");
                 points[strarTowerPoint].setHavaDisk(false);
                 System.out.println("#################" + points[strarTowerPoint].isHavaDisk());
                 System.out.println("#################" + points[endI].isHavaDisk());
                 if (strarTowerPoint != diskNum - 1
                     || strarTowerPoint != diskNum * 2 - 1
                     || strarTowerPoint != diskNum * 3 - 1) {
                   (points[strarTowerPoint].getDisk()).setPressed(false);
                   points[strarTowerPoint].setHavaDisk(false);
                   tempDisk.setPressed(true);
                 } else {
                   points[strarTowerPoint].setHavaDisk(false);
                   // points[endI].setHavaDisk(true);
                   tempDisk.setPressed(true);
                 }
               } else {
                 /* small than points[EndI+1] */
                 System.out.println("----------small---------");
                 disk.setLocation(startX, startY);
               }
             } else {
               /* next not have disk */
               int minMoveNum = ((endI / diskNum) + 1) * diskNum;
               for (int j = endI + 1; j < minMoveNum; j++) {
                 if (points[j].isHavaDisk()) {}
               }
               System.out.println("---------next not have disk----------");
               disk.setLocation(startX, startY);
             }
           }
         }
       }
     }
     if (disk != null && !containPoint) {
       /* not contain point */
       System.out.println("---------not contain point----------");
       disk.setLocation(startX, startY);
     }
   }
 }