-1

Hi there I am quite new to java, however I need to plot a graph in JApplet. I have managed to declare all variables and classes, the equation is working (when compiled on it's own). but all I get is a strait line! can anyone tell me what am I doing wrong please?! in this applet the user will be asked to insert the values for abcd and the min and max values for the x axes

here is my code.......any help will be gratefully appreciated :)

package CubicEquationSolver;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public class graphApplet extends JApplet implements ActionListener {

    private static class g {
        public g() {
        }
    }

    int a;
    int b;
    int c;
    int d;

    int minimumX;
    int maximumX;
    int minimumY;
    int maximumY;

    int xCoOrdinates[];
    int yCoOrdinates[];

    int y;

    // Setting labels
    JLabel labelA = new JLabel("Enter value of A");
    JLabel labelB = new JLabel("Enter value of B");
    JLabel labelC = new JLabel("Enter value of C");
    JLabel labelD = new JLabel("Enter value of D");
    JLabel labelMinX = new JLabel("Minimum X value");
    JLabel labelMaxX = new JLabel("Maximum X value");
    JLabel message = new JLabel("Please insert your values");

    // Values will be entered here using JTextField
    JTextField textA = new JTextField();
    JTextField textB = new JTextField();
    JTextField textC = new JTextField();
    JTextField textD = new JTextField();
    JTextField minX = new JTextField();
    JTextField maxX = new JTextField();
    JTextField ref = new JTextField("Enter value 0-1");

    // declaring the layout for layout manager
    JPanel north = new JPanel();
    JPanel south = new JPanel();
    JPanel west = new JPanel();
    JPanel east = new JPanel();
    JPanel center = new JPanel();

    // declaring buttons using JButtons
    JButton calculate = new JButton("Calculate");
    JButton delete = new JButton("Delete");
    JButton refine = new JButton("Refine");

    // Calling from equation class
    // equation eq = new equation();

    private JPanel panel;
    private int width = center.getWidth();
    private int height = center.getHeight();

    @Override
    public void init() {
        // setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container c = this.getContentPane();
        this.setSize(900, 480);
        this.setVisible(true);

        // listener to buttons
        calculate.addActionListener(this);
        delete.addActionListener(this);
        refine.addActionListener(this);

        // listeer to user's input
        textA.addActionListener(this);
        textB.addActionListener(this);
        textC.addActionListener(this);
        textD.addActionListener(this);
        minX.addActionListener(this);
        maxX.addActionListener(this);
        ref.addActionListener(this);

        // assigning colours to panels to be distinguished
        north.setBackground(Color.LIGHT_GRAY);
        south.setBackground(Color.LIGHT_GRAY);
        west.setBackground(Color.YELLOW);
        east.setBackground(Color.GREEN);
        center.setBackground(Color.GRAY);

        // Declaring border
        BorderLayout layoutBorder = new BorderLayout();
        // setting up the grid (x rows, y clumns, space, space)
        GridLayout layoutGrid = new GridLayout(2, 8, 4, 4);

        // layout grid
        north.setLayout(layoutGrid);
        // set labels
        north.add(labelA);
        north.add(labelB);
        north.add(labelC);
        north.add(labelD);
        north.add(labelMinX);
        north.add(labelMaxX);
        north.add(ref);

        // calculate button
        north.add(calculate);
        // text boxes
        north.add(textA);
        north.add(textB);
        north.add(textC);
        north.add(textD);
        north.add(minX);
        north.add(maxX);
        north.add(refine);
        // delete button
        north.add(delete);

        south.add(message);

        // border layout
        c.add(north, BorderLayout.NORTH);
        c.add(south, BorderLayout.SOUTH);
        c.add(center, BorderLayout.CENTER);
        // c .add(west, BorderLayout.WEST);
        // c .add(east, BorderLayout.EAST);

        // panel = new JPanel();
        // panel.setPreferredSize(new Dimension(width, height));
        // panel.setBackground(Color.GRAY);
        // center.add(panel);

    }

    @Override
    public void actionPerformed(ActionEvent e) // throws NumberFormatException
    {
        // dafault message will be <message> -- "Please insert values"
        message.setText(e.getActionCommand());
        // when button "Delete" is pressed all values in text firlds will turn
        // null
        if (e.getActionCommand().equals("Delete")) {
            message.setForeground(Color.DARK_GRAY);

            textA.setText(null);
            textB.setText(null);
            textC.setText(null);
            textD.setText(null);
            minX.setText(null);
            maxX.setText(null);

            repaint();
        } else if (e.getActionCommand().equals("Calculate"))
            // when "Calculate" button is pressed, values will be attached to
            // equation
            try {
                message.setForeground(Color.DARK_GRAY);

                // -------------------------------------------------
                a = Integer.parseInt(textA.getText());
                b = Integer.parseInt(textB.getText());
                c = Integer.parseInt(textC.getText());
                d = Integer.parseInt(textD.getText());
                minimumX = Integer.parseInt(minX.getText());
                maximumX = Integer.parseInt(maxX.getText());

                System.out.println("center.getWidth() " + center.getWidth());
                System.out.println("center.getHeight() " + center.getHeight());
                System.out.println("minimum " + minX.getText());
                System.out.println("maximum " + maxX.getText());
                System.out.println("a " + textA.getText());
                System.out.println("b " + textB.getText());
                System.out.println("c " + textC.getText());
                System.out.println("d " + textD.getText());
                // ------------------------------------------------------

                message.setText("This is the result for " + "A "
                        + textA.getText() + ", B " + textB.getText() + ", C "
                        + textC.getText() + ", D " + textD.getText());
                draw();
            }

            catch (NumberFormatException ex)
            // if user inputs other than numbers, a warning message in the south
            // panel will show
            {
                message.setText("Please insert numerical value in "
                        + ex.getMessage());
                message.setForeground(Color.red);
                message.setFont(new Font("Tahoma", Font.BOLD, 12));
            }

        else if (e.getActionCommand().equals("Refine")) {
            // for refine
        }

    }

    // ===================================================================================

    private void calculation() {
        xCoOrdinates = new int[(maximumX - minimumX) + 1];
        yCoOrdinates = new int[(maximumX - minimumX) + 1];

        for (int i = 0; i < xCoOrdinates.length; i++)
        // for(int j = 0; j < yCoOrdinates.length; j++)

        {
            // generating the x co-ordinates and storing them in arrays
            xCoOrdinates[i] = minimumX + i;
            // generating the y co-ordinates using the formula given
            y = ((a * (int) Math.pow(i, 3)) + (b * (int) Math.pow(i, 2))
                    + (c * i) + (d));
            // storing y co-ordinates
            yCoOrdinates[i] = y;
            // displaying results
            // System.out.println("X = " + i + "   Y = " + getY());
            System.out.println("These are the values of X = " + i);
        }

        // printing the y axes values
        for (int i = 0; i < yCoOrdinates.length; i++) {

            System.out.println("this is the extracted Y " + yCoOrdinates[i]);

        }

        maximumX = xCoOrdinates[0];
        maximumX = xCoOrdinates[0];
        for (int i = 1; i < yCoOrdinates.length; i++) {
            if (yCoOrdinates[i] > maximumX)
                maximumX = xCoOrdinates[i];
            else if (yCoOrdinates[i] < minimumX)
                minimumX = xCoOrdinates[i];
        }

        System.out.println("MAXX is " + maximumX);
        System.out.println("MINX is " + minimumX);

        maximumY = yCoOrdinates[0];
        minimumY = yCoOrdinates[0];
        for (int i = 1; i < yCoOrdinates.length; i++) {
            if (yCoOrdinates[i] > maximumY)
                maximumY = yCoOrdinates[i];
            else if (yCoOrdinates[i] < minimumY)
                minimumY = yCoOrdinates[i];
        }
        System.out.println("MAXY is " + maximumY);
        System.out.println("MINY is " + minimumY);

    }

    // =================================================================================================

    public void draw() {
        Graphics g = center.getGraphics();
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, center.getWidth(), center.getHeight());
        // g.fillRect(25,25, center.getWidth()-50, center.getHeight()-50);
        double x, y, nextX, nextY;
        int xPoint; // = 0;
        int yPoint; // = 0;
        int nextXpoint; // = 0;
        int nextYpoint; // = 0;

        g.setColor(Color.BLUE);

        for (xPoint = 0; xPoint <= (double) center.getWidth(); xPoint++) {
            x = scaleX(xPoint);
            y = equation(x);
            yPoint = scaleY(y);
            nextXpoint = xPoint + 1;
            nextX = scaleX(nextXpoint);
            nextY = equation(nextX);
            nextYpoint = scaleY(nextY);
            g.drawLine(xPoint, yPoint, nextXpoint, nextYpoint);
            // System.out.println("equation  --->" + eq.getY());
        }

    }

    private double equation(double x) {
        return y;
        // return a*x*x*x + b*x*x + c*x + d;
    }

    private double scaleX(int xPoint) {
        int minXstart = minimumX;
        int maxXend = maximumX;
        double xScale = (double) center.getWidth() / (maxXend - minXstart);
        return (xPoint - (center.getWidth() / 2)) / xScale;
    }

    private int scaleY(double y) {
        int minYstart = minimumY;
        int maxYend = maximumY;
        int yCoord;
        double yScale = (double) center.getHeight() / (maxYend - minYstart);
        yCoord = (int) (-y * yScale) + (int) (center.getHeight() / 2);
        return yCoord;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1527093
  • 19
  • 1
  • 1

3 Answers3

3

There are 3rd party libraries out there to do this type of work for you. Check out:

JFreeChart

Charts4J

Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

Short term help: Check your output, do the values of x and y vary as expected? are they actually used during plotting? Use a debugger to step through your code. Then come back with a more focused questions.

Mid term (within the next two days): Please buy and read the following two books:

http://www.amazon.com/Pragmatic-Programmer-Andrew-Hunt/dp/020161622X

http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?s=books&ie=UTF8&qid=1342369060&sr=1-1&keywords=clean+code

Classes as long as the one you posted are not acceptable, and make it next to impossible to understand the revlevant part. This is the reason why you received the down votes I'd guess.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
3

I'm a JFreeChart fan, but you can also plot graphs using Cartesian coordinates in Java 2D, as suggested here.

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