Bisection Calculator using SwingWorker - Java Swing

Java examples for Swing:Swing Thread

Description

Bisection Calculator using SwingWorker

Demo Code

import java.awt.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXGraph.Plot;
import org.jdesktop.swingx.JXTable;

public class BisectionCalculator extends SwingWorker<Integer, Object[]> {
    JXTable jt;/*from   w ww. j  a v  a2 s  .c  o  m*/
    DefaultTableModel dm;
    Plot d2;
    int precision = 6;
    double point1;
    double point2;
    public int iteration;
    public double root;

    public BisectionCalculator(final Object jt, final DefaultTableModel dm,
            final Plot d2, int point1, int point2, int precision) {
        this.jt = (JXTable) jt;
        this.dm = dm;
        this.d2 = d2;
        this.point1 = point1;
        this.point2 = point2;
        this.precision = precision;
    }

    public static void main(String args[]) {

    }

    @Override
    protected Integer doInBackground() throws Exception {
        double del = 1e-6;
        double midpoint = (point1 + point2) / 2;
        int iteration = 0;
        int flag = 0;

        while (Math.abs(d2.compute(point2) - d2.compute(point1)) > del) {
            //while(iteration < 100){
            //System.out.println(Math.abs(d2.compute(point2)-d2.compute(point1)) + " > " +del);
            publish(recordData(iteration, point1, d2.compute(point1),
                    midpoint, d2.compute(midpoint), point2,
                    d2.compute(point2),
                    Math.abs(d2.compute(point2) - d2.compute(point1))
                            / (2 ^ (iteration + 1))));
            if (d2.compute(point1) * d2.compute(midpoint) < 0)
                point2 = midpoint;
            else
                point1 = midpoint;
            if (ExpressionPlot.Round(d2.compute(midpoint), precision) == 0
                    && flag == 0) {
                root = ExpressionPlot.Round(midpoint, precision);
                this.iteration = iteration + 1;
                flag = 1;
            }
            System.out
                    .println(ExpressionPlot.Round(d2.compute(midpoint), 1));
            midpoint = (point1 + point2) / 2;
            iteration++;
        }
        System.out.println("root is " + ExpressionPlot.Round(point1, 6));

        return iteration - 1;
    }

    private Object[] recordData(Integer iteration, double point1,
            double compute, double midpoint, double compute2,
            double point2, double compute3, double error) {
        Object[] data = { iteration,
                ExpressionPlot.Round(point1, precision),
                ExpressionPlot.Round(compute, precision),
                ExpressionPlot.Round(midpoint, precision),
                ExpressionPlot.Round(compute2, precision),
                ExpressionPlot.Round(point2, precision),
                ExpressionPlot.Round(compute3, precision),
                ExpressionPlot.Round(error, precision) };
        return data;
    }

    protected void process(java.util.List<Object[]> publishedVals) {
        for (int i = 0; i < publishedVals.size(); i++)
            dm.addRow(new Object[] { publishedVals.get(i)[0],
                    publishedVals.get(i)[1], publishedVals.get(i)[2],
                    publishedVals.get(i)[3], publishedVals.get(i)[4],
                    publishedVals.get(i)[5], publishedVals.get(i)[6],
                    publishedVals.get(i)[7] });
    }

    protected void done() {
        try {
            System.out.print(get());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Bisection.java

Demo Code

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.jdesktop.swingx.JXGraph;
import org.jdesktop.swingx.JXGraph.Plot;
import org.jdesktop.swingx.JXTable;
import org.nfunk.jep.JEP;

public class Bisection extends JFrame implements Runnable {




    private static String[] columnNames;
    private static JFrame frame;
    private static BisectionCalculator bisectionCalc;
    private static JXGraph.Plot d;
    public static int iteration;
    public static int flag = 0;

    private DefaultTableModel model;
    private JXTable table;
    private Object[][] datas;
    private JScrollPane pane;
    private JTableHeader header;

    @SuppressWarnings("static-access")
    public Bisection(ExpressionPlot d, int point1, int point2, int precision) {
        this.d = d;
        frame = new JFrame("Data");
        columnNames = new String[] { "i", "a", "f(a)", "c", "f(c)", "b",
                "f(b)", "error" };

        datas = new Object[][] { { "", "", "", "", "", "", "" }, };
        model = new DefaultTableModel(datas, columnNames);
        table = new JXTable(model);

        frame.setLayout(new BorderLayout());
        frame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        frame.add(table, BorderLayout.CENTER);

        pane = new JScrollPane(table);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        header = table.getTableHeader();

        bisectionCalc = new BisectionCalculator(table, model, d, point1,
                point2, precision);//from w  w w.  j  a v  a2 s.com
        bisectionCalc.execute();

        frame.add(pane);
        frame.setSize(800, 800);
        frame.setUndecorated(true);
        frame.getRootPane()
                .setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        frame.setVisible(true);
    }

    //para test rani
    public static double getRoot(Plot d2, double point1, double point2,
            int precision) {
        double del = 1e-6;
        double midpoint = (point1 + point2) / 2;
        iteration = -1;
        while (Math.abs(d2.compute(point2) - d2.compute(point1)) > del) {
            if (d2.compute(point1) * d2.compute(midpoint) < 0) {
                point2 = midpoint;
            } else
                point1 = midpoint;
            midpoint = (point1 + point2) / 2;
            iteration++;
        }

        return ExpressionPlot.Round(midpoint, precision);
    }

    //para test rani
    private static void setRootFlag(double point1, double point2,
            double midpoint) {
        flag = 1;
    }

    //para test rani
    public static void main(String args[]) {
        //Bisection bm = new Bisection();
        //Thread worker = new Thread(bm);
        //worker.start();
    }

    @Override
    public void run() {
    }

    public double getRoot() {
        return bisectionCalc.root;
    }

    public int getIteration() {
        return bisectionCalc.iteration - 1;
    }
}

Related Tutorials