import java.awt.*;
import java.applet.Applet;
import java.awt.image.*;
import java.util.StringTokenizer;
/*
* This applet is made by an AMATEUR, so this code is hard to understand!
Usage:
@ author: Sergej Panic
*/
public class rainbow extends Applet implements Runnable {
int zakasnitev;
boolean stojim = false;
Dimension memDimension;
Image[] memImage;
static Image slika1;
Graphics[] memGraphics;
Thread nit;
String rulercolor;
Dimension d;
int AppletWidth, AppletHeight,x=0,squarewidth,s;
int step,speed;
Color RectColor;
String squares;
int in,move1=0;
int[] b;
int[] c;
int levo=0;
int imagew=1000;
int imageh=1000;
int iw,ih;
public void init() {
d = size();
AppletHeight = d.height;
AppletWidth = d.width;
s = Integer.valueOf(getParameter("squares")).intValue();
step = Integer.valueOf(getParameter("step")).intValue();
speed = Integer.valueOf(getParameter("speed")).intValue();
levo = Integer.valueOf(getParameter("horizontal")).intValue();
rulercolor = getParameter("color");
squarewidth = AppletWidth/s;
if(squarewidth*s != AppletWidth)
squarewidth = squarewidth + 1;
levo=levo*squarewidth;
b = new int[255/step*2+10];
memImage= new Image[2];
memGraphics= new Graphics[2];
System.out.println("Number of Squares = "+s);
System.out.println("AppletHeight = "+AppletHeight);
System.out.println("AppletWidth = "+AppletWidth);
System.out.println("squarewidth = "+squarewidth);
imagew=255/step*2*squarewidth;
imageh=d.height;
//izdelamo ustrezen pomnilniški izravnalnik
// če tak ni na voljo
if ( (memGraphics[0] == null)
|| (d.width != memDimension.width)
|| (d.height != memDimension.height) ) {
memDimension = d;
memImage[0] = createImage(imagew, imageh);
memGraphics[0] = memImage[0].getGraphics();
memGraphics[0].setColor(Color.black);
memGraphics[0].fillRect(0, 0, d.width, d.height);
bc();
}//if
iw=d.width/(imagew);
ih=d.height/imageh;
if ( (memGraphics[1] == null)
|| (d.width != memDimension.width)
|| (d.height != memDimension.height) ) {
memDimension = d;
memImage[1] = createImage((iw+2)*imagew, imageh);
memGraphics[1] = memImage[1].getGraphics();
memGraphics[1].setColor(Color.black);
memGraphics[1].fillRect(0, 0, d.width, d.height);
}//if
}//init()
public void bc()
{
int k,l;
b[0]=0;
for(k=1;k<=255/step*2;k++)
{
if(b[k-1]+step <= 255)
b[k]=b[k-1]+step;
else
{
for(int g=k;g<=255/step*2;g++)
if(b[g-1]-step >= 0)
b[g]=b[g-1]-step;
else
{
b[g-1]=255;
System.out.println("ERROR "+255/step*2);
break;
}
break;
}
}//k
}//bc
public void start() {
if (stojim) {
//zaustavljeni smo
//ne delamo nič
} else {
//sprožimo nit
if (nit == null) {
nit = new Thread(this);
}
nit.start();
}
}
public void stop() {
//zaustavimo nit
nit = null;
//sprostimo pomnilniške slike
memGraphics[0] = null;
memImage[0] = null;
memGraphics[1] = null;
memImage[1] = null;
}
public boolean mouseDown(Event e, int x, int y) {
if (stojim) {
stojim = false;
start();
} else {
stojim = true;
//ne kličemo stop(), da ne
//uničimo izravnalnika
nit = null;
}
return true;
}
public void run() {
//znižamo prioriteto
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
//pomnimo pričetek za izravnavo časa
long pricetek = System.currentTimeMillis();
while (Thread.currentThread() == nit) {
if(move1+levo=0)
move1+=levo;
else
move1=move1+levo+imagew;
else
move1=move1+levo-imagew;
repaint();
//izravnamo zakasnitev
try {
pricetek += speed;
Thread.sleep(Math.max(0,
pricetek-System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
}
}
public void paint(Graphics g) {
for(x=0;x<255/step*2;x++)
{
if(rulercolor.equalsIgnoreCase("green"))
RectColor = new Color(0,b[x],0);
if(rulercolor.equalsIgnoreCase("red"))
RectColor = new Color(b[x],0,0);
if(rulercolor.equalsIgnoreCase("blue"))
RectColor = new Color(0,0,b[x]);
memGraphics[0].setColor(RectColor);
memGraphics[0].fillRect(x*squarewidth, 0, squarewidth,imageh);
}//for
//for(int move=0;move