Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
You can directly access the pixels of a Graphics object using the getRGB() and setRGB() methods.
Example with getRGB() and setRGB() (Run Applet)
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class GetAndSetPixelDemo extends JApplet
{
@Override
public void init()
{
this.setContentPane(new View());
}
public class View extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
final BufferedImage bImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = bImg.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawString("setRGB() and getRGB() demo", 50, 30);
int y;
// write some pixels to red
for (y = 50; y < 100; y++)
{
bImg.setRGB(50, y, (Color.RED).getRGB()); // we shall change this colour in a moment
// set some extra pixels to show that the pixels do get set to red
bImg.setRGB(60, y, (Color.RED).getRGB()); // we do not change this colour
}
// read some pixels.
// change them to black if they are currently red
for (y = 50; y < 100; y++)
{
if (bImg.getRGB(50, y) == (Color.RED).getRGB()) // read the pixels colour
{
bImg.setRGB(50, y, (Color.BLACK).getRGB()); // change the colour if it matches
}
// compare some pixels that are not red to show that the if statement works
if (bImg.getRGB(40, y) == (Color.RED).getRGB()) // read the pixels colour
{
bImg.setRGB(40, y, (Color.BLACK).getRGB()); // change the colour if it matches
}
}
g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);
}
}
}
The getRGB() and setRGB() methods store colour details as a 32 bit integer number, where bits 24-31 are alpha, 16-23 are red, 8-15 are green and 0-7 are blue. The example below shows how we can directly minipulate the alpha, red, green and blue components of a an images pixels.
Manipulating an Image's pixels example (Run Applet)
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ManipulateImagePixelsDemo extends JApplet
{
@Override
public void init()
{
setContentPane(new View());
}
public class View extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
// display two images over the total space of the applet's panel
final Image gif = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
final int gap = 10; // gap between the two displayed images
final int startX = 5;
final int startY = 5;
final int width = (this.getWidth() - 20) / 2;
final int height = (this.getHeight() - 10);
final BufferedImage bImg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = bImg.createGraphics();
g2d.setColor(Color.BLACK);
g2d.drawImage(gif, startX, startY, width, height, this);
for (int x = startX; x < startX + width; x++)
{
for (int y = startY; y < startY + height; y++)
{
int pixelColour;
int alpha;
int red;
int green;
int blue;
// get the pixel's colour as an integer
pixelColour = bImg.getRGB(x, y);
// extract the alpha, red, green and blue colour data from the pixel's RGB colour
alpha = (pixelColour >> 24) & 0xff;
red = (pixelColour >> 16) & 0xff;
green = (pixelColour >> 8) & 0xff;
blue = (pixelColour) & 0xff;
// manipulate the pixel by writting back only the blue component of the RGB colour
bImg.setRGB(x, y, new Color(0, 0, blue, alpha).getRGB());
// manipulate the pixel by adjusting its alpha value
bImg.setRGB(x + width + gap, y, new Color(red, green, blue, alpha / 4).getRGB());
}
}
// draw to the graphic g
g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);
}
}
}
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.