예제 #1
0
 public void mouseDragged(MouseEvent e) {
   Disk disk = null;
   int x, y;
   if (e.getSource() instanceof Disk) {
     move = true;
     disk = (Disk) e.getSource();
     e = SwingUtilities.convertMouseEvent(disk, e, this);
   }
   if (e.getSource() == this) {
     if (move && (disk != null)) {
       x = e.getX();
       y = e.getY();
       if (disk.isPressed() == false) {
         disk.setLocation(x - disk.getWidth() / 2, y - disk.getHeight() / 2);
       } else {
         System.out.println("locked");
       }
     }
   }
 }
예제 #2
0
 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);
     }
   }
 }