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

2023/9/1

re PLASMA EFFECT

1
2
ronald ronald

2023/9/4

#
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
Hello again I took tests again this Monday actually, it's zero for min and max that's it, I want to tell you that I solved the color problem but not the plasma I redid your code to test actually it's zero otherwise I wonder, in your code, is there an error? int min = Math.Float.MAX_VALUE; int max = Math.Float.MIN_VALUE;
danpost danpost

2023/9/4

#
ronald wrote...
in your code, is there an error? int min = Math.Float.MAX_VALUE; int max = Math.Float.MIN_VALUE;
Probably. Both lines should start with float -- not int.
ronald ronald

2023/9/4

#
so I did some tests again, it gives MIN => 0.017131364 MAX => 0.9827103 maybe it's too little how to interpret this?
danpost danpost

2023/9/4

#
ronald wrote...
MIN => 0.017131364 MAX => 0.9827103 ... how to interpret this?
Well, looks like the range is zero to one {o ... 1). Maybe hueShift is supposed to be multiplied by plasma values -- not added. The product should be in parenthesis for 'int'ing, using the following form:
(int) (hueShift * plasma[y][x]%1)
Also, the if-else-else at the end of the for loop in the drawPlasma method looks off. The first condition should always be true, producing red. The second condition is moot when compared to the first one (it is only true when the first condition is also true). So, the second part is never reached. The third part should never be reached either, because the range of hueShift is totally included in the first part. WAIT: hueShift is 255 and doesn't change at all. Maybe you are supposed to be looking at hue there and not at hueShift. Try this:
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);
}
I am not sure what is going on with the drawing and setting of the background image. Seems like the plasma object will always be at the top-left corner of the world.as coded.
ronald ronald

2023/9/4

#
so it always remains black without plasma effect Honestly, I'm not going to spend too much time on this code. otherwise I thank you again
Spock47 Spock47

2023/9/4

#
When adapting source code, start with only changing the things that need to be changed.* - Well, the computation of the plasma (createPlasma, attribute plasma) can be the same as in rosetta. - Putting the values into the image (drawPlasma) has to be modified slightly to use the Greenfoot image.** - And the real-time changing has to be implemented anew using Greenfoot magic (act instead of Timer), being now a simple one-liner. * You still can change it later when you found a working solution. ** Most of the work here is to transform to Greenfoot.Color. To be honest, I am not sure, why Greenfoot uses this inconvenient wrapper class instead of directly using java.awt.Color. Here is a working solution based on the rosetta code.
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());
    }
    
}
Live long and prosper, Spock47
ronald ronald

2023/9/4

#
thanks Spock47 then it works of course I make modifications to adapt the code to greenfoot I knew it was a color problem as bufferedimage does not work on greenfoot but hey you have to find the right code but I never thought of awtColor I tried with awt image bufferedimage - awtgetImage I don't even know if we can do that I didn't succeed, anyway thank you again Spock47, a new STAR TREK film in preparation or not??? I'm kidding, don't be offended
You need to login to post a reply.
1
2