# Thread: sine wave in java2D graphics.

1. ## Re: sine wave in java2D graphics.

Look at these two graphs

http://www.bbc.co.uk/schools/gcsebit...s/graph_24.gif

y=-x3 (cubic)
http://www.bbc.co.uk/schools/gcsebit...s/graph_17.gif

which one looks more like a sine wave?

2. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
would u mind explaining the curves plz.. it is necessary for my project works . im doing my final year project and last date is next monday

Java Code:
double[] x;
double[] y;
... and fill them such that sin(x[ i ]) == y[ i ]; apply the scaling I defined before and you have all your points needed to draw a sine wave.

kind regards,

Jos

3. ## Re: sine wave in java2D graphics.

Originally Posted by JosAH

Java Code:
double[] x;
double[] y;
... and fill them such that sin(x[ i ]) == y[ i ]; apply the scaling I defined before and you have all your points needed to draw a sine wave.

kind regards,

Jos
ty boss, but what is the syntax to plot or draw the scaled values

4. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
ty boss, but what is the syntax to plot or draw the scaled values
between, do u know signal processing.. can we apply fast fourier transform in java.. ? this is also a part of my project

5. ## Re: sine wave in java2D graphics.

Originally Posted by ozzyman
Look at these two graphs

http://www.bbc.co.uk/schools/gcsebit...s/graph_24.gif

y=-x3 (cubic)
http://www.bbc.co.uk/schools/gcsebit...s/graph_17.gif

which one looks more like a sine wave?
ty , and do u know signal processing.. can we apply fast fourier transform in java.. ? this is also a part of my project

6. ## Re: sine wave in java2D graphics.

Yes there are FFT libraries out there that are best found via Google.

7. ## Re: sine wave in java2D graphics.

Originally Posted by Fubarable
Yes there are FFT libraries out there that are best found via Google.
tyvm.. could u suggest me, what to search on google

8. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
tyvm.. could u suggest me, what to search on google
No. I'm sure you're as clever as I am and will play with terms til you find one that works well for you.

9. ## Re: sine wave in java2D graphics.

Originally Posted by Fubarable
No. I'm sure you're as clever as I am and will play with terms til you find one that works well for you.
library, u mean classes or packages ? or something else

10. ## Re: sine wave in java2D graphics.

Originally Posted by Fubarable
No. I'm sure you're as clever as I am and will play with terms til you find one that works well for you.
Nope, sorry 'squire; nothing relevant showed up; not even for "ole busstop", "dolphin", "mammalian protuberances", "biscuit barrel" or "tomato"; that Google thing is worthless ...

kindest regards,

Jos (<--- slightly silly on the right side (1247 points) ;-)

11. ## Re: sine wave in java2D graphics.

Originally Posted by JosAH
Nope, sorry 'squire; nothing relevant showed up; not even for "ole busstop", "dolphin", "mammalian protuberances", "biscuit barrel" or "tomato"; that Google thing is worthless ...
Of course nothing's showing up you ninny. "busstop" is hyphenated! Gosh don't they teach you nuthin' in the Netherlands??

12. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
library, u mean classes or packages ? or something else
But seriously, I have not used FFT with Java (just with Turbo Pascal years ago), but I've seen libraries listed by the hundreds. I'm sure if you do a reasonable search on some good terms you'll come up with several promising leads.

13. ## Re: sine wave in java2D graphics.

As in "bŭßƨƮǒƤ" 'squire? But then it doesn't even look like "muffin" ...

kindest regards,

Jos

14. ## Re: sine wave in java2D graphics.

Originally Posted by JosAH
Nope, sorry 'squire; nothing relevant showed up; not even for "ole busstop", "dolphin", "mammalian protuberances", "biscuit barrel" or "tomato"; that Google thing is worthless ...

kindest regards,

Jos (<--- slightly silly on the right side (1247 points) ;-)
i've done scaling , and dont know ow to plot em next.. heres my coding

