gpt4 book ai didi

java - 带有 slider 的 Java 中的二次方程

转载 作者:行者123 更新时间:2023-11-30 11:24:50 25 4
gpt4 key购买 nike

我必须设计并实现一个应用程序来绘制 ax^2 + bx + c 的方程式,其中 a b 和 c 的值是使用 slider 设置的。我正在编辑我的原始帖子,因此我将尽我所能发布一个 sscce。我的代码如下。一切都编译并运行。我的一个问题是,为什么移动 slider 时我的图形不显示任何内容?这是我的 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);

}
}
}

最佳答案

我猜你是 java 的新手,所以这里有一些入门帮助 ^^

创建一个内容面板并为面板设置一些布局;将 slider 和绘图面板添加到您的内容面板;

您做对了,向 slider 添加了一个更改监听器,但他们应该重绘绘图面板。

我将添加这些片段以方便您使用^_^

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

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

关于java - 带有 slider 的 Java 中的二次方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com