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

2015/11/4

Changing Body's Colors

GreenfootMolly GreenfootMolly

2015/11/4

#
I need help changing the body's colors as they hit the edge of the world.
danpost danpost

2015/11/5

#
Please show the class code of the actor whose color needs changed. Indicate how the image is set to the actor and include any other pertinent information.
GreenfootMolly GreenfootMolly

2015/11/5

#
danpost danpost

2015/11/5

#
Unmatched tags in your last post caused it not to post properly. Here it what it was supposed to show:
GreenfootMolly wrote...

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
//72, 213, 250

/**
 * A 'Body' is any kind of object in space that has a mass. It could be a star, or a planet, 
 * or anything else that floats around in space.
 * 
 * @author Michael Kolling 
 * @version 0.1
 */
public class Body extends SmoothMover
{
    // constants
    private static final double GRAVITY = 5.8;
    private static final Color defaultColor = new Color(Greenfoot.getRandomNumber(250), Greenfoot.getRandomNumber(250), Greenfoot.getRandomNumber(250)); //final means that when this is run, it cant change. this is a constant
    
    // fields
    private double mass;
    
    /**
     * Construct a Body with default size, mass, movement and color.
     */
    public Body()
    {
        this (20, 300, new Vector(0, 1.0), new Color(Greenfoot.getRandomNumber(250), Greenfoot.getRandomNumber(250), Greenfoot.getRandomNumber(250)));
    }
    
    /**
     * Construct a Body with a specified size, mass, movement and color.
     */
    public Body(int size, double mass, Vector movement, Color color)
    {
        this.mass = mass;
        addForce(movement);
        GreenfootImage image = new GreenfootImage (size, size);
        image.setColor (color);
        image.fillOval (0, 0, size-1, size-1);
        setImage (image);
    }
        public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    /**
     * Act. That is: apply  the gravitation forces from
     * all other bodies around, and then move.
     */
    public void act() 
    {
        applyForces();
        move();
        if ( atWorldEdge() ) 
        {
            turn(180);
            new Color(Greenfoot.getRandomNumber(250), Greenfoot.getRandomNumber(250), Greenfoot.getRandomNumber(250));
        }
        
    }
    private void applyForces() //only gets bodies. I am included in the list.
    {
        List<Body> bodies = getWorld().getObjects(Body.class); //asks "whats the world im in?" and "give me the other things in the world."
        for (Body body : bodies) //a form of a loop. repeats for how many bodies there are. this applies gravity to me. this says for every induvidual item, repeat.
        {
            if (body != this) { //as long as the body isn't myself, and then applies gravity to it.
                applyGravity (body);
    }
}
}
    private void applyGravity(Body other) 
    {
        double dx = other.getExactX() - this.getExactX();
        double dy = other.getExactY() - this.getExactY();
        Vector force = new Vector (dx, dy);
        double distance = Math.sqrt (dx * dx + dy * dy); //this is the mass squared. the mass affects the gravity
        double strength = GRAVITY * this.mass * other.mass /
                                    (distance * distance); //how far away you are. gets bigger for long distances
                      double acceleration = strength / this.mass;
                      force.setLength (acceleration);
                      addForce (force);
}
    /**
     * Return the mass of this body.
     */
    public double getMass()
    {
        return mass;
    }
}
It was missing the closing 'code' tag.
You need to login to post a reply.