Towers of Hanoi : Animation « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » AnimationScreenshots 
Towers of Hanoi
     
import java.awt.*;

import javax.swing.*;

import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>A sample game application
 * <li>Graphics animation
 * <li>GObject reparenting
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo14 extends JFrame
{
  private TowersOfHanoi  towersOfHanoi_;
  private GWindow        window_;
  private Peg[]          pegs_;
  private int            nDiscs_;
  private JButton        startButton_;
  
  
  
  public Demo14 (int nDiscs)
  {
    super ("G Graphics Library - Demo 14");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    nDiscs_ = nDiscs;

    // Create the graphic canvas
    window_ = new GWindow (new Color (200230200));
    getContentPane().add (window_.getCanvas());
    
    // Create scene
    GScene scene = new GScene (window_);
    double w0[] {0.0,  0.00.0};
    double w1[] {4.0,  0.00.0};
    double w2[] {0.0,  nDiscs_ * 20.0};
    scene.setWorldExtent (w0, w1, w2);

    // Add title object and add to scene
    scene.add (new Title());
    
    // Create the 3 pegs and add to the scene
    int nPegs = 3;
    pegs_ = new Peg[nPegs];
    for (int i = 0; i < nPegs; i++) {
      pegs_[inew Peg (i + 1.0);
      scene.add (pegs_[i]);
    }

    // Create the discs and add to the first peg
    for (int i = 0; i < nDiscs; i++) {
      Disc disc = new Disc ((double) (nDiscs - i/ nDiscs);
      disc.setPosition (1.0, i);
      pegs_[0].add (disc);
    }

    pack();
    setSize (new Dimension (500500));
    setVisible (true);

    // Create the puzzle and execute the solution
    towersOfHanoi_ = new TowersOfHanoi();
    towersOfHanoi_.solve();
  }



  public void discMoved (int source, int destination)
  {
    // This is the disc to move
    Disc disc = (Discpegs_[source].getChild (pegs_[source].getNChildren()-1);
    
    double y0 = disc.getY();
    double y1 = nDiscs_ + 4.0;

    double x0 = pegs_[source].getX();
    double x1 = pegs_[destination].getX();

    // Animate vertical up movement
    double step = 0.2;
    double y = y0;
    while (y < y1) {
      disc.setPosition (x0, y);
      disc.redraw();
      window_.refresh();
      y += step;
    }

    // Reparent peg
    pegs_[source].remove (disc);
    pegs_[destination].add (disc);
    
    // Animate horizontal movement
    step = 0.05;
    double x = x0;
    while (x != x1) {
      disc.setPosition (x, y);
      disc.redraw();
      window_.refresh();
      x += (x1 > x0 ? step : -step);
      if (Math.abs (x - x10.01x = x1;
    }
    
    // Animate vertical down movement
    step = 0.2;
    y  = y1;
    y1 = pegs_[destination].getNChildren() 1;
    while (y > y1) {
      if (Math.abs (y - y10.01y = y1;
      disc.setPosition (x, y);
      disc.redraw();
      window_.refresh();
      y -= step;
    }
  }
  

  
  /**
   * Graphics object for canvas title.
   */
  class Title extends GObject
  {
    private GSegment  anchor_;
    
    public Title()
    {
      GStyle style = new GStyle();
      style.setLineStyle (GStyle.LINESTYLE_INVISIBLE);
      style.setForegroundColor (new Color (100100200));
      style.setFont (new Font ("serif", Font.PLAIN, 36));
      setStyle (style);

      anchor_ = new GSegment();
      addSegment (anchor_);
      
      GText text = new GText ("Towers of Hanoi", GPosition.SOUTHEAST);
      anchor_.setText (text);
    }
    
    
    public void draw()
    {
      anchor_.setGeometry (2020);
    }
  }
  

  
  /**
   * Graphics representation of a peg.
   */
  class Peg extends GObject
  {
    private double    x_;
    private GSegment  peg_;
    private double[]  xy_;
    
    
    public Peg (double x)
    {
      x_ = x;

      GStyle style = new GStyle();
      style.setBackgroundColor (new Color (100100100));
      setStyle (style);

      peg_ = new GSegment();
      addSegment (peg_);

      xy_ = new double[] {x_ - 0.050.0,
                          x_ - 0.05, nDiscs_ + 2,
                          x_ + 0.05, nDiscs_ + 2,
                          x_ + 0.050.0,
                          x_ - 0.050.0};
    }


    public double getX()
    {
      return x_;
    }
    

    public void draw()
    {
      peg_.setGeometryXy (xy_);
    }
  }


  
  /**
   * Graphics representation of a disc.
   */
  class Disc extends GObject
  {
    private double    size_;
    private GSegment  disc_;
    private double    x_, y_;
    

    public Disc (double size)
    {
      size_ = size;
      
      GStyle style = new GStyle();
      style.setForegroundColor (new Color (25500));
      style.setBackgroundColor (new Color (255150150));
      setStyle (style);
      
      disc_ = new GSegment();
      addSegment (disc_);
    }


    public void setPosition (double x, double y)
    {
      x_ = x;
      y_ = y;
    }


    public double getY()
    {
      return y_;
    }
    
    
    public void draw()
    {
      double[] xy = new double[] {x_ - size_ / 2.0, y_,
                                  x_ - size_ / 2.0, y_ + 1.0,
                                  x_ + size_ / 2.0, y_ + 1.0,
                                  x_ + size_ / 2.0, y_,
                                  x_ - size_ / 2.0, y_};

      disc_.setGeometryXy (xy);
    }
  }
  


  
  /**
   * Class for solving the "Towers of Hanoi" puzzle.
   */   
  class TowersOfHanoi
  {
    public void solve()
    {
      solve (nDiscs_, 021);    
    }
    
    
    private void solve (int nDiscs, int source, int destination, int auxiliary)
    {
      if (nDiscs == 1)
        discMoved (source, destination);
      
      else if (nDiscs > 1) {
        solve (nDiscs - 1, source, auxiliary, destination);
        discMoved (source, destination);
        solve (nDiscs - 1, auxiliary, destination, source);
      }
    }
  }
  
  

  public static void main (String[] args)
  {
    int nDiscs = 8;
    Demo14 demo = new Demo14 (nDiscs);
  }
}

           
         
    
    
    
    
  
G-Towers-of-Hanoi.zip( 203 k)
Related examples in the same category
1.Is Event Dispatcher ThreadIs Event Dispatcher Thread
2.Timer based animation
3.A rotating and scaling rectangle.
4.Fade out an image: image gradually get more transparent until it is completely invisible.
5.Font size animation
6.Hypnosis animationHypnosis animation
7.Noise ImageNoise Image
8.How to create Animation: Paint and threadHow to create Animation: Paint and thread
9.How to create animationHow to create animation
10.Animation: bounce
11.Image BouncerImage Bouncer
12.Text animationText animation
13.Buffered Animation DemoBuffered Animation Demo
14.Bouncing CircleBouncing Circle
15.Hypnosis SpiralHypnosis Spiral
16.Animator DemoAnimator Demo
17.Make your own animation from a series of images
18.Composition technique in this animation.
19.Animated Button
20.Animated Message Panel
21.Animated PasswordField
22.Animated TextField
23.A simple spring simulation in one dimension
24.Shows an animated bouncing ball
25.Shows animated bouncing ballsShows animated bouncing balls
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.