this will work as an application or an applet. its really cool.
Code:
Code:
import java.awt.*;
import java.util.*;
/**
* Example6Applet
*
*based on Example6Applet from http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation.html
*
*
*originally by Arthur van Hoff
*modified by Thomas Dickerson
*/
public class SineAnimation extends java.applet.Applet implements Runnable {
int frame;
int delay;
Thread animator;
Random r;
Dimension offDimension;
Image offImage;
Graphics offGraphics;
boolean isApp;
int fps;
public SineAnimation(int i){fps = i; isApp = true;}
public SineAnimation(){super(); isApp = false;}
public void init() {
if(!isApp){
String str = getParameter("fps");
fps = (str != null) ? Integer.parseInt(str) : 10;
}
delay = (fps > 0) ? (1000 / fps) : 100;
r = new Random((new Date()).getTime());
}
public void start() {
animator = new Thread(this);
animator.start();
}
public void run() {
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator) {
repaint();
try {
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
frame++;
}
}
public void stop() {
animator = null;
offImage = null;
offGraphics = null;
}
public void update(Graphics g) {
Dimension d = getSize();
if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
offGraphics.setColor(new Color((frame + 90)%256,(frame + 180)%256,(frame)%256));
offGraphics.fillRect(0, 0, d.width, d.height);
paintFrame(offGraphics);
g.drawImage(offImage, 0, 0, null);
}
public void paint(Graphics g) {
if (offImage != null) {
g.drawImage(offImage, 0, 0, null);
}
}
public void paintFrame(Graphics g) {
Dimension d = getSize();
int h = d.height / 2;
for (int x = 0 ; x < d.width ; x++) {
int y1 = (int)((1.0 + Math.sin((x - frame) * 0.04)) * h);
int y2 = (int)((1.0 + Math.sin((x + frame) * 0.04)) * h);
int height = getSize().height;
int width = getSize().width;
int col = (int)(256d * (Math.abs((double)y1-(double)y2)/(double)height));
int col2 = (int)(frame+(256d*(double)x/(double)width))%256;
int col3 = (col2 + frame/7) % 256;
g.setColor(new Color(col2,col,col3));
g.drawLine(x, y1, x, y2);
g.setColor(Color.WHITE);
}
}
public static void main(String args[]){
Frame mwindow = new Frame();
mwindow.setUndecorated(true);
mwindow.setLayout(null);
mwindow.setBounds(50,50,600,60);
SineAnimation mySineAnimation = new SineAnimation(10);
mwindow.add(mySineAnimation);
mySineAnimation.setBounds(0,0,600,60);
mwindow.setVisible(true);
mySineAnimation.init();
mySineAnimation.start();
}
}