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

2011/7/15

How can I change the Drawing Color of GreenfootImage?

GameCode GameCode

2011/7/15

#
I know there's a method called 'setColor(java.awt.Color color)' in GreenfootImage. But what shall I write in the brackets??? GreenfootImage img = new GreenfootImage(100,100); img.setColor("white"); // does not work img.setColor(white); // does not work too
mik mik

2011/7/15

#
You need to pay close attention to the type of the parameter in the method signature. The signature is setColor(java.awt.Color color) so the type of the parameter is java.awt.Color That means: It expects an object of class Color, which you find in the Java library in the java.awt package. Next, you should check the javadoc for this class. It will show you that there are two ways to get access to Color objects: via predefined constants, or via constructors. For both versions, you should first import the class Color into your class:
import java.awt.Color;
Then, the first version (using constants) looks like this:
img.setColor(Color.WHITE);
(Checking the javadoc for the Color class will tell you all the color constants you can use, besides WHITE.) The second method is to construct a color object from RGB values. For example
img.setColor(new Color(128, 24, 32));
This allows you to create any color value in the full palette. (There is also a constructor with a fourth parameter, the alpha value, if you need transparency.)
GameCode GameCode

2011/7/15

#
Thank you very much =)
You need to login to post a reply.