source: ether_megapoli/trunk/applets/src/gov/noaa/pmel/sgt/demo/JVectorDemo.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: 8.1 KB
Line 
1/*
2 * $Id: JVectorDemo.java,v 1.6 2001/12/11 21:31:43 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 */
12package gov.noaa.pmel.sgt.demo;
13
14import gov.noaa.pmel.sgt.swing.JPlotLayout;
15import gov.noaa.pmel.sgt.swing.JClassTree;
16import gov.noaa.pmel.sgt.JPane;
17import gov.noaa.pmel.sgt.AbstractPane;
18import gov.noaa.pmel.sgt.VectorAttribute;
19import gov.noaa.pmel.sgt.CartesianRenderer;
20import gov.noaa.pmel.sgt.CartesianGraph;
21import gov.noaa.pmel.sgt.dm.SGTGrid;
22import gov.noaa.pmel.sgt.dm.SGTVector;
23import gov.noaa.pmel.sgt.swing.prop.VectorAttributeDialog;
24
25
26import gov.noaa.pmel.util.GeoDate;
27import gov.noaa.pmel.util.TimeRange;
28import gov.noaa.pmel.util.Range2D;
29import gov.noaa.pmel.util.Dimension2D;
30import gov.noaa.pmel.util.Rectangle2D;
31import gov.noaa.pmel.util.Point2D;
32import gov.noaa.pmel.util.IllegalTimeValue;
33
34import java.awt.*;
35import javax.swing.*;
36import javax.swing.border.EtchedBorder;
37import java.awt.event.ActionEvent;
38
39import java.awt.print.PrinterJob;
40import java.awt.print.PrinterException;
41
42/**
43 * Example demonstrating how to use <code>JPlotLayout</code>
44 * to create a raster-contour plot.
45 *
46 * @author Donald Denbo
47 * @version $Revision: 1.6 $, $Date: 2001/12/11 21:31:43 $
48 * @since 2.1
49 */
50public class JVectorDemo extends JApplet {
51  static JPlotLayout rpl_;
52  private VectorAttribute vectorAttr_;
53  JButton edit_;
54  JButton space_ = null;
55  JButton tree_;
56  JButton print_ = null;
57
58  public void init() {
59    /*
60     * Create the demo in the JApplet environment.
61     */
62    getContentPane().setLayout(new BorderLayout(0,0));
63    setBackground(java.awt.Color.white);
64    setSize(600,430);
65    JPanel main = new JPanel();
66    rpl_ = makeGraph();
67    JPanel button = makeButtonPanel(false);
68    rpl_.setBatch(true);
69    main.add(rpl_, BorderLayout.CENTER);
70    getContentPane().add(main, "Center");
71    getContentPane().add(button, "South");
72    rpl_.setBatch(false);
73  }
74
75  JPanel makeButtonPanel(boolean app) {
76    JPanel button = new JPanel();
77    button.setLayout(new FlowLayout());
78    MyAction myAction = new MyAction();
79    if(app) {
80      print_ = new JButton("Print...");
81      print_.addActionListener(myAction);
82      button.add(print_);
83    }
84    tree_ = new JButton("Tree View");
85    tree_.addActionListener(myAction);
86    button.add(tree_);
87    edit_ = new JButton("Edit VectorAttribute");
88    edit_.addActionListener(myAction);
89    button.add(edit_);
90    /*
91     * Optionally leave the "mark" button out of the button panel
92     */
93    if(app) {
94      space_ = new JButton("Add Mark");
95      space_.addActionListener(myAction);
96      button.add(space_);
97    }
98    return button;
99  }
100  public static void main(String[] args) {
101    /*
102     * Create the demo as an application
103     */
104    JVectorDemo vd = new JVectorDemo();
105    /*
106     * Create a new JFrame to contain the demo.
107     */
108    JFrame frame = new JFrame("Vector Demo");
109    JPanel main = new JPanel();
110    main.setLayout(new BorderLayout());
111    frame.setSize(600,400);
112    frame.getContentPane().setLayout(new BorderLayout());
113    /*
114     * Listen for windowClosing events and dispose of JFrame
115     */
116    frame.addWindowListener(new java.awt.event.WindowAdapter() {
117      public void windowClosing(java.awt.event.WindowEvent event) {
118        JFrame fr = (JFrame)event.getSource();
119        fr.setVisible(false);
120        fr.dispose();
121        System.exit(0);
122      }
123    });
124    /*
125     * Create button panel with "mark" button
126     */
127    JPanel button = vd.makeButtonPanel(true);
128    /*
129     * Create JPlotLayout and turn batching on.  With batching on the
130     * plot will not be updated as components are modified or added to
131     * the plot tree.
132     */
133    rpl_ = vd.makeGraph();
134    rpl_.setBatch(true);
135    /*
136     * Layout the plot and buttons.
137     */
138    main.add(rpl_, BorderLayout.CENTER);
139    frame.getContentPane().add(main, BorderLayout.CENTER);
140    frame.getContentPane().add(button, BorderLayout.SOUTH);
141    frame.pack();
142    /*
143     * Turn batching off. JPlotLayout will redraw if it has been
144     * modified since batching was turned on.
145     */
146    rpl_.setBatch(false);
147
148    frame.setVisible(true);
149  }
150
151  void print_actionPerformed(ActionEvent e) {
152    Color saveColor;
153
154    PrinterJob printJob = PrinterJob.getPrinterJob();
155    printJob.setPrintable(rpl_);
156    printJob.setJobName("Vector Demo");
157    if(printJob.printDialog()) {
158      try {
159        saveColor = rpl_.getBackground();
160        if(!saveColor.equals(Color.white)) {
161          rpl_.setBackground(Color.white);
162        }
163        rpl_.setPageAlign(AbstractPane.TOP,
164                          AbstractPane.CENTER);
165        RepaintManager currentManager = RepaintManager.currentManager(rpl_);
166        currentManager.setDoubleBufferingEnabled(false);
167        printJob.print();
168        currentManager.setDoubleBufferingEnabled(true);
169        rpl_.setBackground(saveColor);
170      } catch (PrinterException pe) {
171        System.out.println("Error printing: " + pe);
172      }
173    }
174
175  }
176
177  void edit_actionPerformed(ActionEvent e) {
178    /*
179     * Create a GridAttributeDialog and set the renderer.
180     */
181     VectorAttributeDialog vad = new VectorAttributeDialog();
182     vad.setJPane(rpl_);
183     vad.setVectorAttribute(vectorAttr_);
184     vad.setVisible(true);
185  }
186
187    void tree_actionPerformed(ActionEvent e) {
188      /*
189       * Create a JClassTree for the JPlotLayout objects
190       */
191        JClassTree ct = new JClassTree();
192        ct.setModal(false);
193        ct.setJPane(rpl_);
194        ct.show();
195    }
196
197  JPlotLayout makeGraph() {
198    /*
199     * This example uses a pre-created "Layout" for raster time
200     * series to simplify the construction of a plot. The
201     * JPlotLayout can plot a single grid with
202     * a ColorKey, time series with a LineKey, point collection with a
203     * PointCollectionKey, and general X-Y plots with a
204     * LineKey. JPlotLayout supports zooming, object selection, and
205     * object editing.
206     */
207    SGTGrid uComp;
208    SGTGrid vComp;
209    SGTVector vector;
210    TestData td;
211    JPlotLayout rpl;
212    /*
213     * Create a test grid with sinasoidal-ramp data.
214     */
215    Range2D xr = new Range2D(190.0f, 250.0f, 3.0f);
216    Range2D yr = new Range2D(0.0f, 45.0f, 3.0f);
217    td = new TestData(TestData.XY_GRID, xr, yr,
218                      TestData.SINE_RAMP, 20.0f, 10.f, 5.0f);
219    uComp = (SGTGrid)td.getSGTData();
220    td = new TestData(TestData.XY_GRID, xr, yr,
221                      TestData.SINE_RAMP, 20.0f, 0.f, 3.0f);
222    vComp = (SGTGrid)td.getSGTData();
223    vector = new SGTVector(uComp, vComp);
224    /*
225     * Create the layout without a Logo image and with the
226     * VectorKey on the graph Pane.
227     */
228    rpl = new JPlotLayout(JPlotLayout.VECTOR,
229                          false, false, "test layout", null, false);
230    rpl.setEditClasses(false);
231    vectorAttr_ = new VectorAttribute(0.0075, Color.red);
232    vectorAttr_.setHeadScale(0.5);
233    /*
234     * Add the grid to the layout and give a label for
235     * the VectorKey.
236     */
237    rpl.addData(vector, vectorAttr_, "First Data");
238    /*
239     * Change the layout's three title lines.
240     */
241    rpl.setTitles("Vector Plot Demo",
242                  "using a JPlotLayout",
243                  "");
244    /*
245     * Resize the graph  and place in the "Center" of the frame.
246     */
247    rpl.setSize(new Dimension(600, 400));
248    return rpl;
249  }
250
251  class MyAction implements java.awt.event.ActionListener {
252        public void actionPerformed(java.awt.event.ActionEvent event) {
253           Object obj = event.getSource();
254           if(obj == edit_) {
255             edit_actionPerformed(event);
256           } else if(obj == space_) {
257             System.out.println("  <<Mark>>");
258           } else if(obj == tree_) {
259               tree_actionPerformed(event);
260           } else if(obj == print_) {
261               print_actionPerformed(event);
262           }
263        }
264    }
265}
266
267
268
Note: See TracBrowser for help on using the repository browser.