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

2013/10/27

Breakout Change Brick Color Help

LugNut29 LugNut29

2013/10/27

#
Hi, I am very new to programming and I am taking a class that is using Greenfoot. What we need to do is take a blank Breakout game and add three bricks that are oversized. I found where I could change the size in the code, but I am unable to add the color to the new brick when I add it to the BreakoutWorld. I get an error "incompatible type" when put this "RED" in the Popup Box. So..it looks like this new Brick("RED") then I get the error. What am I doing wrong? Is there something I'm missing? Thanks, T
With java, you don't set colors with Strings (String str = "string") you set colors using java.awt.Color (Color col = new Color()). If you want to use Color, make sure to import java.awt.Color before the rest of your class (up next where it says "import greenfoot.*;). Also, Color has a couple static Colors that you can use. If you want to set the color of something (going to assume that you're setting drawing color in GreenfootImage) do something like this:
1
2
3
img.setColor(Color.red);
 
img.setColor(Color.blue);
You can't do:
1
img.setColor("Red");
Because you're trying to use a String, where you need to use a Color. Here you can see the rest of Color: Color
SPower SPower

2013/10/27

#
Sidenote: Color.red and Color.blue are perfectly valid, however, the uppercase versions of those colors are also present. The 2 are the exact same, but the uppercase are more java-ish, because constants should always be written in all uppercase. So this:
1
2
3
4
5
6
img.setColor(Color.red);
img.setColor(Color.darkGray);
 
// should be this:
img.setColor(Color.RED);
img.setColor(Color.DARK_GRAY);
again, they have the same effect, but it's a better practise to use uppercases for constants. And it also makes you aware how you should make constants yourself.
LugNut29 LugNut29

2013/10/27

#
Hey All, Thanks for the info above. I verified the java.awt.Color is being called. What I'm trying to do is add a new brick to the Greenfoot GUI and it's asking for new brick(). What do I put in between round brackets? For example, I right click on the actor brick and select new Brick(Color theColor) and I move it to the world, it brings up a popup box asking for input. The input is new brick(something?). Sorry, but I am really green when it comes to programming. Thanks again for all your help.
You need to put in a color, if doing it manually, i think you can type in: "java.awt.Color.red". Otherwise, you want to do it from the world (that's where you're going to be making the bricks anyways).
LugNut29 LugNut29

2013/10/27

#
Ahhh! That worked! Thank you kindly FlyingRabid!
You need to login to post a reply.