KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > example > fundamental > LevelOfDetail


1 /*
2  * LevelOfDetail.java
3  *
4  * Created on Mar 17, 2006 at 4:43:59 PM
5  */

6 package com.thoughtriver.open.vectorvisuals.example.fundamental;
7
8 import java.awt.*;
9 import java.awt.geom.*;
10 import java.util.*;
11
12 import javax.swing.*;
13
14 import com.thoughtriver.open.vectorvisuals.*;
15 import com.thoughtriver.open.vectorvisuals.ui.*;
16
17 /**
18  * This example illustrates the use of the "Level of Detail" (LOD) system used
19  * by Vector Visuals.
20  *
21  * @author Brandon Franklin
22  * @version $Date: 2006/11/25 09:35:06 $
23  */

24 public class LevelOfDetail implements Runnable JavaDoc {
25
26     /** Randomizer used by this example */
27     private final Random random = new Random();
28
29     /** The <CODE>VVDisplay</CODE> used to display the test */
30     private VVDisplay vvDisplay = null;
31
32     /**
33      * Creates a new instance of <CODE>LevelOfDetail</CODE>.
34      */

35     public LevelOfDetail() {
36         VVPanel panel = new VVPanel();
37         vvDisplay = new VVDisplay(panel);
38     }
39
40     /**
41      * Starts the example.
42      */

43     public void run() {
44         JFrame frame = new JFrame("Vector Visuals Level of Detail Example");
45         frame.setSize(640, 480);
46         frame.getContentPane().setLayout(new BorderLayout());
47         frame.getContentPane().add(vvDisplay.getViewPane(), BorderLayout.CENTER);
48         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49
50         frame.setVisible(true);
51
52         // Set up the panel itself
53
vvDisplay.getViewPane().setBackground(Color.DARK_GRAY);
54         vvDisplay.setWorldViewScale(1);
55
56         // Create a whole bunch of random objects
57
for (int i = 0; i < 300; i++) {
58             VisualObject visObj = createRandomObject();
59             vvDisplay.addObject(visObj);
60         }
61
62         // Scatter some random text around to notate the scale
63
for (int i = 0; i < 50; i++) {
64             VisualObject visObj = createRandomTextObject();
65             vvDisplay.addObject(visObj);
66         }
67
68         // Add a specialized mouse listener
69
VVMouseDragListener listener = new VVMouseDragListener(vvDisplay);
70         vvDisplay.addVVMouseListener(listener);
71
72         // Ensure that the viewport itself is draggable and zoomable
73
listener.setViewportDraggable(true);
74         listener.setViewportZoomable(true);
75
76     }
77
78     /**
79      * Creates a new random <CODE>VisualObject</CODE>.
80      *
81      * @return the random <CODE>VisualObject</CODE> created
82      */

83     private VisualObject createRandomObject() {
84         Brush lineBrush = new Brush(new Color(random.nextInt()), null, null);
85         Brush fillBrush = new Brush(new Color(random.nextInt()), null, null);
86
87         VisualObject visObj = new VisualObject(new Rectangle2D.Double(0, 0, random.nextInt(200), random.nextInt(200)), lineBrush, fillBrush);
88         AffineTransform transform = AffineTransform.getTranslateInstance(random.nextInt(4000) - 2000, random.nextInt(4000) - 2000);
89         visObj.setTransform(transform);
90
91         Rectangle2D bounds = visObj.getShape().getBounds2D();
92         for (int i = 0; i < 10; i++) {
93             VisualObject subVisObj = createRandomInnerObject(bounds.getWidth(), bounds.getHeight());
94             visObj.add(subVisObj);
95             subVisObj.setLayer(-1);
96         }
97
98         visObj.addLevelOfDetail(0.075, 1);
99         visObj.addLevelOfDetail(0.01, 0);
100         return visObj;
101     }
102
103     /**
104      * Creates a new random <CODE>VisualObject</CODE>.
105      *
106      * @param xRange the possible X range in which to position the new object
107      * @param yRange the possible Y range in which to position the new object
108      * @return the random <CODE>VisualObject</CODE> created
109      */

110     private VisualObject createRandomInnerObject(final double xRange, final double yRange) {
111         Brush lineBrush = new Brush(new Color(random.nextInt()), null, null);
112         Brush fillBrush = new Brush(new Color(random.nextInt()), null, null);
113
114         VisualObject visObj = new VisualObject((random.nextBoolean() ? new Rectangle2D.Double(0, 0, random.nextInt(200), random.nextInt(200)) : new Ellipse2D.Double(0, 0, random.nextInt(200), random.nextInt(200))), lineBrush, fillBrush);
115         AffineTransform transform = AffineTransform.getTranslateInstance(random.nextDouble()
116                 * xRange, random.nextDouble() * yRange);
117         transform.scale(0.1, 0.1);
118         visObj.setTransform(transform);
119
120         visObj.addLevelOfDetail(0.075, 1);
121         visObj.addLevelOfDetail(0.01, 0);
122         return visObj;
123     }
124
125     /**
126      * Creates a new random <CODE>VisualTextObject</CODE>.
127      *
128      * @return the new <CODE>VisualTextObject</CODE>
129      */

130     private VisualTextObject createRandomTextObject() {
131         Brush lineBrush = new Brush(Color.WHITE, null, new BasicStroke((float) 0.2));
132         Brush fillBrush = new Brush(Color.YELLOW, null, null);
133
134         Font font = FontManager.getSharedInstance().getFont("SANS SERIF-12");
135         AffineTransform transform = AffineTransform.getTranslateInstance(random.nextInt(4000) - 2000, random.nextInt(4000) - 2000);
136         double scale = random.nextInt(10);
137         transform.scale(scale, scale);
138
139         VisualTextObject visObj = new VisualTextObject("scale " + (int) scale, font, lineBrush, fillBrush);
140         visObj.setTransform(transform);
141
142         visObj.addLevelOfDetail(0.2, 1);
143         visObj.addLevelOfDetail(0.01, 0);
144         return visObj;
145     }
146
147     /**
148      * Instantiates a <CODE>LevelOfDetail</CODE> and executes it.
149      *
150      * @param args the command line arguments, ignored here
151      */

152     public static void main(final String JavaDoc[] args) {
153         Runnable JavaDoc example = new LevelOfDetail();
154         example.run();
155     }
156 }
157
Popular Tags