KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > demos > ZoomTest


1 /*
2  * ZoomTest.java of project jchart2d, demonstration of a zoom-enabled
3  * chart.
4  * Copyright 2006 (C) Achim Westermann, created on 23:59:21.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.gui.chart.demos;
24
25 import info.monitorenter.gui.chart.ITrace2D;
26 import info.monitorenter.gui.chart.ZoomableChart;
27 import info.monitorenter.gui.chart.layout.ChartPanel;
28 import info.monitorenter.gui.chart.traces.Trace2DSimple;
29
30 import java.awt.BasicStroke JavaDoc;
31 import java.awt.BorderLayout JavaDoc;
32 import java.awt.Color JavaDoc;
33 import java.awt.Container JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.WindowAdapter JavaDoc;
37 import java.awt.event.WindowEvent JavaDoc;
38 import java.util.Random JavaDoc;
39
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JFrame JavaDoc;
42
43 /**
44  * Demonstration of a zoom - enabled chart ({@link info.monitorenter.gui.chart.ZoomableChart}).
45  * <p>
46  *
47  * @author Alessio Sambarino (Contributor)
48  *
49  * @version $Revision: 1.1 $
50  */

51 public class ZoomTest extends JFrame JavaDoc {
52
53   /**
54    * Action adapter for zoomAllButton.
55    * <p>
56    */

57   class ZoomAllAdapter implements ActionListener JavaDoc {
58     /** The zoomable chart to act upon. */
59     private ZoomableChart m_zoomableChart;
60
61     /**
62      * Creates an instance that will reset zooming on the given zoomable chart
63      * upon the triggered action.
64      * <p>
65      *
66      * @param chart
67      * the target to reset zoomin on.
68      */

69     public ZoomAllAdapter(final ZoomableChart chart) {
70       this.m_zoomableChart = chart;
71     }
72
73     /**
74      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
75      */

76     public void actionPerformed(final ActionEvent JavaDoc event) {
77       this.m_zoomableChart.zoomAll();
78     }
79   }
80
81   /**
82    * Main startup method.
83    * <p>
84    *
85    * @param args
86    * ignored.
87    */

88   public static void main(final String JavaDoc[] args) {
89
90     ZoomTest zoomTest = new ZoomTest();
91     // Show the frame
92
zoomTest.setSize(640, 480);
93     zoomTest.setVisible(true);
94
95   }
96
97   /**
98    * Defcon.
99    * <p>
100    */

101   public ZoomTest() {
102
103     super("ZoomTest");
104
105     Container JavaDoc c = getContentPane();
106     c.setLayout(new BorderLayout JavaDoc());
107
108     // Create a chart
109
ZoomableChart chart = new ZoomableChart();
110
111     // Create ITrace
112
ITrace2D trace = new Trace2DSimple("Trace");
113     trace.setColor(Color.RED);
114     trace.setStroke(new BasicStroke JavaDoc(2));
115
116     // Add all points, as it is static
117
Random JavaDoc random = new Random JavaDoc();
118
119     for (int i = 0; i < 100; i++) {
120       trace.addPoint(i, random.nextDouble() * 10.0 + i);
121     }
122
123     // Add the trace to the chart
124
chart.addTrace(trace);
125
126     // Add chart to the pane
127
c.add(new ChartPanel(chart));
128
129     // Create the zoomAll button
130
JButton JavaDoc zoomAllButton = new JButton JavaDoc("Zoom All");
131     zoomAllButton.addActionListener(new ZoomAllAdapter(chart));
132
133     // Add zoomAll button to the pane
134
c.add(zoomAllButton, BorderLayout.NORTH);
135
136     // Enable the termination button:
137
addWindowListener(new WindowAdapter JavaDoc() {
138       public void windowClosing(final WindowEvent JavaDoc e) {
139         System.exit(0);
140       }
141     });
142
143   }
144 }
145
Popular Tags