//package applet;
/*
* Anipolygon2.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Anipolygon2 extends JApplet implements ActionListener{
private JPanel animation;
private Timer timer;
private int appWidth;
private int appHeight;
private AnimatedPoints ani1, ani2, ani3, ani4, ani5;
/** Initializes the applet Anipolygon2 */
public void init() {
ani1 = new AnimatedPoints(10, 10, 2, 3, 1, 1, 5);
ani2 = new AnimatedPoints(300, 100, 3, 2, -1, 1, 10);
ani3 = new AnimatedPoints(30, 100, 3, 2, -1, 1, 10);
ani4 = new AnimatedPoints(300, 10, 2, 2, 1, 1, 15);
ani5 = new AnimatedPoints(500, 300, 4, 3, 1, 1, 7);
animation = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(new GradientPaint(30,50,Color.blue,100,100,Color.red,true));
int[] xx = new int[]{ani1.getx(),ani2.getx(),ani3.getx(),ani4.getx(),ani5.getx()};
int[] yy = new int[]{ani1.gety(),ani2.gety(),ani3.gety(),ani4.gety(),ani5.gety()};
g2d.fillPolygon(xx,yy,5);
}
};
getContentPane().add(animation);
appWidth = 350;
appHeight = 198;
timer = new Timer(2, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {animation.repaint();}
class AnimatedPoints extends JLabel implements ActionListener{
private int deltaX = 2;
private int deltaY = 3;
private int directionX = 1;
private int directionY = 1;
private int nextX;
private int nextY;
public AnimatedPoints( int startX, int startY, int deltaX, int deltaY,
int directionX, int directionY, int delay){
this.deltaX = deltaX;
this.deltaY = deltaY;
this.directionX = directionX;
this.directionY = directionY;
setLocation(startX, startY);
new Timer(delay, this).start();
}
public int getx(){return nextX;}
public int gety(){return nextY;}
public void actionPerformed(ActionEvent e) {
Container parent = getParent();
// Determine next X position
nextX = getLocation().x + (deltaX * directionX);
if (nextX < 0) {
nextX = 0;
directionX *= -1;
}
if ( nextX + getSize().width > appWidth) {
nextX = appWidth - getSize().width;
directionX *= -1;
}
// Determine next Y position
nextY = getLocation().y + (deltaY * directionY);
if (nextY < 0) {
nextY = 0;
directionY *= -1;
}
if ( nextY + getSize().height > appHeight) {
nextY = appHeight - getSize().height;
directionY *= -1;
}
setLocation(nextX, nextY); //move the Point
}
}
}