superdemo.java
来自「在软件开发中用来绘制各种图表的源码」· Java 代码 · 共 1,115 行 · 第 1/3 页
JAVA
1,115 行
/* --------------
* SuperDemo.java
* --------------
* (C) Copyright 2004-2006, by Object Refinery Limited.
*
*/
package demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A demo for JFreeChart.
*/
public class SuperDemo extends ApplicationFrame
implements ActionListener, TreeSelectionListener {
/** Exit action command. */
public static final String EXIT_COMMAND = "EXIT";
private JPanel displayPanel;
private JPanel chartContainer;
private JPanel descriptionContainer;
private JTextPane descriptionPane;
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public SuperDemo(String title) {
super(title);
setContentPane(createContent());
setJMenuBar(createMenuBar());
}
/**
* Creates a panel for the main window.
*
* @return A panel.
*/
private JComponent createContent() {
JPanel content = new JPanel(new BorderLayout());
JTabbedPane tabs = new JTabbedPane();
JPanel content1 = new JPanel(new BorderLayout());
content1.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JSplitPane splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JTree tree = new JTree(createTreeModel());
tree.addTreeSelectionListener(this);
splitter.setLeftComponent(new JScrollPane(tree));
splitter.setRightComponent(createChartDisplayPanel());
content1.add(splitter);
tabs.add("Demos", content1);
MemoryUsageDemo memUse = new MemoryUsageDemo(300000);
memUse.new DataGenerator(1000).start();
tabs.add("Memory Usage", memUse);
tabs.add("Source Code", createSourceCodePanel());
tabs.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
content.add(tabs);
return content;
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
// first the file menu
JMenu fileMenu = new JMenu("File", true);
fileMenu.setMnemonic('F');
JMenuItem exitItem = new JMenuItem("Exit", 'x');
exitItem.setActionCommand(EXIT_COMMAND);
exitItem.addActionListener(this);
fileMenu.add(exitItem);
// finally, glue together the menu and return it
menuBar.add(fileMenu);
return menuBar;
}
private JPanel createSourceCodePanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
java.net.URL sourceURL = SuperDemo.class.getResource("source.html");
if (sourceURL != null) {
try {
editorPane.setPage(sourceURL);
}
catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + sourceURL);
}
}
else {
System.err.println("Couldn't find file: source.html");
}
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(10, 10));
panel.add(editorScrollPane);
return panel;
}
/**
* Handles menu selections by passing control to an appropriate method.
*
* @param event the event.
*/
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals(EXIT_COMMAND)) {
attemptExit();
}
}
/**
* Exits the application, but only if the user agrees.
*/
private void attemptExit() {
String title = "Confirm";
String message = "Are you sure you want to exit the demo?";
int result = JOptionPane.showConfirmDialog(
this, message, title, JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE
);
if (result == JOptionPane.YES_OPTION) {
dispose();
System.exit(0);
}
}
private JPanel createChartDisplayPanel() {
this.displayPanel = new JPanel(new BorderLayout());
this.chartContainer = new JPanel(new BorderLayout());
this.chartContainer.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(4, 4, 4, 4),
BorderFactory.createLineBorder(Color.black)
)
);
this.chartContainer.add(createNoDemoSelectedPanel());
this.descriptionContainer = new JPanel(new BorderLayout());
this.descriptionContainer.setBorder(
BorderFactory.createEmptyBorder(4, 4, 4, 4)
);
this.descriptionContainer.setPreferredSize(new Dimension(600, 140));
this.descriptionPane = new JTextPane();
this.descriptionPane.setEditable(false);
JScrollPane scroller = new JScrollPane(
this.descriptionPane,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
);
this.descriptionContainer.add(scroller);
displayDescription("select.html");
JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitter.setTopComponent(this.chartContainer);
splitter.setBottomComponent(this.descriptionContainer);
this.displayPanel.add(splitter);
splitter.setDividerLocation(0.75);
return this.displayPanel;
}
/**
* Creates a <code>TreeModel</code> with references to all the individual
* demo applications. This is an ugly piece of hard-coding but, hey, it's
* just a demo!
*
* @return A TreeModel.
*/
private TreeModel createTreeModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("JFreeChart");
root.add(createPieChartsNode());
root.add(createBarChartsNode());
root.add(createLineChartsNode());
root.add(createAreaChartsNode());
root.add(createTimeSeriesChartsNode());
root.add(createFinancialChartsNode());
root.add(createXYChartsNode());
root.add(createMeterChartsNode());
root.add(createMultipleAxisChartsNode());
root.add(createCombinedAxisChartsNode());
root.add(createGanttChartsNode());
root.add(createMiscellaneousChartsNode());
return new DefaultTreeModel(root);
}
/**
* Creates the tree node for the pie chart demos.
*
* @return A populated tree node.
*/
private MutableTreeNode createPieChartsNode() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Pie Charts");
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChartDemo1",
"PieChartDemo1.java"));
DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChartDemo2",
"PieChartDemo2.java"));
DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChartDemo3",
"PieChartDemo3.java"));
DefaultMutableTreeNode n4 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChartDemo8",
"PieChartDemo8.java"));
DefaultMutableTreeNode n5 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChart3DDemo1",
"PieChart3DDemo1.java"));
DefaultMutableTreeNode n6 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChart3DDemo2",
"PieChart3DDemo2.java"));
DefaultMutableTreeNode n7 = new DefaultMutableTreeNode(
new DemoDescription("demo.PieChart3DDemo3",
"PieChart3DDemo3.java"));
DefaultMutableTreeNode n8 = new DefaultMutableTreeNode(
new DemoDescription("demo.MultiplePieChartDemo1",
"MultiplePieChartDemo1.java"));
DefaultMutableTreeNode n9 = new DefaultMutableTreeNode(
new DemoDescription("demo.RingChartDemo1", "RingChartDemo1.java"));
root.add(n1);
root.add(n2);
root.add(n3);
root.add(n4);
root.add(n5);
root.add(n6);
root.add(n7);
root.add(n8);
root.add(n9);
return root;
}
/**
* Creates a tree node containing sample bar charts.
*
* @return The tree node.
*/
private MutableTreeNode createBarChartsNode() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Bar Charts");
root.add(createCategoryBarChartsNode());
root.add(createXYBarChartsNode());
return root;
}
/**
* Creates a tree node containing bar charts based on the
* {@link CategoryPlot} class.
*
* @return The tree node.
*/
private MutableTreeNode createCategoryBarChartsNode() {
DefaultMutableTreeNode root
= new DefaultMutableTreeNode("CategoryPlot");
MutableTreeNode n1 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo1", "BarChartDemo1.java"));
MutableTreeNode n2 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo2", "BarChartDemo2.java"));
MutableTreeNode n3 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo3", "BarChartDemo3.java"));
MutableTreeNode n4 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo4", "BarChartDemo4.java"));
MutableTreeNode n5 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo5", "BarChartDemo5.java"));
MutableTreeNode n6 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo6", "BarChartDemo6.java"));
MutableTreeNode n7 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo7", "BarChartDemo7.java"));
MutableTreeNode n8 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo8", "BarChartDemo8.java"));
MutableTreeNode n9 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo9", "BarChartDemo9.java"));
MutableTreeNode n10 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChartDemo10", "BarChartDemo10.java"));
MutableTreeNode n11 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChart3DDemo1", "BarChart3DDemo1.java"));
MutableTreeNode n12 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChart3DDemo2", "BarChart3DDemo2.java"));
MutableTreeNode n13 = new DefaultMutableTreeNode(new DemoDescription(
"demo.BarChart3DDemo3", "BarChart3DDemo3.java"));
MutableTreeNode n14 = new DefaultMutableTreeNode(new DemoDescription(
"demo.IntervalBarChartDemo1", "IntervalBarChartDemo1.java"));
MutableTreeNode n15 = new DefaultMutableTreeNode(new DemoDescription(
"demo.LayeredBarChartDemo1", "LayeredBarChartDemo1.java"));
MutableTreeNode n16 = new DefaultMutableTreeNode(new DemoDescription(
"demo.LayeredBarChartDemo2", "LayeredBarChartDemo2.java"));
MutableTreeNode n17 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StackedBarChartDemo1", "StackedBarChartDemo1.java"));
MutableTreeNode n18 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StackedBarChartDemo2", "StackedBarChartDemo2.java"));
MutableTreeNode n19 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StackedBarChartDemo3", "StackedBarChartDemo3.java"));
MutableTreeNode n20 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StackedBarChartDemo4", "StackedBarChartDemo4.java"));
MutableTreeNode n21 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StackedBarChartDemo5", "StackedBarChartDemo5.java"));
MutableTreeNode n22 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StackedBarChartDemo6", "StackedBarChartDemo6.java"));
MutableTreeNode n23 = new DefaultMutableTreeNode(new DemoDescription(
"demo.StatisticalBarChartDemo1",
"StatisticalBarChartDemo1.java"));
MutableTreeNode n24 = new DefaultMutableTreeNode(new DemoDescription(
"demo.WaterfallChartDemo1", "WaterfallChartDemo1.java"));
root.add(n1);
root.add(n2);
root.add(n3);
root.add(n4);
root.add(n5);
root.add(n6);
root.add(n7);
root.add(n8);
root.add(n9);
root.add(n10);
root.add(n11);
root.add(n12);
root.add(n13);
root.add(n14);
root.add(n15);
root.add(n16);
root.add(n17);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?