View Javadoc

1   package org.textensor.vis;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Container;
6   import java.awt.Dimension;
7   import java.util.ArrayList;
8   import java.util.HashSet;
9   
10  import javax.swing.JFrame;
11  import javax.swing.JPanel;
12  
13  import org.textensor.report.E;
14  
15  
16  
17  public class Icing3DViewer implements Visualizer {
18  
19      double scaleFactor = 1.;
20  
21      JFrame frame;
22  
23      SceneGraphViewer sceneGraphViewer;
24  
25  
26      int resolution = Visualizer.MEDIUM;
27  
28      IcingPoint[] cachedPoints;
29  
30  
31      public Icing3DViewer() {
32          sceneGraphViewer = new SceneGraphViewer();
33  
34  
35          frame = new JFrame();
36          frame.setPreferredSize(new Dimension(400, 400));
37          Container ctr = frame.getContentPane();
38  
39  
40          ctr.setLayout(new BorderLayout());
41          ctr.add(sceneGraphViewer.getPanel(), BorderLayout.CENTER);
42  
43          frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
44  
45          buildViewable(makeDummyTree());
46          setLightsPercent(80);
47      }
48  
49  
50  
51      public void show() {
52          frame.pack();
53          frame.setVisible(true);
54      }
55  
56  
57  
58      public static void main(String[] argv) {
59          Icing3DViewer sv = new Icing3DViewer();
60          sv.show();
61      }
62  
63  
64  
65  
66      public IcingPoint[] makeDummyTree() {
67          ArrayList<IcingPoint> aip = new ArrayList<IcingPoint>();
68          IcingPoint p0 = new IcingPoint(0, 0, 0, 6);
69          IcingPoint p1 = new IcingPoint(50, 0, 0, 4, p0);
70          IcingPoint p2 = new IcingPoint(80, 0, 40, 2, p1);
71          IcingPoint p3 = new IcingPoint(70, 10, -30, 2, p1);
72          aip.add(p0);
73          aip.add(p1);
74          aip.add(p2);
75          aip.add(p3);
76  
77          IcingPoint[] ret = aip.toArray(new IcingPoint[aip.size()]);
78          return ret;
79      }
80  
81  
82      public JPanel getPanel() {
83          return sceneGraphViewer.getPanel();
84      }
85  
86      public void setScaleFactor(double d) {
87          scaleFactor = d;
88      }
89  
90  
91      public void buildViewable(Object obj) {
92          RunBuilder rb = new RunBuilder(this, obj);
93          Thread thread = new Thread(rb);
94          thread.start();
95      }
96  
97      public void reallyBuildVewable(Object obj) {
98          if (obj instanceof IcingPoint[]) {
99              IcingPoint[] points = (IcingPoint[])obj;
100             cachedPoints = points;
101             SceneGraphBuilder sgb  = new SceneGraphBuilder();
102             sgb.buildTree(points, resolution, scaleFactor);
103             sceneGraphViewer.removeAllDecoration();
104             sceneGraphViewer.setSceneGraph(sgb.getSceneGraph(), null);
105 
106         } else {
107             E.error("cant build viewable from " + obj);
108         }
109     }
110 
111     public void refreshDecoration(Object obj) {
112         RunDecorator rb = new RunDecorator(this, obj);
113         Thread thread = new Thread(rb);
114         thread.start();
115     }
116 
117 
118     public void reallyRefreshDecoration(Object obj) {
119 
120     }
121 
122 
123 
124     public void deltaLights(double d) {
125         sceneGraphViewer.deltaLights(d);
126 
127     }
128 
129 
130 
131     public void setAA(boolean b) {
132         sceneGraphViewer.setAA(b);
133 
134     }
135 
136 
137 
138     public void setResolution(int res) {
139         if (res != resolution) {
140             resolution = res;
141             if (cachedPoints != null) {
142                 SceneGraphBuilder sgb  = new SceneGraphBuilder();
143                 sgb.buildTree(cachedPoints, resolution, scaleFactor);
144                 sceneGraphViewer.setSceneGraph(sgb.getSceneGraph(), null);
145             }
146 
147 
148         }
149     }
150 
151     public Color makeColor(Object obj) {
152         Color ret = null;
153         if (obj instanceof Color) {
154             ret = (Color)obj;
155         } else if (obj instanceof String) {
156             String s = (String)obj;
157             if (s == null) {
158                 s = "0xff0000";
159             }
160             if (!s.toLowerCase().startsWith("0x")) {
161                 s = "0x" + s;
162             }
163             try {
164                 ret = new Color(Integer.decode(s).intValue());
165             } catch (Exception ex) {
166                 E.warning("dodgy color " + s);
167                 ret = Color.red;
168             }
169         } else {
170             ret = Color.cyan;
171         }
172         return ret;
173     }
174 
175 
176 
177     public void setLightsPercent(int p) {
178         sceneGraphViewer.setLightsPercent(p);
179     }
180 
181 
182 
183     public void setFourMatrix(double[] fmo) {
184         sceneGraphViewer.setFourMatrix(fmo);
185     }
186 
187 
188 
189     public double[] getFourMatrix() {
190         return sceneGraphViewer.getFourMatrix();
191     }
192 
193 }