简体   繁体   English

Java中带有滑块的二次方程

[英]Quadratic Equation In Java With Sliders

I have to design and implement an application that draws the graph of the equation of ax^2 + bx + c where the values of ab and c are set using sliders. 我必须设计并实现一个应用程序,该应用程序绘制ax ^ 2 + bx + c方程的图形,其中ab和c的值是使用滑块设置的。 I am editing my original post and thus am going to do my best to post an sscce. 我正在编辑我的原始帖子,因此将尽我所能发布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: 这是我的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);

         }
      }
   }

i guess you're very new to java, so here's some starters help ^^ 我想您是Java的新手,所以这里有一些入门帮助^^

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... 别忘了-这些只是片段,您必须自己做一些工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM