001/* Demo for a live signal plotter.
002
003 @Copyright (c) 1997-2014 The Regents of the University of California.
004 All rights reserved.
005
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the
009 above copyright notice and the following two paragraphs appear in all
010 copies of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027 */
028package ptolemy.plot.demo;
029
030import java.awt.event.WindowAdapter;
031import java.awt.event.WindowEvent;
032
033import javax.swing.JFrame;
034import javax.swing.SwingUtilities;
035
036import ptolemy.plot.PlotLive;
037import ptolemy.util.StringUtilities;
038
039///////////////////////////////////////////////////////////////////
040//// PlotLiveDemo
041
042/**
043 Dynamically plot a test signal, illustrating how to use the
044 PlotLive class.
045
046 @author Edward A. Lee
047 @version $Id$
048 @since Ptolemy II 0.2
049 @Pt.ProposedRating red (eal)
050 @Pt.AcceptedRating red (cxh)
051 */
052@SuppressWarnings("serial")
053public class PlotLiveDemo extends PlotLive {
054    /** Construct a plot for live, animated signal display.
055     *  Configure the title, axes, points style, and persistence.
056     */
057    public PlotLiveDemo() {
058        setYRange(-1, 1);
059        setXRange(-1, 1);
060        setPointsPersistence(60);
061        setMarksStyle("dots");
062    }
063
064    ///////////////////////////////////////////////////////////////////
065    ////                         public methods                    ////
066
067    /** Add points to the plot.  This is called by the base class
068     *  run() method when the plot is live.
069     */
070    @Override
071    public synchronized void addPoints() {
072        // You could plot multiple points at a time here
073        // for faster response, but in our case, we really need
074        // to slow down the response for visual aesthetics.
075        addPoint(0, Math.sin(Math.PI * _count / 25),
076                Math.cos(Math.PI * _count / 100), false);
077        addPoint(0, Math.sin(Math.PI * _count / 45),
078                Math.cos(Math.PI * _count / 70), true);
079        addPoint(1, Math.sin(Math.PI * _count / 45),
080                Math.cos(Math.PI * _count / 70), false);
081        addPoint(2, Math.sin(Math.PI * _count / 20),
082                Math.cos(Math.PI * _count / 100), false);
083        addPoint(3, Math.sin(Math.PI * _count / 50),
084                Math.cos(Math.PI * _count / 70), false);
085        _count += 1.0;
086
087        try {
088            // Findbugs:
089            // [M M SWL] Method calls Thread.sleep() with a lock held [SWL_SLEEP_WITH_LOCK_HELD]
090            // Here it is not a problem since we
091            // actually want to block the other threads
092            // if they want to paint to slow down the drawing.
093            Thread.sleep(5);
094        } catch (InterruptedException e) {
095        }
096    }
097
098    /** Run the demo as an application.
099     * This is very useful for debugging.  The command to run would be
100     * java -classpath $PTII ptolemy.plot.demo.PlotLiveDemo
101     * @param args Not used.
102     */
103    public static void main(String[] args) {
104        // Run this in the Swing Event Thread.
105        Runnable doActions = new Runnable() {
106            @Override
107            public void run() {
108                try {
109                    final PlotLiveDemo plotLiveDemo = new PlotLiveDemo();
110
111                    JFrame frame = new JFrame("PlotLiveDemo");
112                    frame.addWindowListener(new WindowAdapter() {
113                        @Override
114                        public void windowClosing(WindowEvent event) {
115                            plotLiveDemo.stop();
116                            StringUtilities.exit(0);
117                        }
118                    });
119                    frame.getContentPane().add("Center", plotLiveDemo);
120                    frame.setVisible(true);
121                    plotLiveDemo.setButtons(true);
122                    plotLiveDemo.start();
123                    frame.pack();
124                } catch (Exception ex) {
125                    System.err.println(ex.toString());
126                    ex.printStackTrace();
127                }
128            }
129        };
130
131        try {
132            SwingUtilities.invokeAndWait(doActions);
133        } catch (Exception ex) {
134            System.err.println(ex.toString());
135            ex.printStackTrace();
136        }
137    }
138
139    ///////////////////////////////////////////////////////////////////
140    ////                         private variables                 ////
141
142    /** @serial Value being plotted */
143    private double _count = 0.0;
144}