Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Applet - Graphing Quadratic Function Help

807569Jun 8 2006 — edited Jun 24 2006
Hi!

Im making an applet to graph a quadratic function...now im not going to be rude and say "tell me how to do it!" but i was just wondering if i could get some assistance on making it...

Now of course, i've had a go at it, and i've made an applet to display the z intercepts, y intercept, and vertex (turning point). This is determined by the user inputs of the three coefficients a, b, and c.

First question, is this all the information i will need?

Secondly, i am struggling to figure out how to put this information into a graph and make it a parabola...

Thank you very much for your help!

Ps. my applet is below if you wish to have a look, i've made a crude graph if that helps as well...
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Graph2 extends Applet implements ActionListener {

    TextField aInput, bInput, cInput;
    double a, b, c;
    boolean notFirst;
    Button calcButton;
    int size = 400;

    public void init() {
        setLayout(null);
        resize(600, 500);
        Label labelA = new Label("Value a:");
        labelA.setBounds(412, 40, 50, 20);
        Label labelB = new Label("Value b:");
        labelB.setBounds(412, 70, 50, 20);
        Label labelC = new Label("Value c:");
        labelC.setBounds(412, 100, 50, 20);
        aInput = new TextField("2", 5);
        aInput.setBounds(470, 40, 80, 20);
        bInput = new TextField("5", 5);
        bInput.setBounds(470, 70, 80, 20);
        cInput = new TextField("-12", 5);
        cInput.setBounds(470, 100, 80, 20);
        calcButton = new Button("Solve");
        calcButton.setBounds(460, 130, 45, 20);
        add(labelA);
        add(aInput);
        add(labelB);
        add(bInput);
        add(labelC);
        add(cInput);
        add(calcButton);
        aInput.addActionListener(this);
        bInput.addActionListener(this);
        cInput.addActionListener(this);
        calcButton.addActionListener(this);
        notFirst = false;
    }

    public void makeGraph(Graphics g) {
        g.setColor(Color.lightGray);
        // Draw grid
        for (int y = 0; y <= size; y = y + 10) {
            g.drawLine(1, y, size, y);
        }
        for (int x = 0; x <= size; x = x + 10) {
            g.drawLine(x, 1, x, size);
        }
        g.setColor(Color.red);
        // Draw y axis
        g.drawLine(size / 2, 0, size / 2, size);
        for (int i = 0; i <= size; i = i + 20) {
            g.drawLine(size / 2 - 5, i, size / 2 + 5, i);
        }
        g.setColor(Color.blue);
        // Draw x axis
        g.drawLine(0, size / 2, size, size / 2);
        for (int j = 0; j <= size; j = j + 20) {
            g.drawLine(j, size / 2 - 5, j, size / 2 + 5);
        }
        g.setColor(Color.black);
        // Draw positive x axis numbers
        for (int n = 0; n <= 5; n++) {
            g.drawString("" + n * 2, (size / 2 - 4) + n * 40, size / 2 + 17);
        }
        // Draw negative x axis numbers
        for (int n = 1; n <= 5; n++) {
            g.drawString("-" + n * 2, (size / 2 - 6) - n * 40, size / 2 + 17);
        }
        // Draw positive y axis numbers
        for (int n = 1; n <= 5; n++) {
            g.drawString("" + n * 2, size / 2 - 21, (size / 2 + 5) - n * 40);
        }
        // Draw negative y axis numbers
        for (int n = 1; n <= 5; n++) {
            g.drawString("-" + n * 2, size / 2 - 23, (size / 2 + 6) + n * 40);
        }
    }

    public void paint(Graphics g) {
        int yValue = 180;
        makeGraph(g);
        g.drawString("y = ax\u00B2 + bx + c", 420, 20);
        if (notFirst) {
            double zero = ((( -b) + (Math.sqrt((b * b) - (4 * (a * c))))) / (2 * a));
            double zero2 = ((( -b) - (Math.sqrt((b * b) - (4 * (a * c))))) / (2 * a));
            g.drawString("a = " + a, 420, yValue);
            g.drawString("b = " + b, 420, yValue + 20);
            g.drawString("c = " + c, 420, yValue + 40);
            if (a == 0) {
                g.drawString("a Cannot Be 0", 420, yValue + 80);
            } else {
                if (((b * b) - (4 * a * c)) < 0) {
                    g.drawString("No Real Solution", 420, yValue + 80);
                } else {
                    g.drawString("Zero = (" + zero + ", 0)", 420, yValue + 80);
                    g.drawString("Zero = (" + zero2 + ", 0)", 420, yValue + 100);
                    g.drawString("Y Intercept = (0, " + c + ")", 420, yValue + 120);
                    double turnX = -b / (2 * a);
                    double turnY = c - (Math.pow(b, 2)) / (4 * a);
                    g.drawString("Vertex = (" + turnX + ", " + turnY + ")", 420, yValue + 140);
                    double d = (Math.pow(b, 2)) - 4 * a * c;
                    g.drawString("Discriminant = " + d, 420, yValue + 160);
                    if (a < 0) {
                        g.drawString("Parabola = Down", 420, yValue + 180);
                    } else {
                        g.drawString("Parabola Direction = Up", 420, yValue + 180);
                    }
                }
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        notFirst = true;
        if (e.getSource() == calcButton) {
            a = Double.parseDouble(aInput.getText());
            b = Double.parseDouble(bInput.getText());
            c = Double.parseDouble(cInput.getText());
            if (aInput.getText().equals("")) {
                a = 0;
            }
            repaint();
        }
    }
}
-Crawf
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 22 2006
Added on Jun 8 2006
3 comments
157 views