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

2015/11/3

Hello, I have been trying to change the color of the body when it bounces off the edge in Newton's Lab 3. Here is the current code:

1
2
Mack Mack

2015/11/3

#
private void bounceAtEdge()
{
   if (getX() == 0 || getX() == getWorld().getWidth()-1) { 
     
        setLocation((double)getX(), (double)getY());
        getMovement().revertHorizontal();
        accelerate(0.9);
        changeColor()
    }
     
    else if (getY() == 0 || getY() == getWorld().getHeight()-1){
     
        setLocation((double)getX(), (double)getY());
        getMovement().revertVertical();
        accelerate(0.9);
        changeColor()
    }
     
}
 
public void changeColor()
{
    int g = Greenfoot.getRandomNumber(255);
    int r = Greenfoot.getRandomNumber(255);
    int b = Greenfoot.getRandomNumber(255);    
    image.setColor (color);
           
}
danpost danpost

2015/11/3

#
Your 'changeColor' method does one thing -- and one thing only. It sets the drawing color for the image to the color white. It does not change the image in any way; it only assigns the color for future drawing methods (by way of the 'setColor' method call). The 'r', 'g' and 'b' variables are not used after assigned vales within the method. Also, you did not give any information as to what the 'color' field might be (it obviously holds a color -- but the method does not alter the value of the 'color' field).
Mack Mack

2015/11/6

#
this is the code i managed to add but it keeps saying "cannot find symbol - varible rc"
Mack Mack

2015/11/6

#
public boolean atWorldEdge()
    {
        if (getX() <20 || getX() > getWorld() .getWidth() -20)
        return true;
        if (getY() < 20 || getY()> getWorld() .getHeight() -20)
        return true;
        else
        return false;
Color randomColor = new Color(rc, rc, rc);  
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
            image.setColor(randomColor);
                     
         }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(.9);
            image.setColor(randomColor);
             
         }
danpost danpost

2015/11/6

#
Line 8 should be the last line in your 'atWorldEdge' method (you need a closing squiggly bracket after it). Then, you will be missing the method declaration line for the code starting at line 9 (it appears some lines may have been deleted by accident).
Mack Mack

2015/11/6

#
I'm not sure I follow, can you please show me what that step looks like?
danpost danpost

2015/11/6

#
Please show the entire class code. It would be difficult to say what code is missing otherwise (it may still be after looking at the entire class, however).
Jason21 Jason21

2015/11/6

#
I need help with the same stepes please.
Mack Mack

2015/11/6

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

/**
 * 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 Kölling 
 *   (Including improvements by J. Buhl.)
 * @version 1.2
 */
public class Body extends SmoothMover
{
    
    private static final double GRAVITY = 5.8;
    private static final Color defaultColor = new Color (Greenfoot.getRandomNumber(250),
    (Greenfoot.getRandomNumber(250)),
    (Greenfoot.getRandomNumber(250)));
    private double mass;
    
    /**
     * Construct a Body with default size, mass, velocity and color.
     */
    public Body()
    {
        this (20, 300, new Vector(0, 0.0), defaultColor);
    }
    
