The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet - Graphing Quadratic Function Help

6 replies on 1 page. Most recent reply: Jun 8, 2006 4:02 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Josh Crawford

Posts: 5
Nickname: crawf
Registered: Jun, 2006

Applet - Graphing Quadratic Function Help Posted: Jun 7, 2006 10:36 PM
Reply to this message Reply
Advertisement
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


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Applet - Graphing Quadratic Function Help Posted: Jun 7, 2006 11:01 PM
Reply to this message Reply
2. question: java.awt.geom only offers straight lines and arcs of circles and ellipses.
So what you need to do is to create your own renderer.
The easiest way is to transform the x-coordinate on your screen into the x-coordinate of your function (you only need the offset and scale factor for that), calculate the y-value and transform it to a screen y-value.
Use lines to connect the points, if you do it for every x-value on your screen, no one will ever notice. If this is to slow, then try to do it for every second x-value.

Josh Crawford

Posts: 5
Nickname: crawf
Registered: Jun, 2006

Re: Applet - Graphing Quadratic Function Help Posted: Jun 7, 2006 11:12 PM
Reply to this message Reply
ah yes! that sounds feasible! and a fairly good way to go about it!

i dont suppose you have an example or a link that you could point me to of a guide for this sort of thing? just to help me out a bit more on coding thats all...

Thanks very much for your explanation! its helped! :)

-Crawf

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Applet - Graphing Quadratic Function Help Posted: Jun 8, 2006 2:04 AM
Reply to this message Reply
Here's how I do it.

Note: I know there are classes like AffineTransformation or whatever, so you can draw on a Canvas giving an offset and direction of the y-axis. But I never used them, so I'll convert the y-Values manually. (yScreen = y0Real - yReal)

    double scaleFactor = .5; //Unit is [mm / pixel]
    Point2D.Double p0Screen = new Point2D.Double (15, 300); // this is where the point (0,0) lies on the screen. You can also use fraction digits, they'll be rounded when you draw on the Graphics2D Object.
    int pixelsToDraw = 300;
    for (int xScreenRelative = 0; xScreenRelative < pixelsToDraw; xScreenRelative++) {
        double xReal = xScreenRelative * scaleFactor; //No need to look at the offset, I use the counter as coordinate
        double yReal = functionCall(xReal, [otherParameters]); //
        Point2D.Double pScreen = new Point2D.Double(p0Screen.x + xScreenRelative, p0Screen.y - yReal / scaleFactor); //Draw this Point
    }



I just typed this example here in the editor, didn't compile it. So you might encounter an error or two. But the code should give you the idea.


One more thing: I didn't read anything of your code, because you didn't use java tags. Next time, pelase use them.
Read the "Formatting Your Post" section on the right of the input field.

Josh Crawford

Posts: 5
Nickname: crawf
Registered: Jun, 2006

Re: Applet - Graphing Quadratic Function Help Posted: Jun 8, 2006 3:10 AM
Reply to this message Reply
oh!! im really sorry about that! i put the code in CODE tags...instead of java tags...fixing it right away!

-Crawf

Josh Crawford

Posts: 5
Nickname: crawf
Registered: Jun, 2006

Re: Applet - Graphing Quadratic Function Help Posted: Jun 8, 2006 3:11 AM
Reply to this message Reply
and sorry again! i forgot to say thank you!! you've been so helpful! i'll give that a go!

-Crawf

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Applet - Graphing Quadratic Function Help Posted: Jun 8, 2006 4:02 AM
Reply to this message Reply
You're welcome

Hint: If you have to modify a post, usually it's allready too late. All the blank spaces in front of the lines were removed by then. Just use the java tags for your next problem.

Flat View: This topic has 6 replies on 1 page
Topic: The size of a PopUp menu Previous Topic   Next Topic Topic: Canvas is always on top of my menu

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use