AnimatedLines.java

package com.bozoid.cis370.hw05;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*; // for Ellipse2D
import java.awt.font.*;
import java.util.Random;


/**
The classic 'animated lines' screensaver animation.  This animation
draws <code>nlines</code> lines, derived from successive values of
two <code>Trajectory</code> objects, one for each end of a line.
*/
public class AnimatedLines implements Animator
{
    public AnimatedLines(int nlines)
    {
        this.nlines = nlines;
    }

    public void oneFrame(Component comp,Graphics g)
    {
        int i;
        Point2D p1,p2;

        int framewidth = comp.getWidth();
        int frameheight = comp.getHeight();
        if (lines == null) {
            lines = new Line2D[nlines];
            t1 = new Trajectory(r,10,framewidth,frameheight);
            t2 = new Trajectory(r,10,framewidth,frameheight);
            for (i=0; i<nlines; i++) {
                p1 = t1.nextPoint(framewidth,frameheight);
                p2 = t2.nextPoint(framewidth,frameheight);
                lines[i] = new Line2D.Double(p1,p2);
                }
            nextLine = 0;
            }

        Graphics2D g2 = (Graphics2D)g;
        comp.setBackground(Color.BLACK);
        g2.setPaint(Color.GREEN);
        for (i=0; i<nlines; i++)
            g2.draw(lines[i]);
        p1 = t1.nextPoint(framewidth,frameheight);
        p2 = t2.nextPoint(framewidth,frameheight);
        lines[nextLine] = new Line2D.Double(p1,p2);
        nextLine = (nextLine + 1) % nlines;
    }

    private int nlines=30;
    private static Random r = new Random();
    private Trajectory t1,t2;
    private Line2D lines[] = null;
    private int nextLine = 0;
}