import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Animation
extends Applet
implements Runnable
{
int approach = 0;
static final String message = "Animation";
Color c1 = new Color(0x0000ff);
Color c2 = new Color(0xffffff);
Font font = new Font("Helvetica", Font.BOLD, 24);
FontMetrics fm = getFontMetrics(font);
int msgAscent = fm.getAscent(),
msgWidth = fm.stringWidth(message),
msgHeight = fm.getHeight();
int dx = 4, dy = 2;
Thread animator;
Image offscreen;
Dimension dim;
int x = 0, y = 0;
public void init()
{
dim = getSize();
offscreen = createImage(dim.width, dim.height);
addMouseListener(new MyMouseListener());
}
public void start()
{
(animator = new Thread(this)).start();
}
public void stop()
{
animator.stop();
animator = null;
}
void drawBackground(Graphics gr, Color c1, Color c2, int n)
{
int dr = (c2.getRed()-c1.getRed())/n;
int dg = (c2.getGreen()-c1.getGreen())/n;
int db = (c2.getBlue()-c1.getBlue())/n;
int h = dim.height, w = dim.width;
int dw = w/n;
int dh = h/n;
gr.setColor(c1);
gr.fillRect(0, 0, w, h);
for (int r = c1.getRed(), g=c1.getGreen(), b=c1.getBlue();
h > 0;
h -= dh, w -= dw, r += dr, g += dg, b += db)
{
gr.setColor(new Color(r,g,b));
gr.fillArc(-w, -h, 2*w, 2*h, 0, -90);
}
}
public void paint(Graphics g)
{
drawBackground(g, c1, c2, 25);
g.setColor(Color.black);
g.setFont(font);
g.drawString(message, x, y);
}
public void run()
{
while (true)
{
repaint();
try {
animator.sleep(20);
} catch (Exception e) {}
}
}
class MyMouseListener
extends MouseAdapter
{
public void MousePressed(MouseEvent e)
{
approach = ( approach + 1 ) % 4;
x = e.getX(); y = e.getY();
}
}
public void plainUpdate(Graphics g)
{
x = (x + dx) % dim.width;
y = (y + dy) % dim.height;
paint(g);
}
public void clippingUpdate(Graphics g)
{
Rectangle oldRect =
new Rectangle(x, y-msgAscent, msgWidth, msgHeight);
x = (x + dx) % dim.width;
y = (y + dy) % dim.height;
Rectangle newRect =
new Rectangle(x, y-msgAscent, msgWidth, msgHeight);
Rectangle union = newRect.union(oldRect);
g.clipRect(union.x, union.y, union.width, union.height);
paint(g);
}
public void bufferedUpdate(Graphics g)
{
x = (x + dx) % dim.width;
y = (y + dy) % dim.height;
paint(offscreen.getGraphics());
g.drawImage(offscreen, 0, 0, this);
}
public void bufferedClippingUpdate(Graphics g)
{
Rectangle oldRect =
new Rectangle(x, y-msgAscent, msgWidth, msgHeight);
x = (x + dx) % dim.width;
y = (y + dy) % dim.height;
Rectangle newRect =
new Rectangle(x, y-msgAscent, msgWidth, msgHeight);
Rectangle union = newRect.union(oldRect);
Graphics og = offscreen.getGraphics();
og.clipRect(union.x, union.y, union.width, union.height);
paint(og);
g.clipRect(union.x, union.y, union.width, union.height);
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g)
{
switch (approach)
{
case 0:
plainUpdate(g);
break;
case 1:
clippingUpdate(g);
break;
case 2:
bufferedUpdate(g);
break;
case 3:
bufferedClippingUpdate(g);
break;
}
}
}