import java.applet.Applet; import java.awt.*; /** * Sine curve applet/application * Draws one cycle of a sine curve. */ public class BeamTest 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 () { Dimension d = size (); width = d.width; height = d.height; } /** * paint() does the drawing of the axes and sine curve * @param g - destination graphics object */ public void paint (Graphics g) { int x; double offset; int xs1 = 20; int xs2 = 90; double w[] = new double [100]; double v[] = new double [100]; double m[] = new double [100]; double ws1 = 0; double ws2 = 0; w[10] = -50; w[50] = -10; w[51] = -10; w[52] = -20; w[80] = -100; w[95] = -20; for ( x = 0; x < 100; x+=1) { ws1 = ws1 + w[x] * ( x - xs2 ); ws2 = ws2 + w[x] * ( x - xs1 ); } ws1 = ws1 / ( xs2 - xs1 ); ws2 = ws2 / ( xs1 - xs2 ); w[xs1] = w[xs1] + ws1; w[xs2] = w[xs2] + ws2; offset = (double) height / 2; g.drawLine (0, (int) offset, 100, (int) offset); for (x=1; x<100; x+=1) { v[x] = v[x-1] + w[x]; m[x] = m[x-1] + v[x]; g.setColor (Color.blue); g.drawLine ( x, (int) offset, x, (int) ( -m[x] * 0.1 + offset ) ); g.setColor (Color.red); g.drawLine ( x, (int) offset, x, (int) ( -v[x] + offset ) ); g.setColor (Color.black); g.drawLine ( x, (int) offset, x, (int) ( w[x] + offset ) ); } } /** * main() is the application entry point * main() is unused when run as an applet * create a window frame and add the applet inside * @param args[] - command line arguments */ public static void main (String args[]) { Frame f = new Frame ("Beam Test"); BeamTest beam = new BeamTest (); f.resize (200, 200); f.add ("Center", beam); f.show (); beam.init (); } }