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

2014/4/24

ColorTest

adam0314 adam0314

2014/4/24

#
I thought everything was correct, but I keep getting an error "cannot find symbol- variable x" The line in question is line 34. I have provided all code.

ColorTest Subclass of World

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ColorTest here.
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ColorTest extends World
{

    /**
     * Constructor for objects of class ColorTest.
     * 
     */
    public ColorTest()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(400, 400, 1); 
        
        boolean black = true;
        
        for (int x = 0; x < 8; x++)
            {
                if(black) //alternate colors at the start of each column
                    black = false;
                else
                    black = true;
            }
        for (int y = 0; y < 8; y++)
            {
                if(black) //alternate the colors
                    {   
                        ColorPatch blackSquare = new ColorPatch (0,0,0);
                        addObject(blackSquare, x*50 + 25, y*50 + 25);
                        black = false;
                    }
                else
                    {
                        ColorPatch redSquare = new ColorPatch(255,0,0);
                        addObject(redSquare, x*50 + 25, y * 50 + 25);
                        black = true;
                    }
                
             
    }


}}

Actor ColorPatch

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ColorPatch here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ColorPatch extends Actor
{
    /**
     * Act - do whatever the ColorPatch wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public ColorPatch(int r, int g, int b)
    {
        GreenfootImage img = new GreenfootImage(50, 50);
        img.setColor (new java.awt.Color(r,g,b));
        img.fill();
        setImage(img);
        
    }    
}
danpost danpost

2014/4/24

#
Move line 28 to like around 44. You are ending the 'x' loop to soon.
danpost danpost

2014/4/24

#
There is a so much easier way to code this. In fact, everything from line 20 on in your ColorTest class can be reduced to one line (not that you would necessarily want to code it this way):
for (int x=0; x<8; x++) for (int y=0; y<8; y++) addObject(new ColorPatch(((x+y+1)%2)*255, 0, 0), x*50+25, y*50+25);
The reason this works is because the sum of the x and y will be even for one color and odd for the other. I do not think I have the colors reversed, but if I do, you would use '(x+y)' instead of '(x+y+1)'.
You need to login to post a reply.