DrawablesDemo.java |
1 /**************************************************************************** 2 * This demo file is part of yFiles for Java 2.14. 3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28, 4 * 72070 Tuebingen, Germany. All rights reserved. 5 * 6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution 7 * of demo files in source code or binary form, with or without 8 * modification, is not permitted. 9 * 10 * Owners of a valid software license for a yFiles for Java version that this 11 * demo is shipped with are allowed to use the demo source code as basis 12 * for their own yFiles for Java powered applications. Use of such programs is 13 * governed by the rights and conditions as set out in the yFiles for Java 14 * license agreement. 15 * 16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 ***************************************************************************/ 28 package demo.view.rendering; 29 30 import demo.view.DemoBase; 31 import demo.view.DemoDefaults; 32 33 import y.view.Drawable; 34 import y.view.Graph2D; 35 36 import java.awt.Color; 37 import java.awt.Graphics2D; 38 import java.awt.Rectangle; 39 import java.awt.EventQueue; 40 import java.util.Locale; 41 42 /** 43 * This demo shows how to add objects of type {@link Drawable} to 44 * a {@link y.view.Graph2DView} and how to implement such a 45 * <code>Drawable</code> object. 46 * <code>Drawable</code>s represent graphical objects that can be displayed 47 * by a <code>Graph2DView</code>. The main purpose of <code>Drawable</code>s is 48 * to highlight certain regions of the displayed graph. 49 * <br> 50 * The <code>Drawable</code> implemented in this demo draws itself as a box 51 * drawn underneath the displayed graph. The size and location of the box changes 52 * dynamically as the bounding box of the graph changes. 53 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/mvc_view#drawables" target="_blank">Section View Implementations</a> in the yFiles for Java Developer's Guide 54 */ 55 public class DrawablesDemo extends DemoBase { 56 public DrawablesDemo() { 57 //add the drawable 58 view.addBackgroundDrawable( new BoundingBox() ); 59 60 createInitialGraph(); 61 view.fitContent(); 62 } 63 64 protected void createInitialGraph() { 65 Graph2D graph = view.getGraph2D(); 66 graph.createEdge(graph.createNode(100,100,"1"), graph.createEdge(graph.createNode(100,200,"2"), graph.createNode(200,100,"3")).source()); 67 68 69 } 70 71 /** 72 * Represents a graphical bounding box of the displayed graph. 73 */ 74 class BoundingBox implements Drawable { 75 public Rectangle getBounds() { 76 Rectangle r = view.getGraph2D().getBoundingBox(); 77 if ( r.getWidth() > 0.0 ) { 78 r.setFrame( r.getX() - 30, r.getY() - 30, r.getWidth() + 60, r.getHeight() + 60 ); 79 } 80 return r; 81 } 82 83 public void paint( Graphics2D gfx ) { 84 Rectangle r = getBounds(); 85 gfx.setColor( DemoDefaults.DEFAULT_CONTRAST_COLOR ); 86 gfx.fill( r ); 87 gfx.setColor( Color.black ); 88 gfx.draw( r ); 89 } 90 } 91 92 public static void main(String[] args) { 93 EventQueue.invokeLater(new Runnable() { 94 public void run() { 95 Locale.setDefault(Locale.ENGLISH); 96 initLnF(); 97 (new DrawablesDemo()).start("Drawables Demo"); 98 } 99 }); 100 } 101 102 103 } 104 105 106 107 108