Skip to content
Snippets Groups Projects
Commit e6b323bc authored by Alex Nugent's avatar Alex Nugent
Browse files

added Swing Helper

parent f01f12e5
No related branches found
No related tags found
No related merge requests found
File added
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.xeiam.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* ButtonDemo.java requires the following files:
* images/right.gif
* images/middle.gif
* images/left.gif
*/
public class ButtonDemo extends JPanel implements ActionListener {
protected JButton b1, b2, b3;
public ButtonDemo() {
ImageIcon leftButtonIcon = createImageIcon("images/right.gif");
ImageIcon middleButtonIcon = createImageIcon("images/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("images/left.gif");
b1 = new JButton("Disable middle button", leftButtonIcon);
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); // aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");
b2 = new JButton("Middle button", middleButtonIcon);
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_M);
b3 = new JButton("Enable middle button", rightButtonIcon);
// Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);
// Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this);
b1.setToolTipText("Click this button to disable the middle button.");
b2.setToolTipText("This middle button does nothing when you click it.");
b3.setToolTipText("Click this button to enable the middle button.");
// Add Components to this container, using the default FlowLayout.
add(b1);
add(b2);
add(b3);
}
@Override
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
} else {
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
ButtonDemo newContentPane = new ButtonDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
package com.xeiam.swing;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
public class SwingHelper {
Chart[] charts;
public SwingHelper(Chart chart) {
this.charts = new Chart[1];
charts[0] = chart;
}
public SwingHelper(Chart[] charts) {
this.charts = charts;
}
/**
* Create the GUI and show it. For thread safety, this method should be invoked from the event-dispatching thread.
*/
private void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
for (int i = 0; i < charts.length; i++) {
JPanel chartPanel = new JChartPanel(charts[i]);
frame.getContentPane().add(chartPanel);
}
// frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public void displayChart() {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
...@@ -15,14 +15,11 @@ ...@@ -15,14 +15,11 @@
*/ */
package com.xeiam.xcharts.example; package com.xeiam.xcharts.example;
import java.awt.BorderLayout; import com.xeiam.swing.SwingHelper;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xcharts.Chart; import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
import com.xeiam.xcharts.series.Series; import com.xeiam.xcharts.series.Series;
import com.xeiam.xcharts.series.SeriesColor;
import com.xeiam.xcharts.series.SeriesLineStyle;
import com.xeiam.xcharts.series.SeriesMarker; import com.xeiam.xcharts.series.SeriesMarker;
/** /**
...@@ -32,15 +29,15 @@ public class SwingChart { ...@@ -32,15 +29,15 @@ public class SwingChart {
private static void createAndShowGUI() { private static void createAndShowGUI() {
// // generates sine data // generates sine data
// int size = 100; int size = 100;
// double[] xData1 = new double[size + 1]; double[] xData1 = new double[size + 1];
// double[] yData1 = new double[size + 1]; double[] yData1 = new double[size + 1];
// for (int i = 0; i <= size; i++) { for (int i = 0; i <= size; i++) {
// double radians = (Math.PI / (size / 2) * i); double radians = (Math.PI / (size / 2) * i);
// xData1[i] = i - size / 2; xData1[i] = i - size / 2;
// yData1[i] = size * Math.sin(radians); yData1[i] = size * Math.sin(radians);
// } }
// generates linear data // generates linear data
int size2 = 100; int size2 = 100;
...@@ -52,9 +49,6 @@ public class SwingChart { ...@@ -52,9 +49,6 @@ public class SwingChart {
} }
// Create and set up the window. // Create and set up the window.
JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
...@@ -68,29 +62,21 @@ public class SwingChart { ...@@ -68,29 +62,21 @@ public class SwingChart {
// chart.setAxisTitlesVisible(false); // chart.setAxisTitlesVisible(false);
// Series 1 // Series 1
// Series series1 = chart.addSeries("y=sin(x)", xData1, yData1); Series series1 = chart.addSeries("y=sin(x)", xData1, yData1);
// series1.setLineColor(SeriesColor.PURPLE); series1.setLineColor(SeriesColor.PURPLE);
// series1.setLineStyle(SeriesLineStyle.NONE); series1.setLineStyle(SeriesLineStyle.NONE);
// series1.setMarkerColor(SeriesColor.GREEN); series1.setMarkerColor(SeriesColor.GREEN);
// series1.setMarker(SeriesMarker.NONE); series1.setMarker(SeriesMarker.NONE);
// Series 2 // Series 2
Series series2 = chart.addSeries("y=x", xData2, yData2); // Series series2 = chart.addSeries("y=x", xData2, yData2);
// series2.setLineColor(SeriesColor.PURPLE); // series2.setLineColor(SeriesColor.PURPLE);
// series2.setLineStyle(SeriesLineStyle.NONE); // series2.setLineStyle(SeriesLineStyle.NONE);
// series2.setMarkerColor(SeriesColor.GREEN); // series2.setMarkerColor(SeriesColor.GREEN);
series2.setMarker(SeriesMarker.NONE); // series2.setMarker(SeriesMarker.NONE);
// Swing
JPanel chartPanel = new JChartPanel(chart);
// add the panel to the content pane
frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
// Display the window SwingHelper swingHelper = new SwingHelper(chart);
frame.pack(); swingHelper.displayChart();
frame.setLocationRelativeTo(null); // centers on screen
frame.setVisible(true);
} }
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -15,13 +15,8 @@ ...@@ -15,13 +15,8 @@
*/ */
package com.xeiam.xcharts.example; package com.xeiam.xcharts.example;
import java.awt.BorderLayout; import com.xeiam.swing.SwingHelper;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xcharts.Chart; import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
import com.xeiam.xcharts.series.Series; import com.xeiam.xcharts.series.Series;
/** /**
...@@ -31,11 +26,6 @@ public class SwingChart2 { ...@@ -31,11 +26,6 @@ public class SwingChart2 {
private static void createAndShowGUI() { private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
...@@ -65,16 +55,8 @@ public class SwingChart2 { ...@@ -65,16 +55,8 @@ public class SwingChart2 {
// series.setMarker(SeriesMarker.NONE); // series.setMarker(SeriesMarker.NONE);
} }
// Swing SwingHelper swingHelper = new SwingHelper(chart);
JPanel chartPanel = new JChartPanel(chart); swingHelper.displayChart();
// add the panel to the content pane
frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
// Display the window
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen
frame.setVisible(true);
} }
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -15,13 +15,8 @@ ...@@ -15,13 +15,8 @@
*/ */
package com.xeiam.xcharts.example; package com.xeiam.xcharts.example;
import java.awt.BorderLayout; import com.xeiam.swing.SwingHelper;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xcharts.Chart; import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
import com.xeiam.xcharts.series.Series; import com.xeiam.xcharts.series.Series;
/** /**
...@@ -39,11 +34,6 @@ public class SwingChart3 { ...@@ -39,11 +34,6 @@ public class SwingChart3 {
double[] xData2 = new double[] { 0.0, 0.0, 0.0 }; double[] xData2 = new double[] { 0.0, 0.0, 0.0 };
double[] yData2 = new double[] { 0.0, 1.0, 2.0 }; double[] yData2 = new double[] { 0.0, 1.0, 2.0 };
// Create and set up the window.
JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
...@@ -59,16 +49,8 @@ public class SwingChart3 { ...@@ -59,16 +49,8 @@ public class SwingChart3 {
Series series1 = chart.addSeries("y=0", xData1, yData1); Series series1 = chart.addSeries("y=0", xData1, yData1);
Series series2 = chart.addSeries("x=0", xData2, yData2); Series series2 = chart.addSeries("x=0", xData2, yData2);
// Swing SwingHelper swingHelper = new SwingHelper(chart);
JPanel chartPanel = new JChartPanel(chart); swingHelper.displayChart();
// add the panel to the content pane
frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
// Display the window
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen
frame.setVisible(true);
} }
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -15,13 +15,8 @@ ...@@ -15,13 +15,8 @@
*/ */
package com.xeiam.xcharts.example; package com.xeiam.xcharts.example;
import java.awt.BorderLayout; import com.xeiam.swing.SwingHelper;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xcharts.Chart; import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
import com.xeiam.xcharts.series.Series; import com.xeiam.xcharts.series.Series;
import com.xeiam.xcharts.series.SeriesMarker; import com.xeiam.xcharts.series.SeriesMarker;
...@@ -37,36 +32,24 @@ public class SwingChart4 { ...@@ -37,36 +32,24 @@ public class SwingChart4 {
// generates linear data // generates linear data
double[] yData1 = new double[] { 0.0, 0.0, 0.0, -10.0, 15.0, 15.0 }; double[] yData1 = new double[] { 0.0, 0.0, 0.0, -10.0, 15.0, 15.0 };
// Create and set up the window.
JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(600, 300);
// Customize Chart // Customize Chart
chart.setChartTitle("Sample Chart"); chart.setChartTitle("Sample Chart");
chart.setXAxisTitle("X"); chart.setXAxisTitle("X");
chart.setYAxisTitle("Y"); chart.setYAxisTitle("Y");
chart.setChartTitleVisible(false); chart.setChartTitleVisible(true);
chart.setChartLegendVisible(false); chart.setChartLegendVisible(true);
chart.setAxisTitlesVisible(false); chart.setAxisTitlesVisible(true);
// Series // Series
Series series1 = chart.addSeries("y=0", null, yData1); Series series1 = chart.addSeries("y=0", null, yData1);
series1.setMarker(SeriesMarker.NONE); series1.setMarker(SeriesMarker.NONE);
// Swing SwingHelper swingHelper = new SwingHelper(chart);
JPanel chartPanel = new JChartPanel(chart); swingHelper.displayChart();
// add the panel to the content pane
frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
// Display the window
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen
frame.setVisible(true);
} }
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -15,70 +15,54 @@ ...@@ -15,70 +15,54 @@
*/ */
package com.xeiam.xcharts.example; package com.xeiam.xcharts.example;
import java.awt.BorderLayout; import com.xeiam.swing.SwingHelper;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xcharts.Chart; import com.xeiam.xcharts.Chart;
import com.xeiam.xcharts.JChartPanel;
import com.xeiam.xcharts.series.Series; import com.xeiam.xcharts.series.Series;
import com.xeiam.xcharts.series.SeriesMarker;
/** /**
* Demonstrated/Tests plotting horizontal and vertical lines
*
* @author timmolter * @author timmolter
*/ */
public class HugeChart { public class SwingChart5 {
private static void createAndShowGUI() { private static void createAndShowGUI() {
// Create and set up the window. Chart[] charts = new Chart[3];
JFrame frame = new JFrame("XChart"); for (int i = 0; i < charts.length; i++) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); charts[i] = getRandomWalkChart(1000);
frame.setUndecorated(true); }
SwingHelper swingHelper = new SwingHelper(charts);
swingHelper.displayChart();
// Create Chart }
Chart chart = new Chart(800, 600);
long t = System.currentTimeMillis(); private static Chart getRandomWalkChart(int N) {
// generates linear data
int b = 1000000; double[] y = new double[N];
double[] xData = new double[b + 1]; for (int i = 1; i < y.length; i++) {
double[] yData = new double[b + 1]; y[i] = y[i - 1] + Math.random() - .5;
for (int x = 0; x <= b; x++) {
xData[x] = x;
yData[x] = x;
} }
long t1 = System.currentTimeMillis() - t;
t = System.currentTimeMillis(); // Create Chart
System.out.println("B:" + System.currentTimeMillis()); Chart chart = new Chart(600, 300);
// Customize Chart // Customize Chart
chart.setChartTitle("Sample Chart"); chart.setChartTitle("Random Walk");
chart.setXAxisTitle("X"); chart.setXAxisTitle("X");
chart.setYAxisTitle("Y"); chart.setYAxisTitle("Y");
// chart.setChartTitleVisible(false); chart.setChartTitleVisible(true);
// chart.setChartLegendVisible(false); chart.setChartLegendVisible(true);
// chart.setAxisTitlesVisible(false); chart.setAxisTitlesVisible(true);
Series series = chart.addSeries("big data set", xData, yData);
// series.setLineColor(SeriesColor.PURPLE);
// series.setLineStyle(SeriesLineStyle.NONE);
// series.setMarkerColor(SeriesColor.GREEN);
// series.setMarker(SeriesMarker.NONE);
// Swing
JPanel chartPanel = new JChartPanel(chart);
// add the panel to the content pane // Series
frame.getContentPane().add(chartPanel, BorderLayout.CENTER); Series series1 = chart.addSeries("y=0", null, y);
series1.setMarker(SeriesMarker.NONE);
// Display the window return chart;
frame.pack();
frame.setLocationRelativeTo(null); // centers on screen
frame.setVisible(true);
long t2 = System.currentTimeMillis() - t;
System.out.println("Data Generation Time: " + t1);
System.out.println("Plot Generation Time: " + t2);
} }
public static void main(String[] args) { public static void main(String[] args) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment