import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; /** * This class demonstrates how to load an Image from an external file */ public class LoadImageApp extends Component { BufferedImage img; public void paint(Graphics g) { g.drawImage(img, 0, 0, null); } public LoadImageApp() { try { img = ImageIO.read(new File("strawberry.jpg")); } catch (IOException e) { } for(int i = 0; i < 100; i = i + 1) for(int j = 0; j < 100; j = j + 1) img.setRGB(j+50, i+50, 0xFF0000); int width = img.getWidth(); int height = img.getHeight(); int color = img.getRGB(120, 120); int red = color & 0xFF0000 / (256*256); int green = color & 0x00FF00 / 256; int blue = color & 0x0000FF; System.out.println(color); System.out.println(red); System.out.println(green); System.out.println(blue); for(int x = 0; x < width; x = x + 1) { for(int y = 0; y < height; y = y + 1) { // assume we have "color" color = img.getRGB(x, y); red = color & 0xFF0000 / (256*256); green = color & 0x00FF00 / 256; blue = color & 0x0000FF; if(red > 128 && green < 128 && blue < 128) img.setRGB(x, y, 0x0000FF); } } } public Dimension getPreferredSize() { if (img == null) { return new Dimension(100,100); } else { return new Dimension(img.getWidth(null), img.getHeight(null)); } } public static void main(String[] args) { JFrame f = new JFrame("Load Image Sample"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(new LoadImageApp()); f.pack(); f.setVisible(true); } }