home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 1.2 KB | 53 lines |
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * Sine curve applet/application
- * Draws one cycle of a sine curve.
- */
- public class Sine extends Applet {
-
- /*
- * width and height of the applet panel
- */
- int width, height;
-
- /*
- * init() is called when the applet is loaded
- * just get the width and height and save it
- */
- public void init () {
-
- setLayout(null);
- width = 300;
- height = 300;
- setSize(width, height);
- }
-
- /**
- * paint() does the drawing of the axes and sine curve
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- int x, x0;
- double y0, y, A, f, t, offset;
-
- A = (double) height / 4;
- f = 2;
- offset = (double) height / 2;
- x0 = 0;
- y0 = offset;
-
- g.drawLine (x0, (int) y0, width, (int) y0);
- g.drawLine (width/2, 0, width/2, height);
- for (x=0; x<width; x+=1) {
- t = (double) x / ((double) width);
- y = offset - A * Math.sin (2 * Math.PI * f * t);
- g.drawLine (x0, (int) y0, x, (int) y);
- x0 = x;
- y0 = y;
- }
- }
- }
-