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

2012/3/10

Here's a challenge for someone! :)

1
2
Duta Duta

2012/3/10

#
Re-create this! (its also in a different version here) Maybe this could be a thing multiple people work on even: Everyone can contribute little bits of code toward it - then again maybe one person could just do it
kiarocks kiarocks

2012/3/10

#
Wow, that looks hard.
Duta Duta

2012/3/10

#
I've only just found both of the versions have an extra feature I didn't notice before. Hold down the mouse button while moving it - it has different effects between the 2 versions
darkmist255 darkmist255

2012/3/11

#
Very interesting, no idea how to go about it :D.
matt.milan matt.milan

2012/3/11

#
here's what i got so far. for 1, i get a null pointer exception if i actually try to call the mouseDraw() method. for testing purposes (not included here) i created an actor that moves around a bit and then calls, essentially, the mousedraw method on its own xy position, instead of using mouse xy position. i notice he's followed by a white pixel that disappears almost immediately. my next step is to change the fadeout code from setting the background color to black, to setting the background color incrementally less than it's current value. but i'm not sure yet how to get a number value from java.awt.Color. I'm still working on it. public class Black_Background extends World { /** * Constructor for objects of class Black_Background. * */ public Black_Background() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject (new Test_Drawer(), 100,100); } public void act() { // mouseDraw(); fadeOut(); } public void mouseDraw() { getBackground().setColorAt(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY(), Color.WHITE); } public void fadeOut() { for (int i = 0; i < getWidth(); i++) { for (int j = 0; j < getHeight(); j++) { if (getBackground().getColorAt(i,j) != Color.BLACK) { getBackground().setColorAt(i,j, Color.BLACK); } } } } }
Duta Duta

2012/3/11

#
@matt.milan To get around that exception, here's one way:
public void mouseDraw()
{
	MouseInfo m = Greenfoot.getMouseInfo();
	if(m == null) return;
	getBackground().setColorAt(m.getX(),
		m.getY(), Color.WHITE);
}
Also enclose your code that you post on here with tags - it makes it easier to read :)
kiarocks kiarocks

2012/3/11

#
You get a color value by color.getBlue() for blue, color.getGreen() for green, etc.
matt.milan matt.milan

2012/3/11

#
thanks for the instruction duta, that fixed it alright. here's my modified mouseDraw code, i put in a check to see if the x/y values are out of range, beucase it was crashing me out.
    public void mouseDraw()
    {
        MouseInfo m = Greenfoot.getMouseInfo();
        if (m == null) return;
        if (!(m.getX() > getWidth() || m.getY() > getHeight()))
        {
            getBackground().fillOval(m.getX(), m.getY(), 10,10);            
        }
    }
and here's the code for fadeout, at first it was crashing out because i was reaching negative values for color which is no good,
    public void fadeOut()
    {
        for (int i = 0; i < getWidth(); i++)
        {
            for (int j = 0; j < getHeight(); j++)
            {
                fadeColor = getBackground().getColorAt(i,j);
                int r = fadeColor.getRed();
                int b = fadeColor.getBlue();
                int g = fadeColor.getGreen();
                if (r > 5)
                {
                    r-=5;
                }
                if (b > 5)
                {
                    b-=5;
                }
                if (g > 5)
                {
                    g-=5;
                }                
                
                if (getBackground().getColorAt(i,j) != baseColor)
                {
                    getBackground().setColorAt(i,j, new Color(r,b,g));
                }
            }
        }
    }
matt.milan matt.milan

2012/3/11

#
after reflecting on my code for a bit, i think i'm supposed to be writing it differently instead of the mouse painting the background, the mouse should "shoot" objects that paint the background as they move. they should have a lifespan relative to their velocity. they should get the color at each pixel and modify it slightly based on different behavior.
matt.milan matt.milan

2012/3/11

#
about done for the night... so far we got this
import java.awt.Color;
import greenfoot.*
public class Black_Background extends World
{
    private MouseInfo mouseNew;
    private MouseInfo mouseOld;
    private Color fadeColor = new Color(0,0,0);
    private Color baseColor = new Color(0,0,0);
    private Color myColor = new Color (0,200,200);

    private int r;
    private int b;
    private int g;

    public void act()
    {
        mouseShootParticle();
        fadeOut();
    }

