Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
Like Tree14Likes

Thread: sine wave in java2D graphics.

  1. #21
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: sine wave in java2D graphics.

    Look at these two graphs

    y=x2 (quadratic)
    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?
    noobplus likes this.

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    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
    Forget about those Bezier curves; define two arrays instead:

    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
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #23
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by JosAH View Post
    Forget about those Bezier curves; define two arrays instead:

    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. #24
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    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. #25
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by ozzyman View Post
    Look at these two graphs

    y=x2 (quadratic)
    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. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: sine wave in java2D graphics.

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

  7. #27
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

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

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    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. #29
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by Fubarable View Post
    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. #30
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by Fubarable View Post
    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) ;-)
    noobplus and Fubarable like this.
    cenosillicaphobia: the fear for an empty beer glass

  11. #31
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by JosAH View Post
    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. #32
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    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.
    noobplus likes this.

  13. #33
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: sine wave in java2D graphics.

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

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #34
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by JosAH View Post
    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;
        FileReader file_in;
        BufferedReader in;
        String input_line, intStr;
    
        // read data 
        try
        {
          file_in = new FileReader(filename);
          in = new BufferedReader(file_in);
        }
        catch(Exception e)
        {
          System.out.println("read unable to open file "+filename);
          return;
        }
        npts = 0;
        try
        {
          input_line = in.readLine();
          // 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++;
            input_line = in.readLine();
            // System.out.println("line="+input_line);
          }
          file_in.close();
        }
        catch(Exception e)
        {
          System.out.println("read 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. #35
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    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
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  16. #36
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by JosAH View Post
    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. #37
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    "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
    cenosillicaphobia: the fear for an empty beer glass

  18. #38
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by JosAH View Post
    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. #39
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Arrow Re: sine wave in java2D graphics.

    Quote Originally Posted by JosAH View Post
    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. #40
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Do number series represent sine wave?
    By rng in forum New To Java
    Replies: 12
    Last Post: 01-18-2012, 03:46 PM
  2. sine and cosine
    By Dennis in forum Advanced Java
    Replies: 9
    Last Post: 11-13-2010, 05:45 AM
  3. Java2D
    By Alex j in forum Java 2D
    Replies: 1
    Last Post: 04-07-2009, 04:59 PM
  4. Drawing the sine curve
    By bumblyb33 in forum Java 2D
    Replies: 7
    Last Post: 03-26-2009, 10:29 PM
  5. Replies: 0
    Last Post: 06-28-2008, 09:25 PM

Posting Permissions

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