Style inheritance : Tree « Advanced Graphics « Java

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. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
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 
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:[email protected]">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 Hiearchy DemoObject Hiearchy Demo
4. Custom Tree LayoutCustom Tree Layout
ww_w___.___j__a__v_a2__s__.___c_o___m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.