Friday, October 19, 2012

Example to display BufferedImage as ImageIcon

BufferedImage as ImageIcon


package javaeximage;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaTestImage {

static JFrameWin jFrameWindow;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(runJFrameLater);
}

public static class JFrameWin extends JFrame{

public JFrameWin(){
this.setTitle("java-buddy.blogspot.com");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
} catch (IOException ex) {
Logger.getLogger(JavaTestImage.class.getName()).log(Level.SEVERE, null, ex);
}

JLabel jLabel = new JLabel(new ImageIcon(bufferedImage));

JPanel jPanel = new JPanel();
jPanel.add(jLabel);
this.add(jPanel);
}

}

static Runnable runJFrameLater = new Runnable() {

@Override
public void run() {
jFrameWindow = new JFrameWin();
jFrameWindow.setVisible(true);
}

};

}


Related post:
- Example to draw BufferedImage in custom JComponent

No comments:

Post a Comment