Search code examples
javaimageswingbufferedimageimageicon

export imageIcon as buffered imaged


My java code below right now displays a imageIcon in a label and a button. When the button is pressed, It draws a buffered image and exports that image. The image that is exported has nothing to do with the image in the image icon.

Instead of drawing a image I want the image in the ImageIcon exported just like how the image is drawn and exported. So I think the image in the image Icon has to be converted into a buffered image and then export in a 400 width and 400 height image.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.*;  

public class second {  
JFrame f;  
second() throws IOException{ 

//use paintCompent 


    f=new JFrame();  
    JButton b1 = new JButton("Action Listener");

    JLabel b2=new JLabel("");;  


    b2.setIcon(new ImageIcon(new ImageIcon("/Users/johnzalubski/Desktop/javaCode/cd.jpg").getImage().getScaledInstance(400, 400, Image.SCALE_DEFAULT)));





    f.add(b1,BorderLayout.NORTH);  
    f.add(b2,BorderLayout.CENTER);  



    b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int width = 300;
            int height = 300;


            BufferedImage  buffIMg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            Graphics2D g2d = buffIMg.createGraphics();

            g2d.setColor(Color.white);
            g2d.fillRect(0, 0, width, height);
            g2d.setColor(Color.black);
            g2d.fillOval(0,0,width,height);
            g2d.setColor(Color.orange);
            g2d.drawString("Jessica ALba:", 55, 111);


            g2d.dispose();


            File file = new File("aa.png");

            try {
                ImageIO.write(buffIMg, "png",file);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });


    f.setSize(400,400);  
    f.setVisible(true);  
}  
    public static void main(String[] args) throws IOException {  
    new second();  


    }  



}  

Solution

  • In addition to a write method, ImageIO also has a read method. Use that to load your image, instead of using ImageIcon has an image loader:

    BufferedImage unscaledButtonImage;
    try {
        unscaledButtonImage = ImageIO.read(
            new File("/Users/johnzalubski/Desktop/javaCode/cd.jpg"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    

    To scale it, using a scaling Graphics.drawImage method:

    BufferedImage scaledButtonImage =
        new BufferedImage(400, 400, unscaledButtonImage.getType());
    Graphics g = scaledButtonImage.createGraphics();
    g.drawImage(unscaledButtonImage, 0, 0, 400, 400, null);
    g.dispose();
    

    You can make an ImageIcon from it easily:

    b2.setIcon(new ImageIcon(scaledButtonImage));
    

    But you don’t need the ImageIcon after that, because you have the original BufferedImage.