?
Solved

Drawing sinusoidal wave

Posted on 1998-12-18
1
Medium Priority
?
1,039 Views
Last Modified: 2008-03-10
Hi all again!! do you have a function which draws a sin wave using the sin method of Math class? Or at least, can you guide me in programming that function by myself?

Thanks a lot in advance
jstuardo@aclaris.cl
0
Comment
Question by:skel
1 Comment
 
LVL 1

Accepted Solution

by:
malexiev earned 200 total points
ID: 1229731
Try this:

  import java.awt.*;  
  import java.applet.*;
 
  public class Test
  extends Applet {  
   
    private final double scale = 50;
    private double step = 1 / scale;
   
    public Test() {
    }    
   
    public void drawSin(Graphics g, double x0, double x1) {
     
      g.setColor(Color.red);      
      int i = 0;      
      int y0 = getBounds().height >> 1;
      for (double f = x0; f < x1; f += step,i++) {
        g.drawLine(i, (int)((Math.sin(f)) * scale + y0),
                   i, (int)((Math.sin(f)) * scale + y0));
       
      }
    }
   
    public void paint(Graphics g) {
      drawSin(g, -Math.PI, Math.PI);
    }
  }

Hope this helps.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question