1

I am trying to add function plotting to my Java Swing application. From my main app window I want to launch a JDialog with a simple JLabel = "f(x) = " and a JTextField, where user would be asked to enter their own function they want to plot.

I am trying to adapt the demo sample included so that I could redirect the text from JTextField to this class, but I literally got stuck - no ideas on how to do that.

Here's the code for the class that shows the graph:

class OknoFunkcji extends ApplicationFrame {



public OknoFunkcji(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    setContentPane(chartPanel);
}

/**
 * Creates a chart.
 * 
 * @param dataset
 * 
 * @return returns chart instance
 */
private static JFreeChart createChart(XYDataset dataset) {
    // create the chart...
    JFreeChart chart = ChartFactory.createXYLineChart(
        "OknoFunkcji ",       // chart title
        "X",                      // x axis label
        "Y",                      // y axis label
        dataset,                  // data
        PlotOrientation.VERTICAL,  
        true,                     // include legend
        true,                     // tooltips
        false                     // urls
    );

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.getDomainAxis().setLowerMargin(0.0);
    plot.getDomainAxis().setUpperMargin(0.0);
    return chart;
}

/**
 * Creates a sample dataset.
 * 
 * @return A sample dataset.
 */
public static XYDataset createDataset() {
    XYDataset result = DatasetUtilities.sampleFunction2D(new X2(), 
            -10.0, 10.0, 40, "f(x)");
    return result;
}

public static JPanel createDemoPanel() {
    JFreeChart chart = createChart(createDataset());
    return new ChartPanel(chart);
}

static class X2 implements Function2D {


    public double getValue(double x) {
        return x * x + 2;
    }

}


}

How can I alter the getValue method so it uses user-defined function? Here's my JDialog code:

class PanelFunkcji extends JDialog implements ActionListener {
private JLabel lFunkcja;
private JTextField tFunkcja;
private JButton bOK, bCancel;

public PanelFunkcji(JFrame owner) {
    super(owner, "Wprowadzanie funkcji", true);
    setSize(250,120);
    setLayout(null);

    lFunkcja = new JLabel("f(x) = ");
    lFunkcja.setBounds(10, 10, 100, 20);
    add(lFunkcja);

    tFunkcja = new JTextField();
    tFunkcja.setBounds(40, 10, 180, 20);
    add(tFunkcja);

    bOK = new JButton("OK");
    bOK.setBounds(10, 40, 100, 20);
    add(bOK);
    bOK.addActionListener(this);

    bCancel = new JButton("Anuluj");
    bCancel.setBounds(120, 40, 100, 20);
    add(bCancel);
    bCancel.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    Object z = e.getSource();
    if(z == bOK) {
        //TO-DO: Send the function from tFunkcja to the plotter
        setVisible(false);
    }
    else if (z == bCancel) {
        setVisible(false);          
    }

}



}

Sorry for some strings or comments in Polish, hope it isn't much of a problem to you. I would really appreciate any help with this, as I need it for like yesterday and I do not really have time to learn it as I would normally do.

Asunez
  • 2,327
  • 1
  • 23
  • 46

1 Answers1

1

In your implementation of Function2D, substitute Double.valueOf(x) for the variable x in the string s expressing your chosen function. Invoke eval(s) from an available ScriptEngine as shown here. Return the double value of the result.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045