View Single Post
  #2 (permalink)  
Old 01-24-2008, 02:00 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Try an image type based on your local configuration.
On ms vista I get:
Code:
C:\jexp>java ImageTypes loaded image type = 5 compatible dst type = 1
Code:
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImageTypes { private JScrollPane getContent(BufferedImage image) { System.out.println("loaded image type = " + image.getType()); JPanel panel = new JPanel(new GridLayout(1,0)); panel.add(wrap(image)); panel.add(wrap(convert(image))); panel.add(wrap(convert(image, BufferedImage.TYPE_INT_RGB))); return new JScrollPane(panel); } private BufferedImage convert(BufferedImage src) { int w = src.getWidth(); int h = src.getHeight(); BufferedImage dst = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration() .createCompatibleImage(w, h); // Component has a getGraphicsConfiguration method. System.out.println("compatible dst type = " + dst.getType()); Graphics2D g2 = dst.createGraphics(); g2.drawImage(src,0,0,null); g2.dispose(); return dst; } private BufferedImage convert(BufferedImage src, int type) { int w = src.getWidth(); int h = src.getHeight(); BufferedImage dst = new BufferedImage(w, h, type); Graphics2D g2 = dst.createGraphics(); g2.drawImage(src,0,0,null); g2.dispose(); return dst; } private JLabel wrap(BufferedImage image) { JLabel label = new JLabel(new ImageIcon(image)); label.setHorizontalAlignment(JLabel.CENTER); return label; } public static void main(String[] args) throws IOException { String path = "images/blackBear.jpg"; BufferedImage image = ImageIO.read(new File(path)); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new ImageTypes().getContent(image)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Reply With Quote