Return to article
Overriding the update() Method: Example5Applet
Here is the code that draws the sine waves.
The applet itself is shown above.
Note that the animation
is now a lot smoother.
The code for the entire applet can be found in
Example5Applet.java.
/**
* Paint a frame of animation.
*/
public void update(Graphics g) {
Color bg = getBackground();
Dimension d = size();
int h = d.height / 2;
for (int x = 0 ; x < d.width ; x++) {
int y1 = (int)((1.0 + Math.sin((x - frame) * 0.05)) * h);
int y2 = (int)((1.0 + Math.sin((x + frame) * 0.07)) * h);
if (y1 > y2) {
int t = y1;
y1 = y2;
y2 = t;
}
g.setColor(bg);
g.drawLine(x, 0, x, y1);
g.drawLine(x, y2, x, d.height);
g.setColor(Color.black);
g.drawLine(x, y1, x, y2);
}
}