Object Reparent : Tree : Advanced Graphics : Java examples (example source code) Organized by topic

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Advanced Graphics » TreeScreenshots 
Object Reparent

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:[email protected]">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-Object-Reparent.zip( 203 k)
Related examples in the same category
1. Style inheritanceStyle inheritance
2. Graph Tree ModelGraph Tree Model
3. Object Hiearchy DemoObject Hiearchy Demo
4. Custom Tree LayoutCustom Tree Layout
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.