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

2015/12/3

How to change the color of an object in Boids

Wolfpack797 Wolfpack797

2015/12/3

#
Hello, my name is Ramirez. How do I change the color of an object import greenfoot.*; import java.awt.Color; /** * A world for the boids. * * @author Poul Henriksen * @version 2.0 */ public class Sky extends World { public Sky() { super(800, 600, 1); getBackground().setColor(new Color(220,220,100)); getBackground().fill(); populateTrees(100); populateBoids(50); } public void populateBoids(int number) { for(int i=0; i < number; i++) { int x = (int) (Math.random() * getWidth()); int y = (int) (Math.random() * getHeight()); Boid b = new Boid(); addObject(b, x, y); } } public void populateAntiboids(int number) { for(int i=0; i < number; i++) { int x = (int) (Math.random() * getWidth()); int y = (int) (Math.random() * getHeight()); Antiboid a = new Antiboid(); addObject(b, x, y); Greenfoot Image bird = getImage(); image.color(); } } public void populateTrees(int number) { // Size of block in pixels int blockSize = 200; // Trees per block int treesPrBlock = 10; // How close together trees in a block are. Higher number, the closer they are. int blockCoherence = 1; for(int block = 0; block < number / treesPrBlock; block++) { int x = Greenfoot.getRandomNumber(getWidth()/blockSize) * blockSize; int y = Greenfoot.getRandomNumber(getHeight()/blockSize) * blockSize; for(int t = 0; t < treesPrBlock; t++) { int dx = Greenfoot.getRandomNumber(blockSize/blockCoherence); int dy = Greenfoot.getRandomNumber(blockSize/blockCoherence); Tree b = new Tree(); addObject(b, x + dx, y + dy); } } } }
danpost danpost

2015/12/3

#
You are showing these lines of code:
Greenfoot Image bird = getImage();
image.color();
Line 1 has two issues. One is that the 'getImage' method is not a member of the World class and it does not appear to be included in this Sky class. The other issue is that 'Greenfoot Image' should not have the space in the middle of it. The problem with line 2 is that the variable 'image' is not declared anywhere. By the name of the variable, I would presume it would hold a GreenfootImage object; but, 'color', the method you are calling on this object, is not a member of the GreenfootImage class. First, you will need a way to obtain the different colored images you will be using for the Actor instances. They can be saved in image files or they can be created programmatically. These images should be created or read in from the files at start-up to ensure prevention of any lag during play due to processing of these actions. Then, you will need to show some code attempting to utilize these images.
You need to login to post a reply.