public void drawBrighterImage(Graphics2D g2d, BufferedImage im,
int x, int y, float brightness)
/* Draw the image with changed brightness, by using a RescaleOp.
Any alpha channel is unaffected. */
{
if (im == null) {
System.out.println("drawBrighterImage: input image is null");
return;
}
if (brightness < 0.0f) {
System.out.println("Brightness must be >= 0.0f; setting to 0.5f");
brightness = 0.5f;
}
// brightness may be less than 1.0 to make the image dimmer
RescaleOp brigherOp;
if (hasAlpha(im)) {
float[] scaleFactors = {brightness, brightness, brightness, 1.0f};
// don't change alpha
// without the 1.0f the RescaleOp fails, which is a bug (?)
float[] offsets = {0.0f, 0.0f, 0.0f, 0.0f};
brigherOp = new RescaleOp(scaleFactors, offsets, null);
}
else // not transparent
brigherOp = new RescaleOp(brightness, 0, null);
g2d.drawImage(im, brigherOp, x, y);
} // end of drawBrighterImage()
public void drawNegatedImage(Graphics2D g2d, BufferedImage im, int x, int y)
/* Draw the image with 255-<colour value> applied to its RGB components.
Any alpha channel is unaffected. */
{
if (im == null) {
System.out.println("drawNegatedImage: input image is null");
return;
}
if (hasAlpha(im))
g2d.drawImage(im, negOpTrans, x, y); // use predefined RescaleOp
else
g2d.drawImage(im, negOp, x, y);
}
public void drawFadedImage(Graphics2D g2d, BufferedImage im,
int x, int y, float alpha)
/* The degree of fading is specified with the alpha value.
alpha == 1 means fully visible, 0 mean invisible. */
{
if (im == null) {
System.out.println("drawFadedImage: input image is null");
return;
}
if (alpha < 0.0f) {
System.out.println("Alpha must be >= 0.0f; setting to 0.0f");
alpha = 0.0f;
}
else if (alpha > 1.0f) {
System.out.println("Alpha must be <= 1.0f; setting to 1.0f");
alpha = 1.0f;
}
Composite c = g2d.getComposite(); // backup the old composite
g2d.setComposite( AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(im, x, y, null);
g2d.setComposite(c);
// restore the old composite so it doesn't mess up future rendering
}