    public void mouseShootParticle()
    {
        mouseNew = Greenfoot.getMouseInfo();
        if (mouseNew == null ) return;

        if (mouseOld != mouseNew)
        {
            if (!(mouseNew.getX() > getWidth() || mouseNew.getY() > getHeight()))
            {
                addObject(new ColorParticle(calculateMouseSpeed(),
                                            calculateMouseDirection()),
                                           mouseNew.getX(), mouseNew.getY());
                //addObject(new ColorParticle(10, 10),mouseNew.getX(), mouseNew.getY());
            }
            mouseOld = mouseNew;
        } else
        if (mouseOld == mouseNew)
        {
            //idleMouseParticles();
        }
    }

    public void fadeOut()
    {
        for (int i = 0; i < getWidth(); i++)
        {
            for (int j = 0; j < getHeight(); j++)
            {
                if (getBackground().getColorAt(i,j) != baseColor)
                {
                    fadeColor = getBackground().getColorAt(i,j);
                    r = fadeColor.getRed();
                    b = fadeColor.getBlue();
                    g = fadeColor.getGreen();
                    if (r > 10)
                    {
                        r-=5;
                    } else
                    if (r <= 10)
                    {
                        r = 0;
                    }
                    if (b > 10)
                    {
                        b-=5;
                    } else
                    if (b <= 10)
                    {
                        b = 0;
                    }
                    if (g > 10)
                    {
                        g-=5;
                    } else
                    if (g <= 10)
                    {
                        g = 0;
                    }
                    getBackground().setColorAt(i,j, new Color(r,b,g));
                }
            }
        }
    }

    public int calculateMouseSpeed()
    {
        return (int)(calculateMouseDistance() / 1);
    }
    public int calculateMouseDirection()
    {
        return (int)(Math.tan(calculateMouseSlope()));
    }

    public double calculateMouseDistance()
    {
        return Math.sqrt(Math.pow(mouseOld.getX() - mouseNew.getX(),2)
            + Math.pow(mouseOld.getY() - mouseNew.getY(),2));
    }
    public double calculateMouseSlope()
    {
        if (Greenfoot.mouseMoved(null))
        {
            return (mouseOld.getY() - mouseNew.getY() / (mouseOld.getX() - mouseNew.getX()));
        }
        else
        {
            return 1;
        }
    }
I think my math is wrong in calculateMouseSpeed and calculateMouseDirection. i am also experiencing a divide by zero error on my calculateMouseSlope if the mouse isn't moving enough, or is moving too much.
Duta Duta

2012/3/11

#
For calculateMouseDirection I think something like this might work (haven't tested it though):
public int calculateMouseDirection() throws Exception
{
	(int)(180 * Math.atan2(mouseNew.getY() - mouseOld.getY(), mouseNew.getX() - mouseOld.getX()) / Math.PI)
}
Also once you have that you can basically remove calculateMouseSlope(). To call this method do:
try {
	addObject(new ColorParticle(calculateMouseSpeed(),  
		calculateMouseDirection()),  
		mouseNew.getX(), mouseNew.getY());
} catch(Exception e) {}
Duta Duta

2012/3/12

#
So far we're up to here.
kiarocks kiarocks

2012/3/12

#
A bit slow/laggy?
matt.milan matt.milan

2012/3/12

#
it does feel slow, i wonder if there's a limit to how far we can optimize this in greenfoot. duta, take a look at this (this is in world)
    public void grow()
    {
        if (Greenfoot.mousePressed(null))
        {
            shapeSize= 150;
        }
        if (Greenfoot.mouseClicked(null))
        {
            shapeSize = 20;
        }
    }
and then when the mouse fires a particle you call shapesize
            if (!(mouseNew.getX() > getWidth() || mouseNew.getY() > getHeight()))
            {
                try {  
                addObject(new ColorParticle(calculateMouseSpeed(),    
                calculateMouseDirection(), shapeSize),    
                mouseNew.getX() - (shapeSize / 2), mouseNew.getY() - (shapeSize / 2));  
                } catch(Exception e) {}  
               
                
            }
that's how i made it so that when you drag, only new particles get big, the old ones stay small
kiarocks kiarocks

2012/3/12

#
Found the reason for slow: when you darken the world it causes tons of lag
There are more replies on the next page.
1
2