Question
Note: The QuadWindow.java code will be provided at the bottm. I just need help with creating a graph of the quadratic function using GPanel. Thanks.
Note: The QuadWindow.java code will be provided at the bottm. I just need help with creating a graph of the quadratic function using GPanel. Thanks.
Code Source:
QuadWindow.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class QuadWindow extends JFrame{
private JTextField CoA, CoB, CoC;
// roots are readonly fields so labels
private JLabel Root1, Root2, Complex;
private JLabel CoAx, CoBx, CoCx, Root1x, Root2x,Complexno;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public QuadWindow(){
setTitle("Quadratic Fun"); //Title of the window
//Create the five labels
CoAx = new JLabel("A", SwingConstants.CENTER);
CoBx = new JLabel("B", SwingConstants.CENTER);
CoCx = new JLabel("C", SwingConstants.CENTER);
Root1x = new JLabel("Root1", SwingConstants.CENTER);
Root2x = new JLabel("Root2", SwingConstants.CENTER);
Complexno = new JLabel("Complex root", SwingConstants.CENTER);
//Creating 3 text fields
CoA = new JTextField(10);
CoB = new JTextField(10);
CoC = new JTextField(10);
//Creating 3 labels to show the result, initially it's empty
Root1 = new JLabel("", SwingConstants.RIGHT);
Root2 = new JLabel("", SwingConstants.RIGHT);
Complex = new JLabel("", SwingConstants.RIGHT);
//Create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
// we will use a GridLayout we have to display
// a
// b
// c
// root 1
// root 2
//complex no
// 2 buttons
// hence 7 rows 2 columns
Container pane = getContentPane();//Get the container
pane.setLayout(new GridLayout(7, 2));//Set the Layout
//Placing the components in the pane/window
pane.add(CoAx);
pane.add(CoA);
pane.add(CoBx);
pane.add(CoB);
pane.add(CoCx);
pane.add(CoC);
pane.add(Root1x);
pane.add(Root1);
pane.add(Root2x);
pane.add(Root2);
pane.add(Complexno);
pane.add(Complex);
pane.add(calculateB);
pane.add(exitB);
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener//Creates a class on top of the interface
{
public void actionPerformed(ActionEvent e)
{
//Once user click on calculate after that user is not allowed to change the values hence disabling it.
CoA.setEditable(false);
CoB.setEditable(false);
CoC.setEditable(false);
//Storing the inputs into a variable
double a = Double.parseDouble(CoA.getText());
double b = Double.parseDouble(CoB.getText());
double c = Double.parseDouble(CoC.getText());
double root1,root2;
double determinant = b * b - 4 * a * c;
// condition for real and different roots
if(determinant > 0) {
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
Root1.setText("" + root1);
Root2.setText("" + root2);
}
// Condition for real and equal roots
else if(determinant == 0) {
root1 = root2 = -b / (2 * a);
Root1.setText("" + root1);
Root2.setText("" + root2);
}
// If roots are not real
else {
double realPart = -b / (2 *a);
double imaginaryPart = Math.sqrt(-determinant) / (2 * a);
String complex = String.format("%.2f+%.2fi and %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
Complex.setText("" + complex);
}
}
}
private class ExitButtonHandler implements ActionListener//Creates a class on top of the interface
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main ( String[] args)
{
QuadWindow polynomial = new QuadWindow(); // Creating an object of QuadWindow to invoke the constrcutor
}
}
Sample Run:
Extend your QuadWindow, using the GPanel we did in class, to draw a picture of the quadratic function, with axes and tic marks. Just make a JPanel of some reasonable size in your window, turn it into a GPanel, and put the calls into the paint method of the GPanel. Oh,and you'll have to send in the a, b, and c, maybe using a new methods setQuadratic(a,b,c), so that your GPanel knows what quadratic to draw Sample graph (to be added to existing QuadWindow): Xmin -10.0 Xmax 10.0 Ymin -10.0 Ymax 10.0 A 2 B -14 C 24 Draw Graph Extend your QuadWindow, using the GPanel we did in class, to draw a picture of the quadratic function, with axes and tic marks. Just make a JPanel of some reasonable size in your window, turn it into a GPanel, and put the calls into the paint method of the GPanel. Oh,and you'll have to send in the a, b, and c, maybe using a new methods setQuadratic(a,b,c), so that your GPanel knows what quadratic to draw Sample graph (to be added to existing QuadWindow): Xmin -10.0 Xmax 10.0 Ymin -10.0 Ymax 10.0 A 2 B -14 C 24 Draw Graph
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started