Simple, very straight forward but seems uncle google and me getting confused.
I have single JLabel
that already has its own Icon
.
How do I convert the Icon
obtained from JLabel
into a BufferedImage
?
Is there any way around: I tried to multiple casting like this ..
final BufferedImage bf1 = (BufferedImage)((Image)jll_img.getIcon());
..but it failed.
To amplify on @Andrew Thompson's answer, note that an object that implements the Icon
interface knows how to paint something, but it may not have been asked to do so yet. In contrast, a BufferedImage has "an accessible buffer of image data" that your program must render by calling the paintIcon()
method. Here's a related example.
This is the most simpliest way:
labelPicture = new JLabel("Picture");
labelPicture.setIcon(new ImageIcon("picture.png"));
.
.
ImageIcon icon = (ImageIcon)labelPicture.getIcon()
BufferedImage img = (BufferedImage)((Image) icon.getImage());
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.net.URL;
class GetImage {
public static void main(String[] args) throws Exception {
final String text = "Mt Stromlo at sunset";
final URL url = new URL(
"http://i.stack.imgur.com/lxthA.jpg");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ImageIcon imageIcon = new ImageIcon(url);
JLabel imageLabel = new JLabel(
text,
imageIcon,
SwingConstants.CENTER);
JOptionPane.showMessageDialog(null, imageLabel);
Icon icon = imageLabel.getIcon();
BufferedImage bi = new BufferedImage(
icon.getIconWidth(),
icon.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.setColor(Color.WHITE);
g.drawString(text,10,20);
g.dispose();
JOptionPane.showMessageDialog(
null,
new JLabel(new ImageIcon(bi)));
}
});
}
}
Here is some older code from the Sun forums that deals with images that are created in memory.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import javax.swing.*;
class ImageCacheTest {
private JLabel imageLabel;
private Dimension halfScreenSize;
private Random random;
private JProgressBar memory;
private Font bigFont = new Font("Arial", Font.BOLD, 30);
private int count = 0;
private int startMem = 0;
private int maxMem = 0;
private int peakMem = 0;
private int useMem = 0;
ImageCacheTest() {
startMem = ((int) Runtime.getRuntime().freeMemory());
maxMem = ((int) Runtime.getRuntime().freeMemory());
peakMem = ((int) Runtime.getRuntime().freeMemory());
JPanel p = new JPanel(new BorderLayout(4, 4));
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
halfScreenSize = new Dimension(d.width / 2, d.height / 2);
//halfScreenSize = new Dimension(d.width - 11, d.height - 51);
random = new Random();
imageLabel = new JLabel(new ImageIcon(convertToFromBytes(getImage())));
memory = new JProgressBar(0, (int) Runtime.getRuntime().maxMemory());
p.add(imageLabel, BorderLayout.CENTER);
p.add(memory, BorderLayout.SOUTH);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.setLocation(150, 150);
f.pack();
f.setVisible(true);
Runnable r = new Runnable() {
@Override
public void run() {
while (true) {
try {
imageLabel.setIcon(new ImageIcon(convertToFromBytes(getImage())));
memory.setValue((int) Runtime.getRuntime().freeMemory());
useMem = ((int) Runtime.getRuntime().freeMemory());
Thread.sleep(30);
} catch (InterruptedException ex) {
//something exceptio
}finally{
// alive that if required
}
}
}
};
Thread t = new Thread(r);
t.start();
}
private BufferedImage getImage() {
GradientPaint gp = new GradientPaint(0f, 0f, new Color(127 + random.nextInt(128), 127 + random.nextInt(128), 127 + random.nextInt(128)),
(float) halfScreenSize.width, (float) halfScreenSize.width, new Color(random.nextInt(128), random.nextInt(128), random.nextInt(128)));
BufferedImage bi = new BufferedImage(halfScreenSize.width, halfScreenSize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(gp);
g2d.fillRect(0, 0, halfScreenSize.width, halfScreenSize.height);
g2d.setFont(bigFont);
g2d.setColor(Color.BLACK);
if (maxMem < ((int) Runtime.getRuntime().freeMemory())) {
maxMem = ((int) Runtime.getRuntime().freeMemory());
}
if (peakMem > ((int) Runtime.getRuntime().freeMemory())) {
peakMem = ((int) Runtime.getRuntime().freeMemory());
}
useMem = ((int) Runtime.getRuntime().freeMemory()) - useMem;
g2d.drawString("" + ++count, 20, 100);
g2d.drawString("JVM memory status ---> ", 20, 195);
g2d.drawString("tot. memory ---> " + ((int) Runtime.getRuntime().totalMemory()), 20, 240);
g2d.drawString("max. memory ---> " + ((int) Runtime.getRuntime().maxMemory()), 20, 270);
g2d.drawString("free on startUp ---> " + startMem, 20, 300);
g2d.drawString("max free memory ---> " + maxMem, 20, 350);
g2d.drawString("min free memory ---> " + peakMem, 20, 380);
g2d.drawString("act free memory ---> " + ((int) Runtime.getRuntime().freeMemory()), 20, 410);
g2d.drawString("usage of memory ---> " + useMem, 20, 450);
g2d.dispose();
return bi;
}
/** Not entirely sure this method is necessary for indicating 'no cache',
but since the claim was specific to byte arrays, we'll do it. */
private Image convertToFromBytes(BufferedImage image) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ImageCacheTest ict = new ImageCacheTest();
}
};
SwingUtilities.invokeLater(r);
}
}
Screenshot of that code with width dropped to ¼ screen width.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With