/* * h3a.java - revised 18 Apr 05 - width 501, height 361 * @author jackord@kw.igs.net */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class h3a extends Applet implements ActionListener { int kk=0; // Declarations double gr, h; String bs="Run"; Button b=new Button(bs); public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); setBackground(new Color(211, 211, 211)); add(b); b.addActionListener(this); } double rect(int n) { double s, x, dx; s=0; dx=h/n; for (int i=1; i<=n; i=i+1) { x=(i-.5)*dx; s=s+1/Math.sqrt(2*gr*(h-x)); } return s*dx; } public void paint(Graphics g) { double x, dx, s1, s2, xa, xb, va, vb, vbar; int x1, y1, x2, y2; g.setFont(new Font("TimesRoman", Font.PLAIN, 16)); g.setColor(Color.black); g.drawRect(88, 12, 400, 300); g.drawString("0", 85, 332); g.drawString("5", 485, 332); g.drawString("x (m)", 268, 344); g.drawString("0", 72, 318); g.drawString("0.4", 66, 18); g.drawString("1/V(x)", 16, 133); g.drawString("(s/m)", 24, 203); g.setFont(new Font("TimesRoman", Font.PLAIN, 14)); if (kk==1) { gr=9.8; h=4.9; xa=h/8; xb=7*h/8; dx=h/200; va=Math.sqrt(2*gr*(h-xa)); vb=Math.sqrt(2*gr*(h-xb)); vbar=(va+vb)/2; g.setColor(Color.blue); x1=88+(int)(80*xa); y1=312-(int)(750/va); for (int i=1; i<=200; i=i+1) { x2=88+(int)(80*i*dx); y2=312-(int)(750/Math.sqrt(2*gr*(h-i*dx))); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; if (y1<=12) { i=200; } } g.setColor(Color.black); x1=88+(int)(80*xb); y1=312-(int)(750/vb); g.drawLine(x1, 312, x1, y1); x1=88+(int)(80*xa); y1=312-(int)(750/vbar); g.drawLine(x1, 312, x1, y1); g.setColor(Color.green); y1=312-(int)(750/va); x2=88+(int)(80*xb); y2=312-(int)(750/vb); g.drawLine(x1, y1, x2, y2); g.setColor(Color.magenta); y1=312-(int)(750/vbar); g.drawLine(x1, y1, x2, y1); g.setColor(Color.red); y1=312-(int)(750/Math.sqrt(2*gr*(h-xa/2-xb/2))); g.drawLine(x1, y1, x2, y1); g.setColor(Color.black); s1=rect(20); s2=rect(40); g.drawString("Rect: T(20) = "+(float)s1+" T(40) = "+(float)s2+ " T = "+(float)(s2+(s2-s1)/3), 100, 32); g.drawString("Analytic: T = Tvbar = "+(float)(Math.sqrt(2*h/gr)), 200, 64); } kk=0; } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; } repaint(); } }