import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class PlasmaEffect here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PlasmaEffect extends Actor
{
float[][] plasma;
float hueShift = 0;
BufferedImage img;
public PlasmaEffect()
{
}
public void act(){
GreenfootImage gfim = new GreenfootImage(900, 600);
img = new BufferedImage(gfim.getWidth(), gfim.getHeight(), TYPE_INT_RGB);
plasma = createPlasma(gfim.getHeight(), gfim.getWidth());
hueShift = (hueShift + 0.02f) % 1;
setImage(gfim);
}
float[][] createPlasma(int w, int h) {
float[][] buffer = new float[h][w];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
double value = Math.sin(x / 16.0);
value += Math.sin(y / 8.0);
value += Math.sin((x + y) / 16.0);
value += Math.sin(Math.sqrt(x * x + y * y) / 8.0);
value += 4; // shift range from -4 .. 4 to 0 .. 8
value /= 8; // bring range down to 0 .. 1
// requires VM option -ea
assert (value >= 0.0 && value <= 1.0) : "Hue value out of bounds";
buffer[y][x] = (float) value;
}
return buffer;
}
void drawPlasma(GreenfootImage gfim) {
int h = plasma.length;
int w = plasma[0].length;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
float hue = hueShift + plasma[y][x] % 1;
img.setRGB(x, y, Color.BLUE);//HSBtoRGB(hue, 1, 1));
}
gfim.drawImage(img, 0, 0, null);
}
}

