001: /*
002: * FunctionPlotApplet.java
003: *
004: * Created on 29. Juni 2002, 02:02
005: */
006:
007: package de.progra.charting.test;
008:
009: import de.progra.charting.*;
010: import de.progra.charting.swing.*;
011: import de.progra.charting.model.*;
012: import de.progra.charting.render.*;
013: import javax.swing.*;
014: import java.awt.*;
015: import java.awt.event.*;
016:
017: /**
018: *
019: * @author smueller
020: */
021: public class FunctionPlotApplet extends javax.swing.JApplet {
022:
023: JTextField function = new JTextField("x^2", 20);
024: JTextField xmin = new JTextField("-10", 10);
025: JTextField xmax = new JTextField("10", 10);
026: JButton action = new JButton("Plot");
027: ChartPanel chart;
028: DefaultChartDataModel model;
029: JLabel status = new JLabel(" ");
030:
031: /** Creates a new instance of FunctionPlotApplet */
032: public FunctionPlotApplet() {
033: initComponents();
034: }
035:
036: protected void initComponents() {
037: JPanel bag = new JPanel();
038: bag.setLayout(new BorderLayout());
039:
040: JPanel panel = new JPanel();
041: panel.setLayout(new GridLayout(3, 2));
042:
043: panel.add(new JLabel("f(x) = "));
044: panel.add(function);
045: panel.add(new JLabel("Minimum x-value: "));
046: panel.add(xmin);
047: panel.add(new JLabel("Maximum x-value: "));
048: panel.add(xmax);
049:
050: JPanel buttonPanel = new JPanel();
051: buttonPanel.add(action);
052:
053: action.addActionListener(new ActionListener() {
054: public void actionPerformed(ActionEvent e) {
055: actionPressed(e);
056: }
057: });
058:
059: bag.add(panel, BorderLayout.NORTH);
060: bag.add(buttonPanel, BorderLayout.WEST);
061:
062: this .getContentPane().add(bag, BorderLayout.NORTH);
063:
064: model = FunctionPlotter.createChartDataModelInstance(-10.0,
065: 10.0, 2000, "x^2");
066: chart = new ChartPanel(model, "Plotting x^2",
067: DefaultChart.LINEAR_X_LINEAR_Y);
068: chart.getCoordSystem().setPaintLabels(false);
069: chart.setLegend(null);
070: chart.addChartRenderer(new LineChartRenderer(chart
071: .getCoordSystem(), model), 1);
072: this .getContentPane().add(chart, BorderLayout.CENTER);
073:
074: this .getContentPane().add(status, BorderLayout.SOUTH);
075: }
076:
077: public void actionPressed(ActionEvent evt) {
078: xmin.setForeground(Color.black);
079: xmax.setForeground(Color.black);
080: function.setForeground(Color.black);
081: status.setText(" ");
082:
083: double x_min = 0.0;
084: double x_max = 1.0;
085: try {
086: x_min = Double.parseDouble(xmin.getText());
087: } catch (Exception e) {
088: xmin.setForeground(Color.red);
089: status.setText("The minimum x-value was no valid number.");
090: return;
091: }
092:
093: try {
094: x_max = Double.parseDouble(xmax.getText());
095: } catch (Exception e) {
096: xmax.setForeground(Color.red);
097: status.setText("The maximum x-value was no valid number.");
098: return;
099: }
100:
101: try {
102: model = FunctionPlotter.createChartDataModelInstance(x_min,
103: x_max, 2000, function.getText());
104: } catch (Exception e) {
105: function.setForeground(Color.red);
106: status.setText("The function couldn't be parsed.");
107: return;
108: }
109:
110: chart.setTitle(new Title("Plotting " + function.getText()));
111: chart.setLegend(null);
112: chart.setChartDataModel(model);
113: chart.setCoordSystem(new CoordSystem(model));
114: chart.getCoordSystem().setPaintLabels(false);
115: chart.addChartRenderer(new LineChartRenderer(chart
116: .getCoordSystem(), model), 1);
117: chart.revalidate();
118: repaint();
119: }
120: }
|