1 | /* |
---|
2 | * $Id: JGridDemo.java,v 1.12 2003/09/16 19:05:41 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | package gov.noaa.pmel.sgt.demo; |
---|
13 | |
---|
14 | import gov.noaa.pmel.sgt.swing.JPlotLayout; |
---|
15 | import gov.noaa.pmel.sgt.swing.JClassTree; |
---|
16 | import gov.noaa.pmel.sgt.swing.prop.GridAttributeDialog; |
---|
17 | import gov.noaa.pmel.sgt.JPane; |
---|
18 | import gov.noaa.pmel.sgt.AbstractPane; |
---|
19 | import gov.noaa.pmel.sgt.GridAttribute; |
---|
20 | import gov.noaa.pmel.sgt.ContourLevels; |
---|
21 | import gov.noaa.pmel.sgt.CartesianRenderer; |
---|
22 | import gov.noaa.pmel.sgt.CartesianGraph; |
---|
23 | import gov.noaa.pmel.sgt.GridCartesianRenderer; |
---|
24 | import gov.noaa.pmel.sgt.IndexedColorMap; |
---|
25 | import gov.noaa.pmel.sgt.ColorMap; |
---|
26 | import gov.noaa.pmel.sgt.LinearTransform; |
---|
27 | import gov.noaa.pmel.sgt.dm.SGTData; |
---|
28 | |
---|
29 | |
---|
30 | import gov.noaa.pmel.util.GeoDate; |
---|
31 | import gov.noaa.pmel.util.TimeRange; |
---|
32 | import gov.noaa.pmel.util.Range2D; |
---|
33 | import gov.noaa.pmel.util.Dimension2D; |
---|
34 | import gov.noaa.pmel.util.Rectangle2D; |
---|
35 | import gov.noaa.pmel.util.Point2D; |
---|
36 | import gov.noaa.pmel.util.IllegalTimeValue; |
---|
37 | import gov.noaa.pmel.util.Debug; |
---|
38 | |
---|
39 | import java.awt.*; |
---|
40 | import java.awt.print.*; |
---|
41 | import java.awt.event.*; |
---|
42 | import javax.swing.*; |
---|
43 | /** |
---|
44 | * Example demonstrating how to use <code>JPlotLayout</code> |
---|
45 | * to create a raster-contour plot. |
---|
46 | * |
---|
47 | * @author Donald Denbo |
---|
48 | * @version $Revision: 1.12 $, $Date: 2003/09/16 19:05:41 $ |
---|
49 | * @since 2.0 |
---|
50 | */ |
---|
51 | public class JGridDemo extends JApplet { |
---|
52 | static JPlotLayout rpl_; |
---|
53 | private GridAttribute gridAttr_; |
---|
54 | JButton edit_; |
---|
55 | JButton space_ = null; |
---|
56 | JButton tree_; |
---|
57 | JButton print_; |
---|
58 | JButton layout_; |
---|
59 | PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage(); |
---|
60 | |
---|
61 | public void init() { |
---|
62 | /* |
---|
63 | * Create the demo in the JApplet environment. |
---|
64 | */ |
---|
65 | getContentPane().setLayout(new BorderLayout(0,0)); |
---|
66 | setBackground(java.awt.Color.white); |
---|
67 | setSize(600,550); |
---|
68 | JPanel main = new JPanel(); |
---|
69 | rpl_ = makeGraph(); |
---|
70 | JPanel button = makeButtonPanel(false); |
---|
71 | rpl_.setBatch(true); |
---|
72 | main.add(rpl_, BorderLayout.CENTER); |
---|
73 | JPane gridKeyPane = rpl_.getKeyPane(); |
---|
74 | gridKeyPane.setSize(new Dimension(600,100)); |
---|
75 | main.add(gridKeyPane, BorderLayout.SOUTH); |
---|
76 | getContentPane().add(main, "Center"); |
---|
77 | getContentPane().add(button, "South"); |
---|
78 | rpl_.setBatch(false); |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | JPanel makeButtonPanel(boolean mark) { |
---|
83 | MyAction myAction = new MyAction(); |
---|
84 | JPanel button = new JPanel(); |
---|
85 | button.setLayout(new FlowLayout()); |
---|
86 | print_ = new JButton("Print..."); |
---|
87 | print_.addActionListener(myAction); |
---|
88 | button.add(print_); |
---|
89 | layout_ = new JButton("Page Layout..."); |
---|
90 | layout_.addActionListener(myAction); |
---|
91 | button.add(layout_); |
---|
92 | tree_ = new JButton("Tree View"); |
---|
93 | tree_.addActionListener(myAction); |
---|
94 | button.add(tree_); |
---|
95 | edit_ = new JButton("Edit GridAttribute"); |
---|
96 | edit_.addActionListener(myAction); |
---|
97 | button.add(edit_); |
---|
98 | /* |
---|
99 | * Optionally leave the "mark" button out of the button panel |
---|
100 | */ |
---|
101 | if(mark) { |
---|
102 | space_ = new JButton("Add Mark"); |
---|
103 | space_.addActionListener(myAction); |
---|
104 | button.add(space_); |
---|
105 | } |
---|
106 | return button; |
---|
107 | } |
---|
108 | public static void main(String[] args) { |
---|
109 | /* |
---|
110 | * Create the demo as an application |
---|
111 | */ |
---|
112 | JGridDemo gd = new JGridDemo(); |
---|
113 | /* |
---|
114 | * Create a new JFrame to contain the demo. |
---|
115 | */ |
---|
116 | JFrame frame = new JFrame("Grid Demo"); |
---|
117 | JPanel main = new JPanel(); |
---|
118 | main.setLayout(new BorderLayout()); |
---|
119 | frame.setSize(600,500); |
---|
120 | frame.getContentPane().setLayout(new BorderLayout()); |
---|
121 | /* |
---|
122 | * Listen for windowClosing events and dispose of JFrame |
---|
123 | */ |
---|
124 | frame.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
125 | public void windowClosing(java.awt.event.WindowEvent event) { |
---|
126 | JFrame fr = (JFrame)event.getSource(); |
---|
127 | fr.setVisible(false); |
---|
128 | fr.dispose(); |
---|
129 | System.exit(0); |
---|
130 | } |
---|
131 | public void windowOpened(java.awt.event.WindowEvent event) { |
---|
132 | rpl_.getKeyPane().draw(); |
---|
133 | } |
---|
134 | }); |
---|
135 | /* |
---|
136 | * Create button panel with "mark" button |
---|
137 | */ |
---|
138 | JPanel button = gd.makeButtonPanel(true); |
---|
139 | /* |
---|
140 | * Create JPlotLayout and turn batching on. With batching on the |
---|
141 | * plot will not be updated as components are modified or added to |
---|
142 | * the plot tree. |
---|
143 | */ |
---|
144 | rpl_ = gd.makeGraph(); |
---|
145 | rpl_.setBatch(true); |
---|
146 | /* |
---|
147 | * Layout the plot, key, and buttons. |
---|
148 | */ |
---|
149 | main.add(rpl_, BorderLayout.CENTER); |
---|
150 | JPane gridKeyPane = rpl_.getKeyPane(); |
---|
151 | gridKeyPane.setBatch(true); // new line |
---|
152 | gridKeyPane.setSize(new Dimension(600,100)); |
---|
153 | rpl_.setKeyLayerSizeP(new Dimension2D(6.0, 1.0)); |
---|
154 | rpl_.setKeyBoundsP(new Rectangle2D.Double(0.0, 1.0, 6.0, 1.0)); |
---|
155 | main.add(gridKeyPane, BorderLayout.SOUTH); |
---|
156 | frame.getContentPane().add(main, BorderLayout.CENTER); |
---|
157 | frame.getContentPane().add(button, BorderLayout.SOUTH); |
---|
158 | |
---|
159 | JPanel foo = null; |
---|
160 | JPanel bar = null; |
---|
161 | JButton foobutton = null; |
---|
162 | JButton barbutton = null; |
---|
163 | |
---|
164 | /* JPanel foo = new JPanel(); |
---|
165 | foo.setLayout(new BorderLayout()); |
---|
166 | JButton foobutton = new JButton("my foo button"); |
---|
167 | foo.add(foobutton, BorderLayout.CENTER); |
---|
168 | foo.setMaximumSize(new Dimension(700, 100)); |
---|
169 | foo.setMinimumSize(new Dimension(600, 100)); |
---|
170 | foo.setPreferredSize(new Dimension(600, 100)); |
---|
171 | frame.getContentPane().add(foo, BorderLayout.NORTH); |
---|
172 | JPanel bar = new JPanel(); |
---|
173 | bar.setLayout(new BorderLayout()); |
---|
174 | JButton barbutton = new JButton("my bar button"); |
---|
175 | bar.add(barbutton, BorderLayout.CENTER); |
---|
176 | bar.setMaximumSize(new Dimension(150, 2000)); |
---|
177 | bar.setMinimumSize(new Dimension(150, 100)); |
---|
178 | bar.setPreferredSize(new Dimension(150, 200)); |
---|
179 | frame.getContentPane().add(bar, BorderLayout.WEST); */ |
---|
180 | |
---|
181 | frame.pack(); |
---|
182 | frame.setVisible(true); |
---|
183 | /* |
---|
184 | * Turn batching off. JPlotLayout will redraw if it has been |
---|
185 | * modified since batching was turned on. |
---|
186 | */ |
---|
187 | gridKeyPane.setBatch(false); // new line |
---|
188 | rpl_.setBatch(false); |
---|
189 | |
---|
190 | if(Debug.DEBUG) { |
---|
191 | System.out.println("Locations:"); |
---|
192 | System.out.println(" foo = " + foo.getLocation()); |
---|
193 | System.out.println(" foobutton = " + foobutton.getLocation()); |
---|
194 | System.out.println(" bar = " + bar.getLocation()); |
---|
195 | System.out.println(" barbutton = " + barbutton.getLocation()); |
---|
196 | System.out.println(" main = " + main.getLocation()); |
---|
197 | System.out.println(" rpl_ = " + rpl_.getLocation()); |
---|
198 | System.out.println(" gridKeyPane = " + gridKeyPane.getLocation()); |
---|
199 | System.out.println(" button = " + button.getLocation()); |
---|
200 | System.out.println(" print_ = " + gd.print_.getLocation()); |
---|
201 | System.out.println(" space_ = " + gd.space_.getLocation()); |
---|
202 | System.out.println(" tree_ = " + gd.tree_.getLocation()); |
---|
203 | System.out.println(" edit_ = " + gd.edit_.getLocation()); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | void edit_actionPerformed(java.awt.event.ActionEvent e) { |
---|
208 | /* |
---|
209 | * Create a GridAttributeDialog and set the renderer. |
---|
210 | */ |
---|
211 | GridAttributeDialog gad = new GridAttributeDialog(); |
---|
212 | gad.setJPane(rpl_); |
---|
213 | CartesianRenderer rend = ((CartesianGraph)rpl_.getFirstLayer().getGraph()).getRenderer(); |
---|
214 | gad.setGridCartesianRenderer((GridCartesianRenderer)rend); |
---|
215 | // gad.setGridAttribute(gridAttr_); |
---|
216 | gad.setVisible(true); |
---|
217 | } |
---|
218 | |
---|
219 | void tree_actionPerformed(java.awt.event.ActionEvent e) { |
---|
220 | /* |
---|
221 | * Create a JClassTree for the JPlotLayout objects |
---|
222 | */ |
---|
223 | JClassTree ct = new JClassTree(); |
---|
224 | ct.setModal(false); |
---|
225 | ct.setJPane(rpl_); |
---|
226 | ct.show(); |
---|
227 | } |
---|
228 | |
---|
229 | void print_actionPerformed(ActionEvent e) { |
---|
230 | Color saveColor; |
---|
231 | |
---|
232 | PrinterJob printJob = PrinterJob.getPrinterJob(); |
---|
233 | printJob.setPrintable(rpl_, pageFormat); |
---|
234 | printJob.setJobName("Grid Demo"); |
---|
235 | if(printJob.printDialog()) { |
---|
236 | try { |
---|
237 | saveColor = rpl_.getBackground(); |
---|
238 | if(!saveColor.equals(Color.white)) { |
---|
239 | rpl_.setBackground(Color.white); |
---|
240 | } |
---|
241 | rpl_.setPageAlign(AbstractPane.TOP, |
---|
242 | AbstractPane.CENTER); |
---|
243 | RepaintManager currentManager = RepaintManager.currentManager(rpl_); |
---|
244 | currentManager.setDoubleBufferingEnabled(false); |
---|
245 | printJob.print(); |
---|
246 | currentManager.setDoubleBufferingEnabled(true); |
---|
247 | rpl_.setBackground(saveColor); |
---|
248 | } catch (PrinterException pe) { |
---|
249 | System.out.println("Error printing: " + pe); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | } |
---|
254 | |
---|
255 | void layout_actionPerformed(ActionEvent e) { |
---|
256 | PrinterJob pj = PrinterJob.getPrinterJob(); |
---|
257 | pageFormat = pj.pageDialog(pageFormat); |
---|
258 | } |
---|
259 | |
---|
260 | JPlotLayout makeGraph() { |
---|
261 | /* |
---|
262 | * This example uses a pre-created "Layout" for raster time |
---|
263 | * series to simplify the construction of a plot. The |
---|
264 | * JPlotLayout can plot a single grid with |
---|
265 | * a ColorKey, time series with a LineKey, point collection with a |
---|
266 | * PointCollectionKey, and general X-Y plots with a |
---|
267 | * LineKey. JPlotLayout supports zooming, object selection, and |
---|
268 | * object editing. |
---|
269 | */ |
---|
270 | SGTData newData; |
---|
271 | TestData td; |
---|
272 | JPlotLayout rpl; |
---|
273 | ContourLevels clevels; |
---|
274 | /* |
---|
275 | * Create a test grid with sinasoidal-ramp data. |
---|
276 | */ |
---|
277 | Range2D xr = new Range2D(190.0f, 250.0f, 1.0f); |
---|
278 | Range2D yr = new Range2D(0.0f, 45.0f, 1.0f); |
---|
279 | td = new TestData(TestData.XY_GRID, xr, yr, |
---|
280 | TestData.SINE_RAMP, 12.0f, 30.f, 5.0f); |
---|
281 | newData = td.getSGTData(); |
---|
282 | /* |
---|
283 | * Create the layout without a Logo image and with the |
---|
284 | * ColorKey on a separate Pane object. |
---|
285 | */ |
---|
286 | rpl = new JPlotLayout(true, false, false, "JGridDemo Pane", null, true); |
---|
287 | rpl.setEditClasses(false); |
---|
288 | /* |
---|
289 | * Create a GridAttribute for CONTOUR style. |
---|
290 | */ |
---|
291 | Range2D datar = new Range2D(-20.0f, 45.0f, 5.0f); |
---|
292 | clevels = ContourLevels.getDefault(datar); |
---|
293 | gridAttr_ = new GridAttribute(clevels); |
---|
294 | /* |
---|
295 | * Create a ColorMap and change the style to RASTER_CONTOUR. |
---|
296 | */ |
---|
297 | ColorMap cmap = createColorMap(datar); |
---|
298 | gridAttr_.setColorMap(cmap); |
---|
299 | gridAttr_.setStyle(GridAttribute.RASTER_CONTOUR); |
---|
300 | /* |
---|
301 | * Add the grid to the layout and give a label for |
---|
302 | * the ColorKey. |
---|
303 | */ |
---|
304 | rpl.addData(newData, gridAttr_, "First Data"); |
---|
305 | /* |
---|
306 | * Change the layout's three title lines. |
---|
307 | */ |
---|
308 | rpl.setTitles("Raster Plot Demo", |
---|
309 | "using a JPlotLayout", |
---|
310 | ""); |
---|
311 | /* |
---|
312 | * Resize the graph and place in the "Center" of the frame. |
---|
313 | */ |
---|
314 | rpl.setSize(new Dimension(600, 400)); |
---|
315 | /* |
---|
316 | * Resize the key Pane, both the device size and the physical |
---|
317 | * size. Set the size of the key in physical units and place |
---|
318 | * the key pane at the "South" of the frame. |
---|
319 | */ |
---|
320 | // rpl.setKeyLayerSizeP(new Dimension2D(6.0, 1.02)); |
---|
321 | // rpl.setKeyBoundsP(new Rectangle2D.Double(0.01, 1.01, 5.98, 1.0)); |
---|
322 | |
---|
323 | return rpl; |
---|
324 | } |
---|
325 | |
---|
326 | ColorMap createColorMap(Range2D datar) { |
---|
327 | int[] red = |
---|
328 | { 0, 0, 0, 0, 0, 0, 0, 0, |
---|
329 | 0, 0, 0, 0, 0, 0, 0, 0, |
---|
330 | 0, 0, 0, 0, 0, 0, 0, 0, |
---|
331 | 0, 7, 23, 39, 55, 71, 87,103, |
---|
332 | 119,135,151,167,183,199,215,231, |
---|
333 | 247,255,255,255,255,255,255,255, |
---|
334 | 255,255,255,255,255,255,255,255, |
---|
335 | 255,246,228,211,193,175,158,140}; |
---|
336 | int[] green = |
---|
337 | { 0, 0, 0, 0, 0, 0, 0, 0, |
---|
338 | 0, 11, 27, 43, 59, 75, 91,107, |
---|
339 | 123,139,155,171,187,203,219,235, |
---|
340 | 251,255,255,255,255,255,255,255, |
---|
341 | 255,255,255,255,255,255,255,255, |
---|
342 | 255,247,231,215,199,183,167,151, |
---|
343 | 135,119,103, 87, 71, 55, 39, 23, |
---|
344 | 7, 0, 0, 0, 0, 0, 0, 0}; |
---|
345 | int[] blue = |
---|
346 | { 0,143,159,175,191,207,223,239, |
---|
347 | 255,255,255,255,255,255,255,255, |
---|
348 | 255,255,255,255,255,255,255,255, |
---|
349 | 255,247,231,215,199,183,167,151, |
---|
350 | 135,119,103, 87, 71, 55, 39, 23, |
---|
351 | 7, 0, 0, 0, 0, 0, 0, 0, |
---|
352 | 0, 0, 0, 0, 0, 0, 0, 0, |
---|
353 | 0, 0, 0, 0, 0, 0, 0, 0}; |
---|
354 | |
---|
355 | IndexedColorMap cmap = new IndexedColorMap(red, green, blue); |
---|
356 | cmap.setTransform(new LinearTransform(0.0, (double)red.length, |
---|
357 | datar.start, datar.end)); |
---|
358 | return cmap; |
---|
359 | } |
---|
360 | |
---|
361 | class MyAction implements java.awt.event.ActionListener { |
---|
362 | public void actionPerformed(java.awt.event.ActionEvent event) { |
---|
363 | Object obj = event.getSource(); |
---|
364 | if(obj == edit_) { |
---|
365 | edit_actionPerformed(event); |
---|
366 | } else if(obj == space_) { |
---|
367 | System.out.println(" <<Mark>>"); |
---|
368 | } else if(obj == tree_) { |
---|
369 | tree_actionPerformed(event); |
---|
370 | } else if(obj == print_) { |
---|
371 | print_actionPerformed(event); |
---|
372 | } else if(obj == layout_) { |
---|
373 | layout_actionPerformed(event); |
---|
374 | } |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|