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

2015/11/22

Compare a picture with another

Ludmuterol Ludmuterol

2015/11/22

#
I'm progamming a game, where a square jumps from Platform to Platform. To complete the Level the square has to touch a repainter. This repainter changes the color from that square to another. To complete the Level the square has to get a specific color. How do I check if the color from the square has this color(e.g. Green)?
danpost danpost

2015/11/23

#
If the square is completely filled with the one color, you can just get its image and grab the color of any pixel in its image. You should probably give information as to how the image of the square is colored (do you programmatically fill the image yourself or is it a predefined image saved in a file). I say this because the image must be filled with the color (0, 255, 0) precisely for the comparison to become true with:
if (Color.GREEN.equals(square.getImage().getColorAt(0, 0)))
Of course you could be more general about the color by comparing each color part so that you can determine the predominant color. For example:
Color color = square.getColorAt(0, 0);
if (color.getRed < 64 && color.getGreen() > 192 && color.getBlue() < 64)
Another way is to have the square actor hold its color value in a field and add a method to return that color for comparison
//          in class of square
// instance field
Color color; // must be set to a Color object when actor is created
// get method
public Color getColor()
{
    return color;
}

//          in other class
Square square = getOneIntersectingObject(Square.class);
if (square != null)
{
    Color color = square.getColor();
    // etc. (start by making the color comparison)
}
Ludmuterol Ludmuterol

2015/12/7

#
Color color = square.getColorAt(0, 0);
if (color.getRed < 64 && color.getGreen() > 192 && color.getBlue() < 64)
the getRed() and Blue and Green is not working... must i import something?
Ludmuterol Ludmuterol

2015/12/7

#
Ludmuterol wrote...
Color color = square.getColorAt(0, 0);
if (color.getRed < 64 && color.getGreen() > 192 && color.getBlue() < 64)
the getRed() and Blue and Green is not working... must i import something?
sorry for bad English... I'm german
danpost danpost

2015/12/7

#
Ludmuterol wrote...
must i import something?
You will either need to add the following at the top of the class:
import java.awt.Color;
or change line 1 to:
java.awt.Color color = square.getColorAt(0, 0);
You need to login to post a reply.