This site requires JavaScript, please enable it in your browser!
Greenfoot back
ronald
ronald wrote ...

2020/5/1

plasmaeffect

ronald ronald

2020/5/1

#
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);
    }
}
can you help me with this code?
danpost danpost

2020/5/1

#
You will probably have to work directly with gfim (dispensing with the BufferedImage object altogether). That would mean you will need to convert the value of hue into the individual RGB color part values yourself.
ronald ronald

2020/5/4

#
Good morning I just took over the plasma effect project I'll give you my code, it will be simpler I actually got rid of the bufferedimage the code works but I have a little trouble with the if for the moment the colors are displayed in full window but do not give color spots but this is due to the plasma, I think? Thank you for your help
import greenfoot.*;

/**
 * Plasma_Effect
 * 
 * @author (Ronald) 
 * @version (Plasma_Effect)
 */

public class PlasmaEffectWorld extends World {

    float[][] plasma;
    float hueShift = 255;
    private int timer = 42;
    
    GreenfootImage background = getBackground();
    
    public PlasmaEffectWorld() {    
        
        super(600, 600, 1);
        background.setColor(Color.WHITE);
        
        background = new GreenfootImage(getWidth(), getHeight());    
        plasma = createPlasma(getHeight(), getWidth());
    }
    
    public void act() {
        
        timer--;
        if (timer == 0) {
            hueShift = (hueShift + 0.02f) % 1;
            repaint();
        }
        started();
        drawPlasma();
    }
    
    public 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;
    }
    
    public void drawPlasma() {
        
        GreenfootImage gfim = new GreenfootImage(getWidth(), getHeight());
        
        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.HSBtoRGB(hue, 1, 1));
                //Color color = new Color((int) hueShift + (int) plasma[y][x] % 1,1,1);
                //background.setColorAt(x, y, color);
                
                float hue = hueShift + plasma[y][x] % 1;
                Color red = new Color((int) hueShift + (int) plasma[y][x] % 1,1,1);
                Color green = new Color(1, (int) hueShift + (int) plasma[y][x] % 1,1);
                Color blue = new Color(1, 1, (int) hueShift + (int) plasma[y][x] % 1);
                
                if (hueShift <= 255)        background.setColorAt(x, y, red);   //Color.CYAN
                else if (hueShift <= 170)   background.setColorAt(x, y, green); //Color.LIGHT_GRAY);
                else                        background.setColorAt(x, y, blue);  //Color.GRAY);
            }
        gfim.drawImage(background, 0, 0);
        setBackground(gfim);
    }
}
You need to login to post a reply.