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

2013/4/5

Creating squares

mattjames mattjames

2013/4/5

#
Hi guys. Can someone please help me/write me the code to create a simple actor in a shape of a square, any colour, 60 by 60, because I don't get what it tells me to do in the documentation. Thanks
JetLennit JetLennit

2013/4/5

#
you mean create an image with a image editor?
danpost danpost

2013/4/5

#
By stating 'any colour', I presume you mean 'any random colour', and you will need an extra import statement. The code follows:
GreenfootImage image; // for base image
image = new GreenfootImage(60, 60); // create base image
int red = Greenfoot.getRandomNumber(255); // random part for red
int green = Greenfoot.getRandomNumber(255); // random part for green
int blue = Greenfoot.getRandomNumber(255); // random part for blue
Color color; // for random color
color = new Color(red, green, blue); // create random color
image.setColor(color); // set the color
image.fill(); // paint image with set color
setImage(image); // apply the image to the object
Oh yeah, the import statement you need is:
import java.awt.Color;
mattjames mattjames

2013/4/7

#
That's great thanks.
You need to login to post a reply.