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

2011/12/14

How to make an image black and white?

DerriQ DerriQ

2011/12/14

#
I am trying to manipulate an image to be black and white, and have tried many different codes, but can't figure it out. Can anyone help me please?
dordor dordor

2011/12/15

#
Okay, I think you should look at the GreenfootImage class documentation. do some thing like:
1
2
3
4
GreenfootImage image=getImage();
image.setColor(Color.WHITE);
image.fill();
setImage(image);
but to do that you need the code line
1
import java.awt.Color;
at the top of the code.
Busch2207 Busch2207

2011/12/15

#
try this code:
1
2
3
4
5
6
7
8
9
10
11
   GreenfootImage getBlackWhiteImage(String Image){
   GreenfootImage i = new GreenfootImage(Image);
   for(int x=0; x<i.getWidth();x++)
       for(int y=0; y<i.getHeight();y++)
       {
           java.awt.Color C = i.getColorAt(x,y);
           int brightness = (C.getRed()+C.getGreen()+C.getBlue())/3;
           i.setColorAt(x,y, new java.awt.Color ( brightness, brightness, brightness, C.getAlpha()));
   }
   return i;
}
You need to login to post a reply.