I am new in java world.I am trying to plot sine wave graph and I have written code too.My code is giving output but When my frequency value is 50 it form only 1 or 2 wave and when my frequency is 4 or 10 it form many waves .how to resolve this problem please help me.here is my code.
After the execution i want to save the entered value in excel file please help me to solve this problem.
here`public class SineFrame extends javax.swing.JFrame{
//1d array to store the amplitude data, time data is represented or derived from the index of array
double[] amplitude;
int center;
double H_STEP_SIZE = 1d;
int t=101;
int amp=100;
double phase=0.07;
int hSize=600;
int vSize=600;
int frequency=100;
public SineFrame(){
//this.t = t;
// this.amp = amp;
//this.phase = phase;
//to check integer overflow when multiplied with horizontal step size
amplitude = new double[t*(int)H_STEP_SIZE < t?Integer.MAX_VALUE:t*(int)H_STEP_SIZE];
setDefaultCloseOperation(EXIT_ON_CLOSE);
center = vSize/2;
setSize(hSize,vSize);
//calculate the co-ordinates based on input params to draw the wave
calculateCoords(t,amp,phase,frequency);
}
private void calculateCoords(int t, int amp, double phase,double frequency) {
for(int i=0;i<amplitude.length;i++)
amplitude[i] = amp * Math.sin(2*Math.PI*frequency*t+phase*(i/H_STEP_SIZE));
}
@Override
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Point2D prev,cur;
double y;
Line2D lin;
Font font = new Font("Times", Font.BOLD, 25);
FontRenderContext frc = g2.getFontRenderContext();
// TextLayout layout = new TextLayout("A = "+amp+"; Phase = "+Phase+"; t = "+t, font, frc);
//layout.draw(g2, 10f, 50f);
cur = new Point2D.Double(10,center);
//iterate thru' the calculated co-ordinates and draw lines between adjacent points
//to generate the sine wave
for(int i=0;i<amplitude.length;i++){
prev = cur;
y = getCoOrdsFromAmplitude(amplitude[i]);
cur = new Point2D.Double(10+(i/H_STEP_SIZE), y);
System.out.println("Co-ordinates--{x="+cur.getX()+"},{y="+cur.getY()+"}");
lin = new Line2D.Double(prev,cur);
g2.draw(lin);
}
}
private double getCoOrdsFromAmplitude(double amp){
return center + amp;
}
/* public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SineFrame().setVisible(true);
}
});
}
*/
}