Java Code:
import java.io.*;

class fourier extends operations
{
fourier(String filename, int ntrm)
{
boolean debug = true;
double Pi = 3.1415926535897932384626433832795028841971;
double x[] = new double[100];
double y[] = new double[100];
double a[] = new double[100];
double b[] = new double[100];
double xmin, xmax, ymin, ymax, xs, xs1, xs2;
double err, avgerr=0.0, rmserr=0.0, maxerr=0.0;
double sum, sumsq=0.0;
int npts, index, last;
double tpi = 2.0*Pi;
double dx, ti;
String input_line, intStr;

try
{
}
catch(Exception e)
{
System.out.println("read unable to open file "+filename);
return;
}
npts = 0;
try
{
// System.out.println("line="+input_line);
while(input_line!=null)
{
index = 2; // x allow for two blanks
last = input_line.indexOf(' ',index);
// System.out.println("xindex="+0+", xlast="+last);
intStr = input_line.substring(0,last);
// System.out.println("xstring="+intStr);
x[npts] = Double.parseDouble(intStr);
index = last+1; // y
last = input_line.length();
// System.out.println("yindex="+index+", ylast="+last);
intStr = input_line.substring(index,last);
// System.out.println("ystring="+intStr);
y[npts] = Double.parseDouble(intStr);
if(debug)
System.out.println("npts="+npts+", x="+x[npts]+", y="+y[npts]);
npts++;
// System.out.println("line="+input_line);
}
file_in.close();
}
catch(Exception e)
{
return;
}

if(ntrm==-1) ntrm = npts;
ntrm = Math.min(ntrm,(npts-2)/2);
System.out.println(npts+" points read, using "+ntrm+" terms");

xmin = x[0];
xmax = x[0];
ymin = y[0];
ymax = y[0];
for(int j=1; j<npts; j++)
{
xmin = Math.min(x[j],xmin);
xmax = Math.max(x[j],xmax);
ymin = Math.min(y[j],ymin);
ymax = Math.max(y[j],ymax);
}
System.out.println("xmin="+xmin+", xmax="+xmax+
", ymin="+ymin+", ymax="+ymax);

// integration, simple trapezoidal rule
System.out.println("coefficients");
for(int i=0; i<ntrm; i++)
{
a[i] = 0.0;
b[i] = 0.0;
ti = (double)i;
for(int j=0; j<npts-1; j++)
{
xs1 = (x[j]-xmin)/(xmax-xmin);
xs2 = (x[j+1]-xmin)/(xmax-xmin);
dx = xs2-xs1;
a[i] = a[i] + dx*(y[j]*Math.cos(ti*tpi*xs1)+y[j+1]*
Math.cos(ti*tpi*xs2));
b[i] = b[i] + dx*(y[j]*Math.sin(ti*tpi*xs1)+y[j+1]*
Math.sin(ti*tpi*xs2));
}
System.out.println("a["+i+"]="+a[i]+", b["+i+"]="+b[i]);
}
System.out.println(" ");
// check fit

for(int j=0; j<npts; j++)
{
sum = 0.0;
sumsq = 0.0;
xs = (x[j]-xmin)/(xmax-xmin);
for(int i=0; i<ntrm; i++)
{
ti = (double)i;
sum = sum + a[i]*Math.cos(ti*tpi*xs) + b[i]*Math.sin(ti*tpi*xs);
}
err = y[j]-sum;
sumsq = sumsq + err*err;
avgerr = avgerr + Math.abs(err);
maxerr = Math.max(Math.abs(err),maxerr);
if(debug) System.out.println("y["+j+"]="+y[j]+", approx="+sum+
", err"+err);
}
rmserr = Math.sqrt(sumsq/(double)npts);
avgerr = avgerr/(double)npts;
System.out.println("avgerr="+avgerr+", rmserr="+rmserr+", maxerr="+maxerr);
System.out.println("fourier.java finished ");
} // end constructor fourier

public static void main (String[] args)
{
String filename;
int ntrm = -1;
int argc = args.length;

System.out.println("fourier.java running  ");
if(argc>1) // file and ntrm
{
filename = new String(args[0]);
ntrm = Integer.parseInt(args[1]);
System.out.println("fourier series of "+filename+" for "+ntrm+" terms");
new fourier(filename, ntrm);
}
else if(argc>0) // just file, all terms
{
filename = new String(args[0]);
System.out.println("fourier series of "+filename+" for all terms");
new fourier(filename, ntrm);
}
else
{
ntrm = 6;
filename = new String("curve.dat");
System.out.println("fourier series of "+filename+" for "+ntrm);
new fourier(filename, ntrm);
}
} // end main
} // end class fourier

