Java中的二次方程与滑块

时间:2013-12-13 00:30:35

标签: java listener graphing sliders

我必须设计并实现一个应用程序,该应用程序绘制ax ^ 2 + bx + c等式的图形,其中使用滑块设置b和c的值。我正在编辑我的原始帖子,因此我将尽力发布sscce。我的代码如下。一切都编译并运行。我的一个问题是为什么我的图表在移动滑块时没有显示任何内容?这是我的2类文件:

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

         }
      }
   }

1 个答案:

答案 0 :(得分:1)

我猜你对java很新,所以这里有一些初学者帮助^^

创建内容面板并为面板设置一些布局; 将滑块和绘图面板添加到内容面板中;

你做得对,向滑块添加了一个更改侦听器,但是他们应该重绘绘图面板。

我将添加此片段,以便您更轻松^ _ ^

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

不要忘记 - 这些只是片段,你必须自己做一些工作......