Manipulating color and pixel in Java


If you are not familiar with BookClass basics. It is highly recommended to read the following tutorials before starting picture manipulation.
Customize and use BookClasses in eclipse
Manipulating Pictures in Java- I
Manipulating Pictures in Java- II

Let’s have a look at pixel, ColorChooser and color class methods before proceeding with color value manipulation.

Pixel class methods :

Methods Description
getColor() This method returns color object
getred(), getGreen(), getBlue() Each method returns color value between 0 to 255.
getX(), getY() Returns pixel position (x and y) of where this pixel is in that picture
setColor(Color color) This method takes Color object and set it for pixel
setRed(int value), setGreen(int value), setBlue(int value) Each method takes a value between 0 to 255 to set value of a pixel
int correctValue(int value) Correct color value.. must be 0 to 255
updatePicture(int alpha, int red, int green, int blue) Update picture based on color value for this pixel
setAlpha(int value) set the alpha (transparency) to a new alpha value
double colorDistance(Color testColor) get the distance between this pixel’s color and the passed color

Color class methods :

Method Description
new Color(int red,int green, int blue) Take three values as red, green and blue. create color object and return it as Color object
darker(),brighter() returns slightly darker or brigher color object

FileChooser class method

Method Description
ColorChooser.pickAColor() Displays a window to choose some colors

Here is an example how to manipulate pixel. Find decreaseRed() method in following code. The method retrieves all pixel and iterate through each pixel, retrieves each red value of a pixel and change red by multiplying each value with 0.6. We are calling an additional method repaint() to update changes.

Left side is original picture and on right side after calling method decreaseRed()

Left side is original picture and on right side after calling method decreaseRed()