Return to article

Generating Graphics: Example4Applet


You will probably see a lot of flashing in the applet above. We'll fix that in the next example. Below is the drawing code for this applet. The code for the entire applet can be found in Example4Applet.java.

    /**
     * Paint a frame of animation.
     */
    public void paint(Graphics g) {
	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);
	    g.drawLine(x, y1, x, y2);
	}
    }