Java examples for 2D Graphics:BufferedImage Color
invert Black And White BufferedImage
//package com.java2s; import java.awt.Color; import java.awt.image.BufferedImage; public class Main { public static BufferedImage invertBlackAndWhite(BufferedImage image) { BufferedImage imageOut = getBlackImage(image.getWidth(), image.getHeight());//from ww w .j a v a 2 s . com for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { Color c = new Color(image.getRGB(i, j)); //invert white into black if (c.equals(Color.white)) { imageOut.setRGB(i, j, Color.black.getRGB()); } //invert black into white else if (c.equals(Color.black)) { imageOut.setRGB(i, j, Color.white.getRGB()); } //copy the other colors else { imageOut.setRGB(i, j, image.getRGB(i, j)); } } } return imageOut; } public static BufferedImage getBlackImage(int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int i = 0; i < width - 1; i++) { for (int j = 0; j < height - 1; j++) { image.setRGB(i, j, Color.black.getRGB()); } } return image; } }