int min = Integer.MIN_VALUE; int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE; int max = Integer.MAX_VALUE;
(int) (hueShift * plasma[y][x]%1)
public void drawPlasma() {
GreenfootImage gfim = new GreenfootImage(getWidth(), getHeight());
for (int y = 0; y < plasma.length; y++)
for (int x = 0; x < plasma[y].length; x++) {
int hue = (int) (hueShift * plasma[y][x] % 1);
Color red = new Color(hue, 0, 0);
Color green = new Color(0, hue, 0);
Color blue = new Color(0, 0, hue);
if (hue > 170 background.setColorAt(x, y, red);
else if (hue > 85) background.setColorAt(x, y, green);
else background.setColorAt(x, y, blue);
}
}
gfim.drawImage(background, 0, 0);
setBackground(gfim);
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* @author rosettacode, Spock47
*/
public class PlasmaWorld extends World
{
private float[][] plasma;
private float hueShift = 0;
public PlasmaWorld() {
super(600, 400, 1);
plasma = createPlasma(getWidth(), getHeight());
}
private 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;
}
@Override
public void act() {
hueShift = (hueShift + 0.01f) % 1;
drawPlasma();
}
private void drawPlasma() {
final var image = getBackground();
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
float hue = (hueShift + plasma[y][x]) % 1;
image.setColorAt(x, y, getColor(hue));
}
}
}
private Color getColor(final float hue) {
final var awtColor = new java.awt.Color(java.awt.Color.HSBtoRGB(hue, 1, 1));
return new Color(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());
}
}