import java.awt.*; /** Program: twoGraph35: graphs variable-frequency sine curves Purpose: demo of slider-driven updates in realtime @author: Paul Garrett, garrett@math.umn.edu @version: 1.22 30 December 1996 */ public class twoGraph35 extends java.applet.Applet { int t; // the scrollbar's parameter float f; Scrollbar sb; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); sb = new Scrollbar(Scrollbar.HORIZONTAL,80,10,4,199); add(sb); t = 80; } public void paint(Graphics g) { g.drawString("Comparing graphs of sine functions", 10, 50); g.drawString("Move the slider to change", 20, 100); g.drawString(" the frequency and the", 20, 115); g.drawString(" constant in the formula", 20, 130); g.drawString(" - Paul Garrett", 20, 175); g.drawString("garrett@math.umn.edu", 30, 190); //g.drawLine(260,0,260,200); // y-axis //g.drawLine(250,100,460,100); // x-axis g.fillRect(260,0,2,200); // thick y-axis g.fillRect(250,100,210,2); // thick y-axis g.drawLine(260,0,256,7); // arrowheads on y-axis g.drawLine(260,0,264,7); g.drawString(" y ",243,10); // label y-axis g.drawLine(460,100,453,96); // arrowheads on x-axis g.drawLine(460,100,453,104); g.drawString(" x ",460,103); // label x-axis for (int i=10; i <= 190; i+=9) { // 10-pixel grid g.drawLine(i+250,10,i+250,190); // vertical g.drawLine(260,i,440,i); // horizontal } // done with grid... // upper left x, upper left y, width, height g.fillRect(258, 98, 4,4); // blob at (0,0) g.fillRect(348, 98, 4,4); // blob at (1,0) g.fillRect(258, 8, 4,4); // blob at (0,1) g.fillRect(438, 98, 4,4); // blob at (2,0) g.fillRect(438, 8, 4,4); // blob at (2,1) g.fillRect(348, 98, 4,4); // blob at (1,1) g.fillRect(348, 8, 4,4); // blob at (1,1) g.fillRect(258, 188, 4,4); // blob at (0,-1) g.fillRect(348, 188, 4,4); // blob at (1,-1) g.fillRect(438, 188, 4,4); // blob at (2,-1) f = (float) 80/(float)t; g.drawString("y = sin("+f+" * x)",10,70); for (int i=-10; i < 205; i+=2) { g.drawLine(i+250, (int)(100-90*Math.sin((float)(i-10)/t)), i+2+250, (int)(100-90*Math.sin(((float)(i-8))/t))); } } public boolean handleEvent(Event e) { if (e.target == sb && (e.id == Event.SCROLL_ABSOLUTE || e.id == Event.SCROLL_LINE_UP || e.id == Event.SCROLL_LINE_DOWN)) { int v = ((Scrollbar)e.target).getValue(); t = sb.getValue(); repaint(); } return true; } }