Okay, I kinda found a way but it’s way to slow to use it…
First I get the outline of my BufferedImage
/**
* @see http://stackoverflow.com/questions/18591749/create-java-awt-geom-area-from-points
*/
public static Area getOutline(BufferedImage image, Color color, boolean include, int tolerance) {
Area area = new Area();
for (int x=0; x<image.getWidth(); x++) {
for (int y=0; y<image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x,y));
if (include) {
if (isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
} else {
if (!isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
}
}
}
return area;
}
private static boolean isIncluded(Color target, Color pixel, int tolerance) {
int rT = target.getRed();
int gT = target.getGreen();
int bT = target.getBlue();
int rP = pixel.getRed();
int gP = pixel.getGreen();
int bP = pixel.getBlue();
return(
(rP-tolerance<=rT) && (rT<=rP+tolerance) &&
(gP-tolerance<=gT) && (gT<=gP+tolerance) &&
(bP-tolerance<=bT) && (bT<=bP+tolerance) );
}
From that I create an image I can draw:
public BufferedImage getImageWithGradientColor(BufferedImage original, Color color) {
area = (area == null) ? Images.getOutline(original, color, true, 10) : area;
final BufferedImage result = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = result.createGraphics();
g.setClip(area);
g.setPaint(
new GradientPaint( 0, 0, Images.alphaColor(Color.WHITE, 0),
0, (int)(original.getHeight()*(gradientHeight)), Color.WHITE)
);
g.fillRect(0,0,original.getHeight(),original.getWidth());
g.draw(area);
return result;
}
I think creating a new BufferedImage every frame is slowing it down. Is there a better/cleaner way to do so?