1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.rendering;
15  
16  import demo.view.DemoBase;
17  import demo.view.DemoDefaults;
18  
19  import y.view.Drawable;
20  import y.view.Graph2D;
21  
22  import java.awt.Color;
23  import java.awt.Graphics2D;
24  import java.awt.Rectangle;
25  import java.awt.EventQueue;
26  import java.util.Locale;
27  
28  /**
29   * This demo shows how to add objects of type {@link Drawable} to 
30   * a {@link y.view.Graph2DView} and how to implement such a
31   * <code>Drawable</code> object.
32   * <code>Drawable</code>s represent graphical objects that can be displayed 
33   * by a <code>Graph2DView</code>. The main purpose of <code>Drawable</code>s is
34   * to highlight certain regions of the displayed graph.
35   * <br>
36   * The <code>Drawable</code> implemented in this demo draws itself as a box 
37   * drawn underneath the displayed graph. The size and location of the box changes 
38   * dynamically as the bounding box of the graph changes.
39   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/mvc_view.html#drawables">Section View Implementations</a> in the yFiles for Java Developer's Guide
40   */
41  public class DrawablesDemo extends DemoBase {
42    public DrawablesDemo() {
43      //add the drawable
44      view.addBackgroundDrawable( new BoundingBox() );
45      
46      createInitialGraph();
47      view.fitContent();
48    }
49  
50    protected void createInitialGraph() {
51      Graph2D graph = view.getGraph2D();
52      graph.createEdge(graph.createNode(100,100,"1"), graph.createEdge(graph.createNode(100,200,"2"), graph.createNode(200,100,"3")).source());
53      
54      
55    }
56  
57    /**
58     * Represents a graphical bounding box of the displayed graph.
59     */
60    class BoundingBox implements Drawable {
61      public Rectangle getBounds() {
62        Rectangle r = view.getGraph2D().getBoundingBox();
63        if ( r.getWidth() > 0.0 ) {
64          r.setFrame( r.getX() - 30, r.getY() - 30, r.getWidth() + 60, r.getHeight() + 60 );
65        }
66        return r;
67      }
68  
69      public void paint( Graphics2D gfx ) {
70        Rectangle r = getBounds();
71        gfx.setColor( DemoDefaults.DEFAULT_CONTRAST_COLOR );
72        gfx.fill( r );
73        gfx.setColor( Color.black );
74        gfx.draw( r );
75      }
76    }
77  
78    public static void main(String[] args) {
79      EventQueue.invokeLater(new Runnable() {
80        public void run() {
81          Locale.setDefault(Locale.ENGLISH);
82          initLnF();
83          (new DrawablesDemo()).start("Drawables Demo");
84        }
85      });
86    }
87  
88  
89  }
90  
91      
92  
93        
94