Page 3 of 3 FirstFirst 123
Results 41 to 48 of 48
Like Tree14Likes

Thread: sine wave in java2D graphics.

  1. #41
    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
    You're trying to draw scaled coordinates so you can safely cast them to type int without any loss of precision.

    kind regards,

    Jos
    oh
    tx and brb..

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

    Default Re: sine wave in java2D graphics.

    Quote Originally Posted by noobplus View Post
    oh
    tx and brb..
    Are you typing that from a cell phone? If you'd used '&' instead of 'and' it'd be even more unreadable.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #43
    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
    Are you typing that from a cell phone? If you'd used '&' instead of 'and' it'd be even more unreadable.

    kind regards,

    Jos
    oh yeah.. im using windows xp v2, and the "&" key is not working, ill use onscreen keyboard . i had web connection problems.. it's working atm
    regards
    dhilip

  4. #44
    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
    Are you typing that from a cell phone? If you'd used '&' instead of 'and' it'd be even more unreadable.

    kind regards,

    Jos
    I've done the codings.. but it gives run-time errors..
    java.lang.NullPointerException
    at frameof.<init>(frameof.java:56)
    at frameof$1.run(frameof.java:34)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
    ad.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
    java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
    ad.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)

    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    fourier.java //to find Fourier transform <irrelevant. it's running fine. but the class values "as", "bs" & "i" are used in the next class>
    Java Code:
    import java.io.*;
    class fourier
    {
    static double a[] = new double[100];
    static double b[] = new double[100];
    static int i;
    static int a1[] = new int[100];static int b1[]= new int[100];
    static int as[] = new int[100];
    static int bs[] = new int[100];
      fourier(String filename, int ntrm)
      {
        boolean debug = true;
        double Pi = 3.1415926535897932384626433832795028841971;
        double x[] = new double[100];
        double y[] = 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(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(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);
          
        }
      	        a1[i]=(int) ((a[i]-0)/(2*3.14-0));//Max is 2pi din is zero
    		b1[i]=(int) ((b[i]-0)/(1-0));
    		as[i]=a1[i]*(100-0)+0 ;   //sxmin,max not set yet,... will set it based on screen values..
    		bs[i]=b1[i]*(100-0)+0;
    		
    	}//end of main
    
    
    }//end of fourier
    frameof.java //the error occurs while running this class
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.border.EmptyBorder;
    
    
    public class frameof extends JFrame 
    {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	private JPanel contentPane;
    	private static Graphics g;
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			
    
    			public void run() {
    				try {
    					
    					
    					frameof frame = new frameof(g);
    					frame.setVisible(true);
    					JLabel text=new JLabel("label",SwingConstants.CENTER);
    					
    					text.setPreferredSize(new Dimension(300,100));
    					frame.getContentPane().add(text,BorderLayout.CENTER);
    					frame.setLocationRelativeTo(null);
    					frame.pack();
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public frameof(Graphics g) {
    		Graphics gg = (Graphics) g;
    		paintComponents(gg);
    		for(fourier.i=1;fourier.i< 10;fourier.i++){
    		gg.drawLine(fourier.as[fourier.i-1], fourier.bs[fourier.i-1], fourier.as[fourier.i], fourier.bs[fourier.i]); }  
                       //I'm using drawLine as you told
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    	}
    
    }
    regards
    dhilip
    Last edited by noobplus; 03-06-2012 at 07:14 PM.

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

    Default Re: sine wave in java2D graphics.

    The drawing part of your code doesn't make sense: Swing supplies a Graphics object to your paintComponent(Graphics g) method; your version tries to fiddle-diddle with a non-existing Graphics object; it doesn't work (it is null actually). Read the Swing tutorial for an explanation how to do it. Summary: extend a JPanel and override the paintComponent(Graphics g) method; paint your sine wave in that method and add your JPanel extension to a JFrame.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #46
    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
    The drawing part of your code doesn't make sense: Swing supplies a Graphics object to your paintComponent(Graphics g) method; your version tries to fiddle-diddle with a non-existing Graphics object; it doesn't work (it is null actually). Read the Swing tutorial for an explanation how to do it. Summary: extend a JPanel and override the paintComponent(Graphics g) method; paint your sine wave in that method and add your JPanel extension to a JFrame.

    kind regards,

    Jos
    ok boss

  7. #47
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: sine wave in java2D graphics.

    :)

    What does brb mean ?

  8. #48
    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 fatabass View Post
    :)

    What does brb mean ?
    i was trying jos' ideas into the codings, and i thouht i would "Be Right Back" with some other doubts

Page 3 of 3 FirstFirst 123

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
  •