Java Reference
In-Depth Information
a Thread . (Note that Java 1.3 includes java.util.Timer , a class that is similar to,
but quite distinct from, javax.swing.Timer .)
Example 11•17: Hypnosis.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.Timer; // Import explicitly because of java.util.Timer
/**
* A Swing component that smoothly animates a spiral in a hypnotic way.
**/
public class Hypnosis extends JComponent implements ActionListener {
double x, y; // The center of the spiral
double r1, r2; // The inner and outer radii of the spiral
double a1, a2; // The start and end angles of the spiral
double deltaA; // How much the angle changes each frame
double deltaX, deltaY; // The trajectory of the center
float linewidth; // How wide the lines are
Timer timer; // The object that triggers the animation
BufferedImage buffer; // The image we use for double-buffering
Graphics2D osg;
// Graphics2D object for drawing into the buffer
public Hypnosis(double x, double y, double r1, double r2,
double a1, double a2, float linewidth, int delay,
double deltaA, double deltaX, double deltaY)
{
this.x = x; this.y = y;
this.r1 = r1; this.r2 = r2;
this.a1 = a1; this.a2 = a2;
this.linewidth = linewidth;
this.deltaA = deltaA;
this.deltaX = deltaX;
this.deltaY = deltaY;
// Set up a timer to call actionPerformed() every delay milliseconds
timer = new Timer(delay, this);
// Create a buffer for double-buffering
buffer = new BufferedImage((int)(2*r2+linewidth),
(int)(2*r2+linewidth),
BufferedImage.TYPE_INT_RGB);
// Create a Graphics object for the buffer, and set the linewidth
// and request antialiasing when drawing with it
osg = buffer.createGraphics();
osg.setStroke(new BasicStroke(linewidth, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND));
osg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
// Start and stop the animation by starting and stopping the timer
public void start() { timer.start(); }
public void stop() { timer.stop(); }
/**
Search WWH ::




Custom Search