Wednesday, April 6, 2011

Java Display Image Gray

JButton  photoBtn = new JButton();
File file = new File("C:/photos/myImage.jpg");
BufferedImage myImage = ImageIO.read(file);
BufferedImage imageGray = new BufferedImage(myImage.getWidth(), myImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = imageGray.getGraphics();
g.drawImage(myImage, 0, 0, 200, 200, null);
g.dispose();
photoBtn.setIcon(new ImageIcon(imageGray));
photoBtn.revalidate();

The above code display colored image as gray in button as icon.

No comments:

Post a Comment