import java.applet.*; import java.awt.*; import java.awt.event.*; /** * Sine curve applet/application * Draws one cycle of a sine curve. */ public class ControlTest extends Applet implements ActionListener { /* * width and height of the applet panel */ int width, height; String message = ""; // Display user action Button quit; /* * init() is called when the applet is loaded * just get the width and height and save it */ public void init () { Dimension d = getSize (); width = d.width; height = d.height; Label one = new Label("One"); quit = new Button("Quit"); add(one); add(quit); quit.addActionListener(this); } /** * action() Recieve, recognize, handle events */ public void actionPerformed( ActionEvent ae ) { String actionString = ae.getActionCommand(); if( actionString.equals("Quit") ) { message = "Quit pressed."; stop(); } repaint(); } /** * paint() does the drawing of the axes and force, shear, and bending curves. * @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 ) ); } g.drawString( message, 10, 180 ); } /** * 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 ("Control Test"); ControlTest beam = new ControlTest (); f.setSize (200, 200); f.add ("Center", beam); f.show (); beam.init (); } }