| DrawablesDemo.java |
1 /****************************************************************************
2 **
3 ** This file is part of yFiles-2.6.
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-2008 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 y.view.Drawable;
18
19 import java.awt.Color;
20 import java.awt.Graphics2D;
21 import java.awt.Rectangle;
22
23 /**
24 * This demo shows how to add objects of type Drawable to
25 * a Graph2DView and how to implement such a Drawable object.
26 * Drawables represent graphical objects that can be displayed
27 * by a Graph2DView. The main purpose of Drawables is to highlight
28 * certain regions of the displayed graph.
29 * <br>
30 * The Drawable implemented in this demo draws itself as a red box
31 * drawn underneath the displayed graph. The size and location of the box changes
32 * dynamically as the bounding box of the graph changes.
33 */
34 public class DrawablesDemo extends DemoBase {
35 public DrawablesDemo() {
36 //add the drawable
37 view.addBackgroundDrawable( new BoundingBox() );
38 loadGraph( "resource/drawablesDemo.gml" );
39 }
40
41 /**
42 * Represents a graphical bounding box of the displayed graph.
43 */
44 class BoundingBox implements Drawable {
45 public Rectangle getBounds() {
46 Rectangle r = view.getGraph2D().getBoundingBox();
47 if ( r.getWidth() > 0.0 ) {
48 r.setFrame( r.getX() - 30, r.getY() - 30, r.getWidth() + 60, r.getHeight() + 60 );
49 }
50 return r;
51 }
52
53 public void paint( Graphics2D gfx ) {
54 Rectangle r = getBounds();
55 gfx.setColor( Color.red );
56 gfx.fill( r );
57 gfx.setColor( Color.black );
58 gfx.draw( r );
59 }
60 }
61
62 public static void main( String args[] ) {
63 DrawablesDemo demo = new DrawablesDemo();
64 demo.start( "Drawables Demo" );
65 }
66
67
68 }
69
70
71
72
73