source: ether_megapoli/trunk/applets/src/AppletTimeSerie.java @ 174

Last change on this file since 174 was 174, checked in by vmipsl, 13 years ago

Applets _ récupération des sources

File size: 15.7 KB
Line 
1import gov.noaa.pmel.sgt.Axis;
2import gov.noaa.pmel.sgt.CartesianGraph;
3import gov.noaa.pmel.sgt.Graph;
4import gov.noaa.pmel.sgt.JPane;
5import gov.noaa.pmel.sgt.Layer;
6import gov.noaa.pmel.sgt.LineAttribute;
7import gov.noaa.pmel.sgt.LineCartesianRenderer;
8import gov.noaa.pmel.sgt.LineKey;
9import gov.noaa.pmel.sgt.LinearTransform;
10import gov.noaa.pmel.sgt.PlainAxis;
11import gov.noaa.pmel.sgt.SGLabel;
12import gov.noaa.pmel.sgt.StackedLayout;
13import gov.noaa.pmel.sgt.TimeAxis;
14import gov.noaa.pmel.sgt.demo.TestData;
15import gov.noaa.pmel.sgt.dm.SimpleLine;
16import gov.noaa.pmel.sgt.swing.JClassTree;
17import gov.noaa.pmel.util.Dimension2D;
18import gov.noaa.pmel.util.GeoDate;
19import gov.noaa.pmel.util.IllegalTimeValue;
20import gov.noaa.pmel.util.Point2D;
21import gov.noaa.pmel.util.Range2D;
22import gov.noaa.pmel.util.SoTPoint;
23import gov.noaa.pmel.util.SoTRange;
24import gov.noaa.pmel.util.TimeRange;
25
26import javax.swing.*;
27import javax.swing.border.EtchedBorder;
28import java.awt.*;
29import java.util.Enumeration;
30
31public class AppletTimeSerie
32        extends JApplet
33{
34    JPane mainPane_;
35    JClassTree tree_ = null;
36    boolean isApplet_ = true;
37    JFrame frame = null;
38
39    JPanel controlPanel = new JPanel();
40    JCheckBox stacked = new JCheckBox();
41    ButtonGroup gridtype = new ButtonGroup();
42    JCheckBox grid = new JCheckBox();
43    JButton showTree = new JButton();
44
45    public void init()
46    {
47        /*
48        * Create a JLayoutDemo within a JApplet
49        */
50        setLayout( new BorderLayout( 0, 0 ) );
51        setBackground( Color.white );
52        setSize( 426, 712 );
53
54        makeControlPanel();
55        add( controlPanel, BorderLayout.SOUTH );
56
57        makeGraph();
58        add( mainPane_, BorderLayout.CENTER );
59    }
60
61    void makeControlPanel()
62    {
63        controlPanel.setLayout( new GridBagLayout() );
64//    controlPanel.setBackground(new java.awt.Color(200,200,200));
65        controlPanel.setBounds( 0, 679, 426, 33 );
66        controlPanel.setBorder( new EtchedBorder() );
67        gridtype.add( stacked );
68        stacked.setSelected( true );
69        stacked.setText( "Overlayed" );
70
71        GridBagConstraints gbc = new GridBagConstraints();
72        gbc.gridx = 0;
73        gbc.gridy = 0;
74        gbc.gridwidth = 1;
75        gbc.gridheight = 1;
76        gbc.weightx = 0.0;
77        gbc.weighty = 1.0;
78        gbc.anchor = GridBagConstraints.EAST;
79        gbc.fill = GridBagConstraints.BOTH;
80        gbc.insets = new Insets( 5, 15, 5, 5 );
81        gbc.ipadx = 0;
82        gbc.ipady = 0;
83        controlPanel.add( stacked, gbc );
84        stacked.setBounds( 15, 5, 84, 23 );
85        gridtype.add( grid );
86        grid.setText( "Grid" );
87
88        gbc.gridx = 1;
89        gbc.gridy = 0;
90        gbc.gridwidth = 1;
91        gbc.gridheight = 1;
92        gbc.weightx = 0.5;
93        gbc.weighty = 1.0;
94        gbc.anchor = GridBagConstraints.WEST;
95        gbc.fill = GridBagConstraints.VERTICAL;
96        gbc.insets = new Insets( 5, 5, 5, 0 );
97        gbc.ipadx = 0;
98        gbc.ipady = 0;
99        controlPanel.add( grid, gbc );
100        grid.setBounds( 109, 5, 53, 23 );
101        showTree.setText( "Show Class Tree" );
102
103        gbc.gridx = 2;
104        gbc.gridy = 0;
105        gbc.gridwidth = 1;
106        gbc.gridheight = 1;
107        gbc.weightx = 1.0;
108        gbc.weighty = 1.0;
109        gbc.anchor = GridBagConstraints.EAST;
110        gbc.fill = GridBagConstraints.VERTICAL;
111        gbc.insets = new Insets( 5, 5, 5, 15 );
112        gbc.ipadx = 0;
113        gbc.ipady = 0;
114        controlPanel.add( showTree, gbc );
115        showTree.setBackground( Color.yellow.brighter() );
116        showTree.setBounds( 302, 5, 109, 23 );
117
118        SymItem lSymItem = new SymItem();
119        stacked.addItemListener( lSymItem );
120        grid.addItemListener( lSymItem );
121        SymAction lSymAction = new SymAction();
122        showTree.addActionListener( lSymAction );
123    }
124
125    void makeGraph()
126    {
127        /*
128        * This example explicitly creates the JPane, Layers, Axes, and SGLabels.
129        */
130        /*
131        * Create JPane, place in the center of the frame
132        * and set the layout to use the StackedLayout.  StackedLayout is
133        * a custom layout manager designed to place Components directly
134        * over one another.
135        */
136        mainPane_ = new JPane( "Layout Demo", new Dimension( 426, 400 ) );
137        mainPane_.setLayout( new StackedLayout() );
138        /*
139        * Create the two random time series using the TestData class and
140        * the SimpleLine classes from sgt.dm
141        */
142        SimpleLine line;
143        SimpleLine line2;
144        GeoDate start = null;
145        GeoDate stop = null;
146        TimeRange tr;
147        TestData td;
148        try
149        {
150            start = new GeoDate( "1992-11-01", "yyyy-MM-dd" );
151            stop = new GeoDate( "1993-02-20", "yyyy-MM-dd" );
152        }
153        catch( IllegalTimeValue e )
154        {
155        }
156        tr = new TimeRange( start, stop );
157        td = new TestData( TestData.TIME_SERIES, tr, 1.0f,
158                TestData.RANDOM, 1.2f, 0.0f, 20.0f );
159        line = (SimpleLine) td.getSGTData();
160        //
161        try
162        {
163            start = new GeoDate( "1992-11-01", "yyyy-MM-dd" );
164            stop = new GeoDate( "1993-02-20", "yyyy-MM-dd" );
165        }
166        catch( IllegalTimeValue e )
167        {
168        }
169        tr = new TimeRange( start, stop );
170        td = new TestData( TestData.TIME_SERIES, tr, 1.0f,
171                TestData.RANDOM, 1.2f, 0.5f, 30.0f );
172        line2 = (SimpleLine) td.getSGTData();
173        /*
174        * Get the axis ranges from SGTLine
175        */
176        SoTRange ynRange, yRange;
177        SoTRange tnRange;
178        String yLabel;
179        yRange = line.getYRange();
180        yRange.add( line2.getYRange() );
181        tnRange = line.getXRange();
182        /*
183        * compute the range for the y and time axes
184        * and get the y axis label from line's metadata
185        */
186        ynRange = Graph.computeRange( yRange, 6 );
187        yLabel = line.getYMetaData().getName();
188        /*
189        * LayoutDemo will have two layers.
190        * One layer for each line to be drawn.
191        * The first layer will contain the axes and labels
192        * and the first set of data. The second layer will
193        * contain the second set of data.
194        */
195        /*
196        * xsize, ysize are the width and height in physical units
197        * of the Layer graphics region.
198        *
199        * xstart, xend are the start and end points for the TimeAxis
200        * ystart, yend are the start and end points for the Y axis
201        */
202        double xsize = 4.0;
203        double xstart = 0.6;
204        double xend = 3.25;
205        double ysize = 3.0;
206        double ystart = 0.6;
207        double yend = 2.50;
208
209        Layer layer, layer2;
210        SGLabel label, title, ytitle;
211        CartesianGraph graph, graph2;
212        LinearTransform xt, yt;
213        PlainAxis yleft;
214        TimeAxis xbot;
215        LineKey lkey;
216        GeoDate stime;
217        /*
218        * create the first layer
219        */
220        layer = new Layer( "First Layer", new Dimension2D( xsize, ysize ) );
221        /*
222        * create a time stamp label for the plot
223        * position the label at the lower left corner
224        * and add to the first layer
225        * (NOTE: the time will be displayed for the GMT time zone)
226        */
227        stime = new GeoDate();
228        label = new SGLabel( "Date Stamp", stime.toString(),
229                new Point2D.Double( 0.05, 0.05 ) );
230        label.setAlign( SGLabel.BOTTOM, SGLabel.LEFT );
231        label.setColor( Color.magenta );
232        label.setHeightP( 0.15 );
233        label.setFont( new Font( "Dialog", Font.PLAIN, 10 ) );
234        layer.addChild( label );
235        /*
236        * create a title for the plot
237        * position the label centered on the graph
238        * and add to the first layer
239        */
240        title = new SGLabel( "Title", "Layout Demo",
241                new Point2D.Double( xsize / 2.0, ysize ) );
242        title.setAlign( SGLabel.TOP, SGLabel.CENTER );
243        title.setHeightP( 0.20 );
244        title.setFont( new Font( "Helvetica", Font.BOLD, 14 ) );
245        layer.addChild( title );
246        /*
247        * create a LineKey
248        * the LineKey will be a legend for the two lines created
249        * position the key in the upper right corner
250        * and add to the first layer
251        */
252        lkey = new LineKey();
253        lkey.setId( "Legend" );
254        lkey.setLocationP( new Point2D.Double( xsize - 0.01, ysize ) );
255        lkey.setVAlign( LineKey.TOP );
256        lkey.setHAlign( LineKey.RIGHT );
257        layer.addChild( lkey );
258        /*
259        * add the first layer to the Pane
260        */
261        mainPane_.add( layer );
262        /*
263        * create first CartesianGraph and transforms
264        */
265        graph = new CartesianGraph( "First Graph" );
266        xt = new LinearTransform( new Range2D( xstart, xend ), tnRange );
267        graph.setXTransform( xt );
268        yt = new LinearTransform( new Range2D( ystart, yend ), ynRange );
269        graph.setYTransform( yt );
270        /*
271        * Create the time axis, set its range in user units
272        * and its origin. Add the axis to the first graph.
273        */
274        SoTPoint point = new SoTPoint( ynRange.getStart(), tnRange.getStart() );
275        xbot = new TimeAxis( "Bottom Axis", TimeAxis.MONTH_YEAR );
276        xbot.setRangeU( tnRange );
277        xbot.setLocationU( point );
278        Font xbfont = new Font( "Helvetica", Font.ITALIC, 14 );
279        xbot.setLabelFont( xbfont );
280        xbot.setMinorLabelInterval( 1 );
281        graph.addXAxis( xbot );
282        /*
283        * Create the vertical axis, set its range in user units
284        * and its origin.  Create the axis title and add the
285        * axis to the first graph.
286        */
287        yleft = new PlainAxis( "Left Axis" );
288        yleft.setRangeU( ynRange );
289        yleft.setLocationU( point );
290        yleft.setLabelFont( xbfont );
291        ytitle = new SGLabel( "Y-Axis Title", yLabel,
292                new Point2D.Double( 0.0, 0.0 ) );
293        Font ytfont = new Font( "Helvetica", Font.PLAIN, 14 );
294        ytitle.setFont( ytfont );
295        ytitle.setHeightP( 0.2 );
296        yleft.setTitle( ytitle );
297        graph.addYAxis( yleft );
298        /*
299        * Add the first graph to the first layer.
300        */
301        layer.setGraph( graph );
302        /*
303        * Create a LineAttribute for the display of the first
304        * line. Associate the attribute and the line with the
305        * first graph.  Add the line to the LineKey.
306        */
307        LineAttribute attr;
308
309        attr = new LineAttribute( LineAttribute.MARK, 20, Color.red );
310        attr.setMarkHeightP( 0.1 );
311        graph.setData( line, attr );
312        lkey.addLineGraph( (LineCartesianRenderer) graph.getRenderer(),
313                new SGLabel( "1st line", "Red Data",
314                        new Point2D.Double( 0.0, 0.0 ) ) );
315        /*
316        * Create the second layer and add it the the Pane.
317        * Create the second graph and associate it with the
318        * second layer.
319        */
320        layer2 = new Layer( "Second Layer", new Dimension2D( xsize, ysize ) );
321        mainPane_.add( layer2 );
322        graph2 = new CartesianGraph( "Second Graph", xt, yt );
323        layer2.setGraph( graph2 );
324        /*
325        * Create a LineAttribute for the display of the second
326        * line. Associate the attribute and the line with the
327        * second graph.  Add the line to the LineKey.
328        */
329        LineAttribute attr2;
330        attr2 = new LineAttribute( LineAttribute.MARK, 2, Color.blue );
331        attr2.setMarkHeightP( 0.1 );
332        graph2.setData( line2, attr2 );
333        lkey.addLineGraph( (LineCartesianRenderer) graph2.getRenderer(),
334                new SGLabel( "2nd line", "Blue Data",
335                        new Point2D.Double( 0.0, 0.0 ) ) );
336    }
337
338    /**
339     * Change the Pane layout to StackedLayout.
340     *
341     * @param event
342     */
343
344    void stacked_itemStateChanged( java.awt.event.ItemEvent event )
345    {
346        /*
347        * Get the component list for mainPane_ and change
348        * the layout to StackedLayout.
349        */
350        Component[] comps = mainPane_.getComponents();
351        mainPane_.setBatch( true );
352        mainPane_.setLayout( new StackedLayout() );
353        /*
354        * Remove any axes that have been associated with
355        * the second graph.  With the layers overlayed it
356        * is not necessary to have duplicate axes.
357        */
358        Graph gr2 = ( (Layer) comps[1] ).getGraph();
359        ( (CartesianGraph) gr2 ).removeAllXAxes();
360        ( (CartesianGraph) gr2 ).removeAllYAxes();
361        /*
362        * Tell the Applet that the mainPane_ needs to
363        * be layed out and re-draw the mainPane_.
364        */
365        if( isApplet_ )
366        {
367            validate();
368        }
369        else
370        {
371            frame.validate();
372        }
373        mainPane_.setBatch( false );
374        if( tree_ != null )
375        {
376            if( tree_.isVisible() )
377            {
378                tree_.setJPane( mainPane_ );
379                tree_.expandTree();
380            }
381        }
382    }
383
384    /**
385     * Change the Pane layout to GridLayout.
386     *
387     * @param event
388     */
389
390    void grid_itemStateChanged( java.awt.event.ItemEvent event )
391    {
392        /*
393        * Get the component list for mainPane_ and change
394        * the layout to GridLayout.
395        */
396        Component[] comps = mainPane_.getComponents();
397        mainPane_.setBatch( true );
398        mainPane_.setLayout( new GridLayout( 2, 0 ) );
399        /*
400        * Get the first and second graphs from the first
401        * and second layers, respectively.
402        */
403        Graph gr = ( (Layer) comps[0] ).getGraph();
404        Graph gr2 = ( (Layer) comps[1] ).getGraph();
405        /*
406        * Create copies of all X-Axes associated with the first
407        * graph for the second graph. If the axes are not copied then
408        * the second graph will have the second line plotted, but without
409        * any axes.
410        */
411        for( Enumeration xa = ( (CartesianGraph) gr ).xAxisElements(); xa.hasMoreElements(); )
412        {
413            ( (CartesianGraph) gr2 ).addXAxis( ( (Axis) xa.nextElement() ).copy() );
414        }
415        /*
416        * Create copies of all Y-Axes associated with the first
417        * graph for the second graph.
418        */
419        for( Enumeration ya = ( (CartesianGraph) gr ).yAxisElements(); ya.hasMoreElements(); )
420        {
421            ( (CartesianGraph) gr2 ).addYAxis( ( (Axis) ya.nextElement() ).copy() );
422        }
423        /*
424        * Tell the Applet that the mainPane_ needs to
425        * be layed out and re-draw the mainPane_.
426        */
427        if( isApplet_ )
428        {
429            validate();
430        }
431        else
432        {
433            frame.validate();
434        }
435        //    mainPane_.draw();
436        mainPane_.setBatch( false );
437        if( tree_ != null )
438        {
439            if( tree_.isVisible() )
440            {
441                tree_.setJPane( mainPane_ );
442                tree_.expandTree();
443            }
444        }
445    }
446
447    void showTree_ActionPerformed( java.awt.event.ActionEvent event )
448    {
449        /*
450        * Create the ClassTree dialog to display the classes used
451        * in the mainPane_ and allow editing.
452        */
453        if( tree_ == null )
454        {
455            tree_ = new JClassTree( "Classes for LayoutDemo" );
456        }
457        tree_.setJPane( mainPane_ );
458        tree_.show();
459    }
460
461    class SymItem
462            implements java.awt.event.ItemListener
463    {
464        public void itemStateChanged( java.awt.event.ItemEvent event )
465        {
466            Object object = event.getSource();
467            if( object == stacked )
468                stacked_itemStateChanged( event );
469            else if( object == grid )
470                grid_itemStateChanged( event );
471        }
472    }
473
474    class SymAction
475            implements java.awt.event.ActionListener
476    {
477        public void actionPerformed( java.awt.event.ActionEvent event )
478        {
479            Object object = event.getSource();
480            if( object == showTree )
481                showTree_ActionPerformed( event );
482        }
483    }
484
485}
Note: See TracBrowser for help on using the repository browser.