I am trying to create a GUI using Java Swing for the mathematical equation 5((θ/β) - cos(2πθ/β))
.
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);
}
}
}
}
This program is working fine and I can get the cos graph on swing GUI.
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
.
I have changed the above code to calculate the y
co-ordinate to support this equation like this:
y = yBase - getValue(i) * yScale;
here getValue
method is:
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?