class operations extends fourier
{
public static int a1[i],b1[i],as[i],bs[i];
void scaling(a[],b[]){
a1[i]=(a[i]-0)/(2*math.PI-0);//dmax is 2pi dmin is zero
b1[i]=(b[i]-0)/(1-0);//cmax is 1 cmin is zero
as[i]=a1[i]*(SXmax-SXmin)+SXmin ;   //sxmin,max not set yet,... will set it based on screen values..
bs[i]=b1[i]*(SYmax-SYmin)+SYmin; }
}
Last edited by Fubarable; 03-04-2012 at 07:24 PM. Reason: code tags added

15. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
i've done scaling , and dont know ow to plot em next..
If you have arrays 'sx' and 'sy' containing the scaled points and if you have a Graphics object 'g' (so you have something to draw on), all you have to do is 'connect the dots':

Java Code:
for (int i= 1; i < sx.length; i++)
g.drawLine(sx[i-1], sy[i-1], sx[i], sy[i]);
Read the API documentation for the Graphics class for details.

kind regards,

Jos

16. ## Re: sine wave in java2D graphics.

Originally Posted by JosAH
If you have arrays 'sx' and 'sy' containing the scaled points and if you have a Graphics object 'g' (so you have something to draw on), all you have to do is 'connect the dots':

Java Code:
for (int i= 1; i < sx.length; i++)
g.drawLine(sx[i-1], sy[i-1], sx[i], sy[i]);
Read the API documentation for the Graphics class for details.

kind regards,

Jos
"sx" and "sy" do u mean the "a[]" and "b[]" present in my codings? or a new scaling values are required? ..
sorry for this silly doubt, but it explains me more :P
regards

17. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
"sx" and "sy" do u mean the "a[]" and "b[]" present in my codings? or a new scaling values are required? ..
sorry for this silly doubt, but it explains me more :P
regards
Looking at your code I'd say that 'as' and 'bs' contain the scaled points.

kind regards,

Jos

18. ## Re: sine wave in java2D graphics.

Originally Posted by JosAH
Looking at your code I'd say that 'as' and 'bs' contain the scaled points.

kind regards,

Jos
oh yeah tyvm boss, and thx again for the "g.drawLine" :]

19. ## Re: sine wave in java2D graphics.

Originally Posted by JosAH
Looking at your code I'd say that 'as' and 'bs' contain the scaled points.

kind regards,

Jos
hi again..
i typed "g.drawLine " on eclipse.. it says the command doesn't support double datatypes, and supports only int
i need a syntax to draw lines across double datatypes ...example, (0.0,0.5)(0.1,0.6)
regards
dhilip

20. ## Re: sine wave in java2D graphics.

Originally Posted by noobplus
hi again..
i typed "g.drawLine " on eclipse.. it says the command doesn't support double datatypes, and supports only int
i need a syntax to draw lines across double datatypes ...example, (0.0,0.5)(0.1,0.6)
regards
dhilip
You're trying to draw scaled coordinates so you can safely cast them to type int without any loss of precision.

kind regards,

Jos

Page 2 of 3 First 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•