001/* A simple plot application with two plots
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026 */
027package ptolemy.plot.demo;
028
029import java.awt.GridBagConstraints;
030import java.awt.GridBagLayout;
031import java.io.File;
032import java.io.FileInputStream;
033import java.io.FileNotFoundException;
034import java.io.IOException;
035
036import javax.swing.JFrame;
037import javax.swing.SwingUtilities;
038
039import ptolemy.plot.Plot;
040
041//////////////////////////////////////////////////////////////////////////
042//// TwoPlotExample
043
044/**
045 TwoPlotExample is a simple example that uses displays two plots side by side.
046
047 <p>To compile and run this application, do the following:</p>
048 <pre>
049 javac -classpath ../../.. TwoPlotExample.java
050 java -classpath ../../.. ptolemy.plot.demo.TwoPlotExample
051 </pre>
052
053 @author Christopher Hylands
054 @version $Id$
055 @since Ptolemy II 0.2
056 @Pt.ProposedRating red (eal)
057 @Pt.AcceptedRating red (cxh)
058 */
059@SuppressWarnings("serial")
060public class TwoPlotExample extends JFrame {
061    /** We use a constructor here so that we can call methods
062     *  directly on the Frame.  The main method is static
063     *  so getting at the Frame is a little trickier.
064     */
065    TwoPlotExample() {
066        // Instantiate the two plots.
067        Plot leftPlot = new Plot();
068        Plot rightPlot = new Plot();
069
070        // Set the size of the toplevel window.
071        setSize(800, 300);
072
073        // Create the left plot by calling methods.
074        // Note that most of these methods should be called in
075        // the event thread, see the Plot.java class comment.
076        // In this case, main() is invoking this constructor in
077        // the event thread.
078        leftPlot.setSize(350, 300);
079        leftPlot.setButtons(true);
080        leftPlot.setTitle("Left Plot");
081        leftPlot.setYRange(-4, 4);
082        leftPlot.setXRange(0, 100);
083        leftPlot.setXLabel("time");
084        leftPlot.setYLabel("value");
085        leftPlot.addYTick("-PI", -Math.PI);
086        leftPlot.addYTick("-PI/2", -Math.PI / 2);
087        leftPlot.addYTick("0", 0);
088        leftPlot.addYTick("PI/2", Math.PI / 2);
089        leftPlot.addYTick("PI", Math.PI);
090        leftPlot.setMarksStyle("none");
091        leftPlot.setImpulses(true);
092
093        // Call setConnected before reading in data.
094        leftPlot.setConnected(false, 1);
095
096        boolean first = true;
097
098        for (int i = 0; i <= 100; i++) {
099            leftPlot.addPoint(0, i, 5 * Math.cos(Math.PI * i / 20), !first);
100            leftPlot.addPoint(1, i, 4.5 * Math.cos(Math.PI * i / 25), !first);
101            leftPlot.addPoint(2, i, 4 * Math.cos(Math.PI * i / 30), !first);
102            leftPlot.addPoint(3, i, 3.5 * Math.cos(Math.PI * i / 35), !first);
103            leftPlot.addPoint(4, i, 3 * Math.cos(Math.PI * i / 40), !first);
104            leftPlot.addPoint(5, i, 2.5 * Math.cos(Math.PI * i / 45), !first);
105            leftPlot.addPoint(6, i, 2 * Math.cos(Math.PI * i / 50), !first);
106            leftPlot.addPoint(7, i, 1.5 * Math.cos(Math.PI * i / 55), !first);
107            leftPlot.addPoint(8, i, 1 * Math.cos(Math.PI * i / 60), !first);
108            leftPlot.addPoint(9, i, 0.5 * Math.cos(Math.PI * i / 65), !first);
109            first = false;
110        }
111
112        // Create the right plot by reading in a file.
113        rightPlot.setButtons(true);
114        leftPlot.setSize(350, 300);
115
116        File file = new File(".", "data.plt");
117
118        FileInputStream input = null;
119        try {
120            rightPlot.clear(true);
121            input = new FileInputStream(file);
122            rightPlot.read(input);
123        } catch (FileNotFoundException ex) {
124            System.err.println("File not found: " + file + " : " + ex);
125        } catch (IOException ex) {
126            System.err.println("Error reading input: " + file + " : " + ex);
127        } finally {
128            if (input != null) {
129                try {
130                    input.close();
131                } catch (Exception ex) {
132                    ex.printStackTrace();
133                }
134            }
135        }
136        // Override the title in the file.
137        rightPlot.setTitle("Right Plot");
138
139        // Layout the two plots
140        GridBagLayout gridbag = new GridBagLayout();
141        GridBagConstraints c = new GridBagConstraints();
142        getContentPane().setLayout(gridbag);
143
144        // Handle the leftPlot
145        c.gridx = 0;
146        c.gridy = 0;
147        c.gridwidth = 1;
148        gridbag.setConstraints(leftPlot, c);
149        getContentPane().add(leftPlot);
150
151        // Handle the rightPlot
152        c.gridx = 1;
153        c.gridy = 0;
154        c.gridwidth = 1;
155        c.fill = GridBagConstraints.BOTH;
156        c.weightx = 1.0;
157        c.weighty = 1.0;
158        gridbag.setConstraints(rightPlot, c);
159        getContentPane().add(rightPlot);
160
161        setVisible(true);
162    }
163
164    /** Run the demonstration a standalone java application.
165     *  We simple instantiate this class, most of the work
166     *  happens in the constructor.
167     * @param args Not used.
168     */
169    public static void main(String[] args) {
170        // We execute everything in the Swing Event Thread, see
171        // the comment
172        Runnable doAction = new Runnable() {
173            @Override
174            public void run() {
175                new TwoPlotExample();
176            }
177        };
178
179        SwingUtilities.invokeLater(doAction);
180    }
181}