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/1

#
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);
    }
}
ronald ronald

2023/9/1

#
D I just realized that I made a lot of mistakes so I reviewed my code, I show you, the window is still black an explanation ? maybe it comes from plasma as I always said earlier thanks again
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 H = hueShift + plasma[y][x] % 1;
                //img.setRGB(x, y, Color.HSBtoRGB(hue, 1, 1));
        
                float S = 0;
                float V = 0;
                float C = S * V;
                float X = C * (1 - ((H / 60) % 2) - 1);
                float m = V - C;
        
                float s = S / 100;
                float v = V / 100;
                float r = 0;
                float g = 0;
                float b = 0;
                
                int R = (int) (r + m) * 255;
                int G = (int) (g + m) * 255;
                int B = (int) (b + m) * 255;
                if(H >= 0 && H < 60) {
                    r = C;
                    g = X;
                    b = 0;
                    background.setColorAt(x, y, new Color((int) r, (int) g, (int) b));
                } else if(H >= 60 && H < 120) {
                    r = X;
                    g = C;
                    b = 0;
                    background.setColorAt(x, y, new Color((int) r, (int) g, (int) b));
                } else if(H >= 120 && H < 180) {
                    r = 0;
                    g = C;
                    b = X;
                    background.setColorAt(x, y, new Color((int) r, (int) g, (int) b));
                } else if(H >= 180 && H < 240) {
                    r = 0;
                    g = X;
                    b = C;
                    background.setColorAt(x, y, new Color((int) r, (int) g, (int) b));
                } else if(H >= 240 && H < 300) {
                    r = X;
                    g = 0;
                    b = C;
                    background.setColorAt(x, y, new Color((int) r, (int) g, (int) b));
                } else {
                    r = C;
                    g = 0;
                    b = X;
                    background.setColorAt(x, y, new Color((int) r, (int) g, (int) b));
                }
            }
        gfim.drawImage(background, 0, 0);
        setBackground(gfim);
    }
danpost danpost

2023/9/2

#
ronald wrote...
the window is still black an explanation ? << Code Omitted >>
Think about what happens starting at line 12. Trace it in your head ALL values from there to line 26 (inclusive) will be zero. It is no wonder why all you have is black.
ronald ronald

2023/9/2

#
Yes, it's true. even when entering values, the screen is not stained it's just a black or green or red square I modified the code a little and added values but I have an error message I even have the impression that it won't work because of the INT RGB TYPE of the bufferedimage THANKS
double H = 120.25;
                double S = 1.25;
                double V = 1.25;
                double C = V * S;
                double X = C * (1 - ((H / 60) % 2) - 1);
                double m = V - C;
        
                double red = 25.02;
                double green = 120.25;
                double blue = 25.56;
ronald ronald

2023/9/2

#
java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green Blue at java.desktop/java.awt.Color.testColorValueRange(Color.java:310) at java.desktop/java.awt.Color.<init>(Color.java:395) at java.desktop/java.awt.Color.<init>(Color.java:369) at greenfoot.Color.<init>(Color.java:120) at PlasmaEffectWorld.drawPlasmaHSB(PlasmaEffectWorld.java:123) at PlasmaEffectWorld.act(PlasmaEffectWorld.java:40) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
ronald ronald

2023/9/2

#
OK sorry I see that I did something stupid again by the way line 123 corresponds to lines 31 36 41 46 51 56 and line 40 is the method in act ie drawPlama()
ronald ronald

2023/9/2

#
ok for the mistake, it's not complicated I simply divided by 10 or 100 the background image - line 123 - but it remains black but hey it doesn't matter, I don't expect a miracle like plasma effect
danpost danpost

2023/9/3

#
ronald wrote...
I even have the impression that it won't work because of the INT RGB TYPE of the bufferedimage << Code Omitted >>
I believe the code given here is difficult to follow. It is difficult to tell what any of the variables are for or what you are trying to do with them. However, I also believe that a lot of what your are doing is not necessary. The only thing that varies in value is H, presuming that the plasma array is filled with different values. Maybe check that first (that there are different values in the array). However, the range of values you check the variable H for is huge compared to the range H could possibly be. In fact, the range of H is exactly one (1) and the range of values for changing color is sixty (60). The range of H needs to be 360 to get all possible color value combinations
danpost danpost

2023/9/3

#
I also notice you were using this: (not a line of code, but an expression)
    (int) plasma[y][x] % 1
Regardless of which thing the compiler does first ('int'ing of 'mod'ing), the result will be zero. If 'int'ing first, the decimal part of the plasma value is dropped, then when 'mod'ing by 1, the integer part is dropped, leaving zero. If 'mod'ing first, the units are dropped first, then the 'int'ing will drop the decimal part, again leaving zero as the result. I believe that the compiler will actually do the 'int'ing first, and to 'mod' first, you would have to use parenthesis:
    (int) (plasma[y][x] % 1)
But, again, this combination of operations ('int'ing and 'mod'ing by 1) will always result in zero.
ronald ronald

2023/9/3

#
firstly i found this code in rosetta code and yes it is difficult to follow with all these values but in my opinion it will not work on greenfoot maybe on eclipse or netbeans if I understand correctly TYPE INT RGB gives the colors in java awt image bufferedimage or all the shades of colors but on greenfoot it does not work. that's how I understand maybe I'm wrong I didn't expect a miracle but hey, too bad and I want to tell you that I found a FastImage plasma scenario on greenfoot with all the effects. easy to understand but I can figure it out
danpost danpost

2023/9/3

#
ronald wrote...
if I understand correctly TYPE INT RGB gives the colors in java awt image bufferedimage or all the shades of colors
What is the range of values (min and max) in the plasma array?
int min = Math.Float.MAX_VALUE;
int max = Math.Float.MIN_VALUE;
for (int y=0; y<plasma.length; y++) {
    for (int x=0; x<plasma[y].length; x++) {
        if (plasma[y][x] < min) min = plasma[y][x];
        if (plasma[y][x] > max) max = plasma[y][x];
    }
}
System.out.println("MIN:  "+min+"\n"MAX:  "+max);
Use this code after the array is filled with its values and copy/paste the output here in a post. Immediately afterwards, remove these lines from your code.
ronald ronald

2023/9/3

#
D ah yes, it’s a good idea to do a system out print me who never thinks about that this sometimes allows you to know the results I also take advantage of this to know the ranges of colors
double H = 360;
double S = 1;
double V = 1;
RED => 255.0 GREEN => 0.0 BLUE => 0.0 MIN => -2147483648 MAX => 2147483647
ronald ronald

2023/9/3

#
otherwise with with code these are the same results
double H = hueShift + (int) (plasma[y][x] % 1);
//double H = 0;
double S = 1;
double V = 1;
float hueShift = 360;
RED => 255.0 GREEN => 0.0 BLUE => 0.0 MIN => -2147483648 MAX => 2147483647
ronald ronald

2023/9/3

#
frankly, I don't understand anything I did the same thing again but this time I closed my project and reopened several times and it gives nothing in minimum or maximum integer value on the other hand there is red
 int min = Integer.MIN_VALUE;
 int max = Integer.MAX_VALUE;
if (plasma[y][x] < min) min = (int) plasma[y][x];
if (plasma[y][x] > max) max = (int) plasma[y][x];
and I also modified your code a little it drags or slows down
ronald ronald

2023/9/3

#
it was the printing of the colors that slowed down so I deleted ultimately it gives the same result as at the beginning but not the right color GREEN
double H = hueShift + (int) (plasma[y][x] % 1);
RED
double H = 360;
There are more replies on the next page.
1
2