Advertisement
marichan022

SnowmanOfHanoi.java

Feb 17th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package SOH;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. @SuppressWarnings("serial")
  7. public class SnowmanOfHanoi extends JComponent {
  8.     private int n, snowmanHeight, floorLevel;
  9.     private final int snowballSize = 25;
  10.     private int[] x = new int[6], y = new int[6], diameter = new int[6];
  11.     private JFrame frame;
  12.    
  13.     SnowmanOfHanoi (int n, JFrame frame) {
  14.         this.frame = frame;                 // referenca
  15.         floorLevel = frame.getHeight() - 250;
  16.         setN(n);
  17.     }
  18.    
  19.     public void repaint() {
  20.         frame.repaint();
  21.         try {
  22.             Thread.sleep(2);
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.    
  28.     public void setN(int n) {
  29.         this.n = n;
  30.         snowmanHeight = n * (n+1) * snowballSize / 2;
  31.         for(int i = n-1; i >= 0; --i) {                     // 0-ti block je najmanji
  32.             diameter[i] = (i+1) * snowballSize;
  33.             x[i] = calculateX(1, i+1);
  34.             y[i] = (i == n-1) ? floorLevel : y[i+1];
  35.             y[i] += 2*i+1 - diameter[i];                    // malo utonuti u onu vecu kuglu
  36.         }
  37.         repaint();
  38.     }
  39.    
  40.     void towerOfHanoi(int n, int from_rod, int to_rod, int aux_rod) {
  41.         if (n == 0)   return;
  42.         towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
  43.         moveAndPaint(n-1, to_rod);
  44.         //System.out.println("Move disk " + n + " from rod " +  from_rod + " to rod " + to_rod);        
  45.         towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
  46.     }
  47.    
  48.     void moveAndPaint(int which, int dest) {
  49.         // dizemo pravokutnik
  50.         int top = floorLevel - snowmanHeight - diameter[n-1] - 100;
  51.         while(y[which] != top) {
  52.             --y[which];
  53.             repaint();
  54.         }
  55.         // micemo pravokutnik lijevo ili desno
  56.         int destX = calculateX(dest, which+1);
  57.         while(x[which] != destX) {
  58.             x[which] += (x[which] > destX) ? -1 : 1;
  59.             repaint();
  60.         }
  61.         // spustamo pravokutnik
  62.         int max = floorLevel;
  63.         for(int i = 0; i < n; ++i) {
  64.             if (i == which) continue;
  65.             if (getRodFromPos(i+1) == dest && y[i] < max)
  66.                 max = y[i];
  67.         }
  68.         while(y[which] != max - diameter[which] + 2*which + 1) {    // malo utonuti u onu vecu kuglu
  69.             ++y[which];
  70.             repaint();
  71.         }
  72.     }
  73.    
  74.     protected void paintComponent(Graphics g) {
  75.         int start = floorLevel - snowmanHeight - 50;
  76.         Image img = Toolkit.getDefaultToolkit().getImage(SnowmanOfHanoi.class.getResource("download.jpg"));
  77.         g.drawImage(img, 0, 0, this);
  78.         g.drawLine(getPosFromRod(1), start, getPosFromRod(1), floorLevel);
  79.         g.drawLine(getPosFromRod(2), start, getPosFromRod(2), floorLevel);
  80.         g.drawLine(getPosFromRod(3), start, getPosFromRod(3), floorLevel);
  81.         for (int i = 0; i < n; ++i) {
  82.             g.setColor(new Color(255, 255, 255));
  83.             g.fillOval(x[i], y[i], snowballSize*(i+1), snowballSize*(i+1));
  84.         }
  85.     }
  86.    
  87.     public int calculateX(int which_rod, int which_block) {
  88.         int offset = which_block * snowballSize / 2;
  89.         return getPosFromRod(which_rod) - offset;
  90.     }
  91.    
  92.     public int getPosFromRod(int which_rod) {
  93.         return which_rod * frame.getWidth() / 4;
  94.     }
  95.    
  96.     public int getRodFromPos(int which_block) {
  97.         // iz jednadzbe calculateX = x[i] izvedemo varijablu which_rod
  98.         return (x[which_block-1] * 4 + 2 * which_block * snowballSize) / frame.getWidth();
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement