Question

I have to design and implement an application that draws the graph of the equation of ax^2 + bx + c where the values of a b and c are set using sliders. I am editing my original post and thus am going to do my best to post an sscce. My code is below. Everything compiles and runs. My one question is why is my graph not displaying anything when the sliders are moved? Here are my 2 class files:

import java.awt.*;
import javax.swing.*;

public class QuadraticGraph
{

   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Quadratic Grapher");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add(new QuadraticPanel());

      frame.pack();
      frame.setVisible(true);
   }
}



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

public class QuadraticPanel extends JPanel
{
   private JPanel controls, quadpanel;
   private JSlider aslider, bslider, cslider;
   private JLabel alabel, blabel, clabel;

   //-----------------------------------------------------------------
   //  Sets up the sliders and their labels, aligning them along
   //  their left edge using a box layout.
   //-----------------------------------------------------------------
   public QuadraticPanel()
   {
      aslider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
      aslider.setMajorTickSpacing (50);
      aslider.setMinorTickSpacing (10);
      aslider.setPaintTicks (true);
      aslider.setPaintLabels (true);
      aslider.setAlignmentX (Component.LEFT_ALIGNMENT);
      bslider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
      bslider.setMajorTickSpacing (50);
      bslider.setMinorTickSpacing (10);
      bslider.setPaintTicks (true);
      bslider.setPaintLabels (true);
      bslider.setAlignmentX (Component.LEFT_ALIGNMENT);

      cslider = new JSlider (JSlider.HORIZONTAL, -25, 25, 0);
      cslider.setMajorTickSpacing (50);
      cslider.setMinorTickSpacing (10);
      cslider.setPaintTicks (true);
      cslider.setPaintLabels (true);
      cslider.setAlignmentX (Component.LEFT_ALIGNMENT);

      SliderListener listener = new SliderListener();
      aslider.addChangeListener (listener);
      bslider.addChangeListener (listener);
      cslider.addChangeListener (listener);

      alabel = new JLabel ("A: 0");
      alabel.setAlignmentX (Component.LEFT_ALIGNMENT);
      blabel = new JLabel ("B: 0");
      blabel.setAlignmentX (Component.LEFT_ALIGNMENT);
      clabel = new JLabel ("C: 0");
      clabel.setAlignmentX (Component.LEFT_ALIGNMENT);
      controls = new JPanel();
      BoxLayout layout = new BoxLayout (controls, BoxLayout.Y_AXIS);
      controls.setLayout (layout);
      controls.add (alabel);
      controls.add (aslider);
      controls.add (Box.createRigidArea (new Dimension (0, 20)));
      controls.add (blabel);
      controls.add (bslider);
      controls.add (Box.createRigidArea (new Dimension (0, 20)));
      controls.add (clabel);
      controls.add (cslider);

      quadpanel = new JPanel();
      quadpanel.setPreferredSize (new Dimension (500, 500));
      quadpanel.setBackground (Color.white);

      add (controls);
      add (quadpanel);
   }
   //*****************************************************************
   //  Represents the listener for all three sliders.
   //*****************************************************************
   private class SliderListener implements ChangeListener
   {
      private double a, b, c, x, y, g, h;

      //--------------------------------------------------------------
      //  Gets the value of each slider, then updates the labels and
      //  the color panel.
      //--------------------------------------------------------------
      public void stateChanged (ChangeEvent event)
      {
         a = aslider.getValue();
         b = bslider.getValue();
         c = cslider.getValue();

         alabel.setText ("A: " + a);
         blabel.setText ("B: " + b);
         clabel.setText ("C: " + c);

      }
         public void  paintComponent (Graphics page)

         {

         x = (-b + (Math.sqrt((b*b - ((4 * a * c))))))/ (2 * a);
         y= (a*(Math.pow(x,2)))+(b*x)+(c);
         int g = (int)Math.round(x);
         int h = (int)Math.round(y);
         page.setColor (Color.black);
         page.drawOval (g, h, 1, 1);

         }
      }
   }
Was it helpful?

Solution

i guess you're very new to java, so here's some starters help ^^

create a content Panel and set some layout to the panel; add the sliders and a drawing-panel to your content panel;

you're doing it right, adding an change listener to the slider, but they should redraw the drawing-panel.

i'll add this snippets to make it easier for you ^_^

private JPanel drawPanel; //don't forget to create a proper one! override paint in that panel!
private int a,b,c;
public QuadraticPanel(){ //constructor
    setLayout(new BoderLayout();
    JSlider aSidler = new JSlider();
    slider.addChangeListener(new ChangeListener(){
        @Override
        public void stateChanged(ChangeEvent arg0) {
              a = arg0.getValue(); //setting a value
              //it might even be better to calculate the value
              //BEFORE you redraw
              //recalcEquotiation()
              drawPanel.repaint(); //and redraw the paint-panel
        }           
    });
    add(aSlider, Borderlayout.WEST); //add more sliders with better layouts or subcomponents
    add(drawPanel, BorderLayout.CENTER);
 }

don't forget - these are just snippets, you'll have to do some work on your own...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top