方程 5((θ/β) - cos(2πθ/β)) 的 Java Swing GUI [英] Java Swing GUI for equation 5((θ/β) - cos(2πθ/β))
问题描述
我正在尝试使用 Java Swing 创建一个用于数学方程 5((θ/β) - cos(2πθ/β))
的 GUI.
I am trying to create a GUI using Java Swing for the mathematical equation 5((θ/β) - cos(2πθ/β))
.
最初我开始使用一个简单的余弦函数并创建了 GUI,它工作正常.这是我的余弦函数程序:
Initially I started using a simple cosine function and created the GUI and it is working correctly. Here is my program for cosine function:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SimpleCosWave extends JFrame {
public SimpleCosWave() {
setLayout(new BorderLayout());
add(new CosGraph(), BorderLayout.CENTER);
}
public static void main(String[] args) {
SimpleCosWave frame = new SimpleCosWave();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setTitle("SineWave");
}
class CosGraph extends JPanel {
public void paintComponent(Graphics g) {
int xBase = 100;
int top = 100;
int yScale = 100;
int xAxis = 360;
int yBase = top + yScale;
int x, y;
g.drawLine(xBase, top, xBase, top + 2 * yScale);
g.drawLine(xBase, yBase, xBase + xAxis, yBase);
g.setColor(Color.red);
for (int i = 0; i < xAxis; i++) {
x = xBase + i;
y = yBase - (int) Math.cos(Math.toRadians(i)) * yScale;
g.drawLine(x,y,x,y);
}
}
}
}
该程序运行良好,我可以在 Swing GUI 上获取 cos 图.
This program is working fine and I can get the cos graph on swing GUI.
现在我试图扩展这个程序以支持方程 - 5((θ/β) - cos(2πθ/β))
其中 θ
的范围从 0 到 360 度
并且 β
的值是 0 <β<360
.
Now I am trying to extend this program to support the equation - 5((θ/β) - cos(2πθ/β))
where θ
ranges from 0 to 360 degrees
and value of β
is such that it is 0 < β < 360
.
我改变了上面的代码来计算 y
坐标来支持这个方程:
I have changed the above code to calculate the y
co-ordinate to support this equation like this:
y = yBase - getValue(i) * yScale;
这里的getValue
方法是:
private int getValue(int theta) {
int beta = 45;
double b = (theta/beta);
double angle = 2*Math.PI*(b);
double c = Math.cos(angle);
double result = 5*(b-c);
return (int)result;
}
当我进行此更改时,我没有得到任何正确的图形或波形,而是得到一条水平线.
When I do this change then I am not getting any proper graph or wave, instead I am getting a horizontal line.
有人可以帮我在这段代码中哪里出错吗?
Can someone please help me where I am doing mistake in this code?
推荐答案
正如 Wim Delauwe 所指出的,当一个 int 除以一个 int 时,你正在做整数除法.例如, 45/90
导致 int 值为零.但是,45.0/90.0
计算结果为双精度值 0.5.如果任一操作数是双精度数,则另一个被强制为双精度数,因此 45/90.0
也将产生 0.5.
As Wim Delauwe points out, when dividing an int by an int, you are doing integer division. For example, 45 / 90
results in an int value of zero . However, 45.0 / 90.0
evaluates to the double value of 0.5. If either operand is a double, the other is coerced to a double, so 45 / 90.0
will also yield 0.5.
同样,由于您的值在相对较小的范围内,因此在计算出与该值对应的确切像素之前,不应将值截断为整数,以保持精度.因此,getValue
应该有一个 double 的返回类型,并且它应该返回 result
而不进行强制转换.
Similarly, because your values are in a relatively small range, you should not truncate a value to an integer until you have calculated the exact pixel corresponding to the value, to preserve the precision. Thus, getValue
should have a return type of double, and it should return result
without casting it.
然后,在您的绘图代码中,您将进行演员表:
Then, in your drawing code, you would do the cast:
y = yBase - (int) (getValue(i) * yScale);
然而,正如 David Wallace 所指出的,getValue(i) 似乎返回的值大约在 -5 到 +43 之间.将这些值乘以 yScale 将创建一个非常垂直拉伸的图形.
However, as David Wallace points out, getValue(i) seems to return values between roughly -5 and +43. Multiplying those values by yScale is going to create a very vertically stretched graph.
你可以硬编码规范化:
y = yBase - (int) (getValue(i) / 43.0 * yScale);
我会选择计算最大的 y 值,并以此对它们进行标准化:
I would opt to calculate the largest y value, and normalize them by that:
double maxY = 0;
for (int i = 0; i < xAxis; i++) {
maxY = Math.max(maxY, Math.abs(getValue(i)));
}
for (int i = 0; i < xAxis; i++) {
x = xBase + i;
y = yBase - (int) (getValue(i) / maxY * yScale);
g.drawLine(x,y,x,y);
}
这篇关于方程 5((θ/β) - cos(2πθ/β)) 的 Java Swing GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!