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

2013/1/21

Recoloring a image used from a file

TisLars TisLars

2013/1/21

#
Hello, I'm working on a basic particle system. However, I have 3 images .png images (star, diamond, circle) and they are all white. I want this image to get a random number to generate a new color. Here is the code used right now:
import greenfoot.*;
import java.awt.Color;

public class cParticle extends cParticleSystem
{
	int dirX;
	int dirY;
	int Power;
	int Life;

	public cParticle(int X, int Y, int power, int color, int life)
	{
		int imgNum = (Greenfoot.getRandomNumber(3) + 1);
		GreenfootImage display = new GreenfootImage("particle_" + imgNum + ".png");
		display.setColor(new Color(color, color, color));
		setImage(display);
		
		dirX = X;
		dirY = Y;
		Power = power;
		Life = life;
	}

	public void act()
	{
		setLocation(getX() + dirX + (Power), getY() + dirY + (Power));
		update();
	}

	public void update()
	{
		if (Life > 5) {
			Life--;
			if (Power > 0) {
				Power--;
			}
			getImage().setTransparency(Life);
		}
	}
}
The images still appear as white. Is there a way to let Greenfoot overwrite the colors of a imported image? Thanks!
danpost danpost

2013/1/21

#
If all three color parts (red, green and blue) have the same value for all the colored pixels in your images, you could check and replace the colors one at a time. Even if the color parts are fairly close in value, you could check the ranges and do the same. However, it might be best in your case to create the images yourself. You could do this programmatically; or you could use a paint program to fill/draw the areas with different colors and save each different colored/shaped image independently and change the image by setting it to a different filename.
You need to login to post a reply.