    /**
     * Construct a Body with a specified size, mass, velocity and color.
     */
    public Body(int size, double mass, Vector velocity, Color color)
    {
        this.mass = mass;
        addToVelocity(velocity);
        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
        Color randomColor = new Color(rc);  
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
            image.setColor(randomColor);
                     
         }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(.9);
            image.setColor(randomColor);
             
         }
    }
    
    /**
     * Act. That is: apply  the gravitation forces from
     * all other bodies around, and then move.
     */
    public void act() 
    {
        applyForces();
        move();
        bounceAtEdge();
        if(atWorldEdge())
        {
            turn (180);
            new Color (Greenfoot.getRandomNumber(250), 
            Greenfoot. getRandomNumber(250), 
            Greenfoot. getRandomNumber(250));
            
        }
    }
    
    /**
     * Check whether we have hit the edge of the universe. If so, bounce off it.
     */
    private void bounceAtEdge()
    {
    
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            invertHorizontalVelocity();
            accelerate(0.9);
        }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            invertVerticalVelocity();
            accelerate(0.9);
        }
    }
    
    /**
     * Apply the forces of gravity from all other celestial bodies in this universe.
     */
    private void applyForces()
    {
        List<Body> bodies = getWorld().getObjects(Body.class); 
        
        for (Body body : bodies) 
        {
            if (body != this) 
            {
                applyGravity(body);
            }
        }
        
        // ensure that we don't get too fast: If the current speed is very fast, decelerate a bit.
        if (getSpeed() > 7) 
        {
            accelerate (0.9);  // acceleration with factor < 1 is actually slowing down.
        }
    }
    
    /**
     * Apply the force of gravity of a given body to this one.
     * (Force is applied for one time unit; dt==1.)
     */
    private void applyGravity(Body other)
    {
        double dx = other.getExactX() - this.getExactX();
        double dy = other.getExactY() - this.getExactY();
        Vector dv = new Vector (dx, dy); //sets direction correctly; length still invalid
        double distance = Math.sqrt (dx*dx + dy*dy);
        double force = GRAVITY * this.mass * other.mass / (distance * distance);
        double acceleration = force / this.mass;
        dv.setLength (acceleration);
        addToVelocity (dv);
    }
    
    /**
     * Return the mass of this body.
     */
    public double getMass()
    {
        return mass;
    }
    /**
     * Check if object is at border edge, if so change its color
     */
    public void colorAtEdge()
    {
       if (getX() == 0 || getX() == getWorld().getWidth()-1)
       {
         int r = Greenfoot.getRandomNumber(256);
         int g = Greenfoot.getRandomNumber(256);
         int b = Greenfoot.getRandomNumber(256);  
         GreenfootImage imageColor = getImage();
         int imageHeight = getImage().getHeight();
         imageColor.setColor(new Color (r,g,b));
         imageColor.fillOval(0, 0, imageHeight, imageHeight);
        }
       else if (getY() == 0 || getY() == getWorld().getHeight()-1)
       {
         int r = Greenfoot.getRandomNumber(256);
         int g = Greenfoot.getRandomNumber(256);
         int b = Greenfoot.getRandomNumber(256);  
         GreenfootImage imageColor = getImage();
         int imageHeight = getImage().getHeight();
         imageColor.setColor(new Color (r,g,b));
         imageColor.fillOval(0, 0, imageHeight, imageHeight);         
        }
    }
}
danpost danpost

2015/11/6

#
Did you have a Body class given you before you started adding things to the code?
Mack Mack

2015/11/6

#
yeah I did. This scenario contained 3 actor classes. Obstacle, SmoothMover, and Body
danpost danpost

2015/11/6

#
Replace the Body class with the original you had started with and begin from there.
Mack Mack

2015/11/6

#
the only code I added to the body class was this code
Color randomColor = new Color(rc);  
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
            image.setColor(randomColor);
                      
         }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(.9);
            image.setColor(randomColor);
              
         }
    }
Mack Mack

2015/11/6

#
actually it was this method that was added.
public boolean atWorldEdge()
    {
        if (getX() <20 || getX() > getWorld() .getWidth() -20)
        return true;
        if (getY() < 20 || getY()> getWorld() .getHeight() -20)
        return true;
        else
        Color randomColor = new Color(rc);  
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
            image.setColor(randomColor);
                      
         }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(.9);
            image.setColor(randomColor);
              
         }
    }
     
danpost danpost

2015/11/6

#
Where did acquire the code you added to the Body class? It is just a code snippet -- not a method in itself. The 'rc' variable needs to be declared prior to when the snippet itself is executed; 'rc' must be an int value between zero and 256^3-1 (after looking at the possible ways to construct a Color object).
There are more replies on the next page.
1
2