Drawing the Quadratic formula with sliders on a JP

2019-02-26 12:31发布

问题:

So, I'm trying to make a program where you can input the quadratic formula (ax^2+bx+c) via sliders. Then it draws a graph as you adjust for A, B, and C.

Issues:

I want the stuff I wrote in super paint and the sliders to be in one place. The sliders are in place when I run it. There's space with the correct background where I want my graph in the panel but no actual graph.

Here's my driver class:

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

public class quadraticslider
{

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

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

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

Here's the panel class:

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

public class quadraticpanel extends JPanel
{
   private JPanel controls, graphPanel;
   private JSlider ASlider, BSlider, CSlider;
   private JLabel ALabel, BLabel, CLabel;
   double A, B, C, x,Y;

  //
  //SLIDERS YO
  //
   public quadraticpanel()
   {
      ASlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 0);
      ASlider.setMajorTickSpacing (50);
      ASlider.setMinorTickSpacing (10);
      ASlider.setPaintTicks (true);
      ASlider.setPaintLabels (true);
      ASlider.setAlignmentX (Component.LEFT_ALIGNMENT);

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

      CSlider = new JSlider (JSlider.HORIZONTAL, 0, 255, 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);



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

      add (controls); 
      add (graphPanel);




   }   

    //Here I'm taking the equation, running it through -10 to 10
    //It takes the doubles from the equation, converts
    //it to int then draws the quadratic formula in dots.

     public void paintComponent(Graphics page)
   {  

     super.paintComponent (page);

     for ( x=-10; x <= 10; x++)
     {
         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.fillOval (g, h, 1, 1);
      }


    }


   public class SliderListener implements ChangeListener
   {
///
///Reads the user input via slider.
///

      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);

      }
   }
}

回答1:

These examples using JFreeChart may be of interest. As shown here, you can animate the rendering using SwingWorker, and this example updates a chart using a JSlider.

Addendum: This variation of your code may guide you going forward. Note,

  • Override relevant methods in your graphPanel.

  • Scale and invert coordinates, as shown here.

  • Consider JSpinner for fractional values.

  • Use constants for consistency.

  • Use common naming conventions for clarity.

  • See also Initial Threads.

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

/** @see https://stackoverflow.com/a/20556929/230513 */
public class QuadraticSlider {

    private static final int N = 500;
    private static final int A = 1;
    private static final int B = 0;
    private static final int C = 0;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Quadratic Slider");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new QuadraticPanel());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    private static class QuadraticPanel extends JPanel {

        private Box controls;
        private JPanel graphPanel;
        private JSlider aSlider, bSlider, cSlider;
        private JLabel aLabel, bLabel, cLabel;
        double a, b, c, x, y;

        public QuadraticPanel() {
            aSlider = new JSlider(JSlider.HORIZONTAL, -25, 25, A);
            aSlider.setMajorTickSpacing(10);
            aSlider.setMinorTickSpacing(5);
            aSlider.setPaintTicks(true);
            aSlider.setPaintLabels(true);
            aSlider.setAlignmentX(Component.LEFT_ALIGNMENT);

            bSlider = new JSlider(JSlider.HORIZONTAL, -10, 10, B);
            bSlider.setMajorTickSpacing(5);
            bSlider.setMinorTickSpacing(1);
            bSlider.setPaintTicks(true);
            bSlider.setPaintLabels(true);
            bSlider.setAlignmentX(Component.LEFT_ALIGNMENT);

            cSlider = new JSlider(JSlider.HORIZONTAL, -100, 100, C);
            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");
            bLabel = new JLabel("b: 0");
            cLabel = new JLabel("c: 0");
            controls = new Box(BoxLayout.Y_AXIS);
            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);

            graphPanel = new JPanel() {
                private static final int SCALE = 5;
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(N, N);
                }

                @Override
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    for (x = -10; x <= 10; x++) {
                        y = a * x * x + b * x + c;
                        g.setColor(Color.black);
                        int w = (int) (x * SCALE) + N / 2;
                        int h = (int) (-y * SCALE) + N / 2;
                        g.fillOval(w, h, 5, 5);
                    }
                }
            };
            graphPanel.setBackground(Color.white);

            add(controls);
            add(graphPanel);
            listener.stateChanged(null);
        }

        class SliderListener implements ChangeListener {

            @Override
            public void stateChanged(ChangeEvent event) {
                a = aSlider.getValue() / 5d;
                b = bSlider.getValue();
                c = cSlider.getValue();

                aLabel.setText("a: " + a);
                bLabel.setText("b: " + b);
                cLabel.setText("c: " + c);

                repaint();
            }
        }
    }
}


回答2:

_"error: possible loss of precision Y = (A*(Math.pow(x,2)))+(B*x)+(C); ^ required: int found: double"_

All your int variables int A, B, C, x,Y;. Make them doubles. double A, B, C, x,Y;



回答3:

I modified the code so that it solves for x given a b and c. Then plugs x back in and solves for y. I'm still not getting it to draw though. I also took out the loop since the sliders are setting the a b and c values which lead to x. Anyone know why it won't draw?

package quadraticslider;

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

public class quadraticslider
{

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

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

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

package quadraticslider;

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

public class quadraticpanel extends JPanel
{
   private JPanel controls, graphPanel;
   private JSlider ASlider, BSlider, CSlider;
   private JLabel ALabel, BLabel, CLabel;
   double A, B, C, x,Y;


   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);



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

      add (controls); 
      add (graphPanel);




   }   



     public void paintComponent(Graphics page)
   {  

     super.paintComponent (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);



    }


   public class SliderListener implements ChangeListener
   {
///
///Reads the user input via slider.
///

      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);

      }

} }