/* Value Description 0x00000000 Black 0x00FF0000 Bright Red 0x0000FF00 Bright Green 0x000000FF Bright Blue 0x007F7F7F Gray 0x00FFFF00 Bright Yellow 0x00FF7F7F Red Pastel 0x00FFFFFF White */ import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class BufferedImageTest { static BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB); public static void draw_1() { /* fill the rectangle (0,0) to (300,300) with color 0xFFFFFFFF */ for (int x = 0; x < 300; x++) for (int y = 0; y < 300; y++) image.setRGB(x, y, 0xFFFFFFFF); } public static void draw_2() { /* set color of the diagonal from (0,0) to (299,299) to Green */ for (int x = 0; x < 300; x++) image.setRGB(x, x, 0xFF00FF00); /* set color of the rectangle from (10,20) to (199,40) to Black */ for (int x = 10; x < 200; x++) for (int y = 20; y < 40; y++) image.setRGB(x, y, 0x00000000); /* set color of the diagonal from (0,299) to (299,0) to Red */ for (int x = 299; x > 0; x--) image.setRGB(x, 299-x, 0x00FF0000); } public static void main(String[] args) { try { JFrame frame = new JFrame("Buffered Image - Set Pixel"); draw_1(); draw_2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel(new ImageIcon(image))); frame.pack(); frame.setVisible(true); } catch (Exception e) {e.printStackTrace();} } }