1 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 24 public class LevelOfDetail implements Runnable { 25 26 27 private final Random random = new Random(); 28 29 30 private VVDisplay vvDisplay = null; 31 32 35 public LevelOfDetail() { 36 VVPanel panel = new VVPanel(); 37 vvDisplay = new VVDisplay(panel); 38 } 39 40 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 vvDisplay.getViewPane().setBackground(Color.DARK_GRAY); 54 vvDisplay.setWorldViewScale(1); 55 56 for (int i = 0; i < 300; i++) { 58 VisualObject visObj = createRandomObject(); 59 vvDisplay.addObject(visObj); 60 } 61 62 for (int i = 0; i < 50; i++) { 64 VisualObject visObj = createRandomTextObject(); 65 vvDisplay.addObject(visObj); 66 } 67 68 VVMouseDragListener listener = new VVMouseDragListener(vvDisplay); 70 vvDisplay.addVVMouseListener(listener); 71 72 listener.setViewportDraggable(true); 74 listener.setViewportZoomable(true); 75 76 } 77 78 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 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 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 152 public static void main(final String [] args) { 153 Runnable example = new LevelOfDetail(); 154 example.run(); 155 } 156 } 157 | Popular Tags |