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

2015/7/22

ColorTest assignment

Turtle Turtle

2015/7/22

#
I am working on a ColorTest assignment, and need to create a checker board background. I have it compiled but instead of red and black squares i get Greenfeet in a grid pattern instead of red and black squares. World- ColorTest import greenfoot.*; /** * Write a description of class ColorTest here. * * @author (Tyler Schenk) * @version (21July2015) */ public class ColorTest extends World { /** * Constructor for objects of class ColorTest. * */ public ColorTest() { super(400, 400, 1); boolean black = true; for (int x = 0; x < 8; x++) { if (black) black = false; else black = true; for (int y = 0; y < 8; y++) { if (black) { 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 - Class import greenfoot.*; /** * Write a description of class ColorPatch here. * * @author (Tyler Schenk) * @version (21July2015) */ 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 void act() { // Add your action code here. } public ColorPatch (int r, int g, int b) { GreenfootImage img = new GreenfootImage (80, 20); img.setColor (new java.awt.Color(201,0,153)); img.fill(); } }
danpost danpost

2015/7/22

#
One major issue is that you have 'r', 'g' and 'b' color part values passed to the ColorPatch constructor, but they are not used within the constructor code block. The size of the GreenfootImage created within that constructor does not match what grid you are trying to create, either.
Turtle Turtle

2015/7/22

#
Could you please show me what you mean please?
danpost danpost

2015/7/22

#
Regardless of what values you use in 'new ColorPatch(??, ??, ??)', that is passed to:
public ColorPatch(int r, int g, int b)
the ColorPatch object will always the size (80, 20) with color (r=201, g=0, b=153) because of the lines
GreenfootImage img = new GreenfootImage(80, 20);
img.setColor(new java.awt.Color(201, 0, 153));
img.fill();
Another issue is that the image created is not being set to the actor.
Turtle Turtle

2015/7/22

#
I see what your saying but with this being new to me, i am not sure how to fix it. I just need to create a simple checker board, and its kicking my butt.
danpost danpost

2015/7/22

#
Instead of (201, 0, 153), use (r, g, b) and add a statement at the end to set the image to the actor (see the Actor class API documentation for the method to use).
Turtle Turtle

2015/7/22

#
Is this what correct? I dont think the last command is correct. import greenfoot.*; /** * Write a description of class ColorPatch here. * * @author (Tyler Schenk) * @version (21July2015) */ 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 void act() { // Add your action code here. } public ColorPatch (int r, int g, int b) { GreenfootImage img = new GreenfootImage (80, 20); img.setColor (new java.awt.Color(r, g, b )); <-----CHANGED TO RGB img.fill(); } public GreenfootImage getImage(new ColorPatch); <-----ADDED }
danpost danpost

2015/7/22

#
The change was correct; but, the addition is wrong. First off, the addition needs to within the constructor; secondly, you need to use the 'setImage' method -- not the 'getImage' one.
Turtle Turtle

2015/7/22

#
import greenfoot.*; /** * Write a description of class ColorPatch here. * * @author (Tyler Schenk) * @version (21July2015) */ 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 void act() { // Add your action code here. } public ColorPatch (int r, int g, int b) { GreenfootImage img = new GreenfootImage (80, 20); img.setColor (new java.awt.Color(r, g, b )); img.fill(); setImage(ColorPatch); <------Changed to this, but recieving an error "cannot find symbol- variable ColorPatch" } }
danpost danpost

2015/7/22

#
The 'setImage' method is the one to use; however, it requires the image to be set to the actor as the parameter -- not the name of the class of the actor. There is no reference to the image, 'img', within that line of code you created.
Turtle Turtle

2015/7/22

#
Got it to work danpost, thank you for much for your time. ^_^
You need to login to post a reply.