| BackgroundDemo.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 y.option.EnumOptionItem;
32 import y.option.MappedListCellRenderer;
33 import y.option.OptionHandler;
34 import y.view.DefaultBackgroundRenderer;
35 import y.view.EdgeRealizer;
36 import y.view.LineType;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.DefaultListCellRenderer;
41 import javax.swing.JToolBar;
42 import java.awt.Color;
43 import java.awt.Dimension;
44 import java.awt.EventQueue;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.util.HashMap;
48 import java.util.Locale;
49 import java.util.Map;
50
51 /**
52 * Demonstrates different modes for drawing images in the
53 * background of Graph2DView.
54 * <p>
55 * <b>Usage:</b> Create some nodes and try the different background settings available
56 * via the toolbar button. Try changing the window size, zooming in and out and
57 * moving the view port by right dragging.
58 * </p><p>
59 * <b>Third Party Licenses:</b><br/>
60 * The USA map that is used as background in this demo is based on
61 * <a href="http://commons.wikimedia.org/wiki/File:Blank_US_Map.svg">Blank_US_Map.svg by Theshibboleth</a>
62 * and licensed under the
63 * <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported</a>
64 * license.
65 * </p>
66 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/mvc_view#render_order" target="_blank">Section View Implementations</a> in the yFiles for Java Developer's Guide
67 */
68 public class BackgroundDemo extends DemoBase
69 {
70 DefaultBackgroundRenderer renderer;
71
72 static final String[] bgImages = {
73 "resource/yWorksBig.png",
74 "resource/yWorksSmall.gif",
75 "resource/usamap.png",
76 "resource/ySplash.jpg",
77 "resource/tile.png",
78 "<NONE>"
79 };
80
81
82 public BackgroundDemo()
83 {
84 renderer = new DefaultBackgroundRenderer(view);
85 renderer.setImageResource(getSharedResource(bgImages[0]));
86 renderer.setMode(DefaultBackgroundRenderer.CENTERED);
87 renderer.setColor(Color.white);
88 view.setBackgroundRenderer(renderer);
89 view.setPreferredSize(new Dimension(600,400));
90
91 view.setWorldRect(0,0,1000,1000);
92
93 //use thicker edges
94 EdgeRealizer er = view.getGraph2D().getDefaultEdgeRealizer();
95 er.setLineType(LineType.LINE_2);
96 }
97
98 /**
99 * Returns ViewActionDemo toolbar plus a button to change the
100 * background of the view.
101 */
102 protected JToolBar createToolBar()
103 {
104 JToolBar bar = super.createToolBar();
105 bar.addSeparator();
106 bar.add(createActionControl(new ChangeBackground()));
107 return bar;
108 }
109
110 /**
111 * An action that displays a dialog that allows to change the background
112 * properties of the view.
113 */
114 class ChangeBackground extends AbstractAction
115 {
116 /** The powerful yFiles dialog generator */
117 OptionHandler op;
118 Map xlate;
119
120 ChangeBackground()
121 {
122 super("Background");
123 xlate = new HashMap(11);
124 xlate.put(new Byte(DefaultBackgroundRenderer.FULLSCREEN),"Fullscreen");
125 xlate.put(new Byte(DefaultBackgroundRenderer.TILED), "Tiled");
126 xlate.put(new Byte(DefaultBackgroundRenderer.BRICKED), "Bricked");
127 xlate.put(new Byte(DefaultBackgroundRenderer.CENTERED), "Centered");
128 xlate.put(new Byte(DefaultBackgroundRenderer.PLAIN), "Plain");
129 xlate.put(new Byte(DefaultBackgroundRenderer.DYNAMIC), "Dynamic");
130
131 putValue(Action.SMALL_ICON, getIconResource("resource/properties.png"));
132 }
133
134 public void actionPerformed(ActionEvent e)
135 {
136 if(op == null)
137 {
138 op = new OptionHandler("Background");
139 op.addEnum("Mode",
140 xlate.keySet().toArray(),
141 new Byte(renderer.getMode()),
142 new MappedListCellRenderer(xlate));
143 op.addColor("Color",renderer.getColor());
144 op.addEnum("Image",bgImages,0)
145 // disable unwanted I18N
146 .setAttribute(EnumOptionItem.ATTRIBUTE_RENDERER,
147 new DefaultListCellRenderer());
148 }
149
150 final ActionListener actionListener = new ActionListener() {
151 public void actionPerformed(ActionEvent e) {
152 renderer.setMode(((Byte)op.get("Mode")).byteValue());
153 renderer.setColor((Color)op.get("Color"));
154 String imageSrc = op.getString("Image");
155 if("<NONE>".equals(imageSrc)) {
156 renderer.setImage(null);
157 } else {
158 renderer.setImageResource(getSharedResource(imageSrc));
159 }
160 view.updateView();
161 }
162 };
163
164 OptionSupport.showDialog(op, actionListener, true, view.getFrame());
165 }
166 }
167
168 public static void main(String[] args) {
169 EventQueue.invokeLater(new Runnable() {
170 public void run() {
171 Locale.setDefault(Locale.ENGLISH);
172 initLnF();
173 (new BackgroundDemo()).start("Background Demo");
174 }
175 });
176 }
177 }
178
179
180
181