Style inheritance : Tree « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Advanced Graphics » TreeScreenshots 
Style inheritance
Style inheritance

import java.awt.*;

import javax.swing.*;

import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>Nested GObject hierarchy
 * <li>Style inheritance
 * <li>Simple selection interaction
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo8 extends JFrame
  implements GInteraction
{
  private GScene  scene_;
  
  
  public Demo8()
  {
    super ("G Graphics Library - Demo 8");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the GUI
    JPanel topLevel = new JPanel();
    topLevel.setLayout (new BorderLayout());
    getContentPane().add (topLevel);        

    JPanel buttonPanel = new JPanel();
    buttonPanel.add (new JLabel ("Button 1 to select color, button 2 to unselect"));

    JPanel graphicsPanel = new JPanel();
    topLevel.add (buttonPanel,   BorderLayout.NORTH);

    // Create the graphic canvas
    GWindow window = new GWindow();
    topLevel.add (window.getCanvas(), BorderLayout.CENTER);    
    
    // Create scene with default viewport and world extent settings
    scene_ = new GScene (window, "Scene");

    double w0[] {0.0,    1200.00.0};
    double w1[] {1200.01200.00.0};
    double w2[] {0.0,       0.00.0};    
    scene_.setWorldExtent (w0, w1, w2);

    GStyle style = new GStyle();
    style.setForegroundColor (new Color (000));
    style.setBackgroundColor (new Color (255255255));
    style.setFont (new Font ("Dialog", Font.BOLD, 14));
    scene_.setStyle (style);
    
    // Create som graphic objects
    GObject object1 = new TestObject ("1", scene_,  500.0100.0);

    GObject object2  = new TestObject ("2",  object1, 250.0250.0);
    GObject object3  = new TestObject ("3",  object1, 500.0250.0);
    GObject object4  = new TestObject ("4",  object1, 625.0250.0);

    GObject object5  = new TestObject ("5",  object2, 150.0400.0);
    GObject object6  = new TestObject ("6",  object2, 250.0400.0);        
    GObject object7  = new TestObject ("7",  object2, 350.0400.0);            

    GObject object8  = new TestObject ("8",  object4, 625.0400.0);

    GObject object9  = new TestObject ("9",  object7, 250.0550.0);
    GObject object10 = new TestObject ("10", object7, 350.0550.0);    

    GObject object11 = new TestObject ("11", object8, 475.0550.0);
    GObject object12 = new TestObject ("12", object8, 600.0550.0);
    GObject object13 = new TestObject ("13", object8, 725.0550.0);
    GObject object14 = new TestObject ("14", object8, 850.0550.0);            

    GObject object15 = new TestObject ("15", object9, 150.0700.0);
    GObject object16 = new TestObject ("16", object9, 250.0700.0);
    GObject object17 = new TestObject ("17", object9, 350.0700.0);

    GObject object18 = new TestObject ("18", object13, 725.0700.0);            

    GObject object19 = new TestObject ("19", object17, 350.0850.0);

    GObject object20 = new TestObject ("20", object19, 250.01000.0);
    GObject object21 = new TestObject ("21", object19, 350.01000.0);
    GObject object22 = new TestObject ("22", object19, 450.01000.0);        

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

    window.startInteraction (this);
  }


  
  public void event (GScene scene, int event, int x, int y)
  {
    if (event == GWindow.BUTTON1_UP ||
        event == GWindow.BUTTON2_UP) {
      boolean isSelected = event == GWindow.BUTTON1_UP;

      GSegment selectedSegment = scene_.findSegment (x, y);
      if (selectedSegment == nullreturn;
      
      GStyle style = selectedSegment.getOwner().getStyle();
      if (style == nullreturn;

      if (isSelected)
        style.setBackgroundColor (new Color ((floatMath.random(),
                                             (floatMath.random(),
                                             (floatMath.random()));
      else
        style.unsetBackgroundColor();
        
      scene_.refresh();
    }
  }
  
  

  private class TestObject extends GObject
  {
    private TestObject  parent_;
    private double      x_, y_;
    private GSegment    circle_;
    private GSegment    line_;

    
    TestObject (String name, GObject parent, double x, double y)
    {
      parent_ = parent instanceof TestObject ? (TestObjectparent : null;
      
      x_ = x;
      y_ = y;

      line_ = new GSegment();
      addSegment (line_);
      
      circle_ = new GSegment();
      addSegment (circle_);

      circle_.setText (new GText (name, GPosition.MIDDLE));

      setStyle (new GStyle());
      
      parent.add (this);
    }

    
    double getX()
    {
      return x_;
    }


    double getY()
    {
      return y_;
    }
    
    
    
    public void draw()
    {
      if (parent_ != null
        line_.setGeometry (parent_.getX(), parent_.getY(), x_, y_);
      
      circle_.setGeometryXy (Geometry.createCircle (x_, y_, 40.0));
    }
  }
  


  public static void main (String[] args)
  {
    new Demo8();
  }
}

           
       
G-StyleInheritance.zip( 218 k)
Related examples in the same category
1. Graph Tree ModelGraph Tree Model
2. Object Reparent
3. Object HierarchyDemoObject HierarchyDemo
4. Custom Tree LayoutCustom Tree Layout
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.