0
votes

My project is Open an image and Save it again, I use ImageIcon,JLabel and JPanel to display it (I used ImageIcon and JPanel, but It didn't work, ImageIcon could not add to JPanel). when I open it, It always display the image but not full size to JFrame. this code I write in class OpenImage extends JFrame

public class Draw_JPanel extends JFrame{
       Load_image panel_im = new Load_image();
public void OpenImage()
{   
    JFileChooser fc = new JFileChooser();
    int result = fc.showOpenDialog(null);
    if(result == JFileChooser.APPROVE_OPTION)
    {
        File file = fc.getSelectedFile();
        String name = file.getName();
        try {
            image = ImageIO.read(file);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        imageic = new ImageIcon(image);
        height1 = imageic.getIconHeight();
        width1 = imageic.getIconWidth();
         picLabel = new JLabel(new ImageIcon(image));
         panel_im.add(picLabel,BorderLayout.CENTER);
    }
    this.pack();

}

and the code of Load_image class

public class Load_image extends JPanel{
public Load_image()
{   
    this.setBackground(Color.RED);
}
}

Sorry but I can't upload image

2
you see full size of your image? or your try to resize your image to fill all JFrame area?alex2410
I try to resize my image size to JFrameĐăng Nguyễn
I think your image has fixed size, because of it your Label can't resize it, if you want to resize image try to read it and that or find somthing elsealex2410
my projectis display and save it against, but if I resize image to JFrame, when I save it, its size will be change. how can I pack an imageIcon to JLabel and then pack to JPanel ?Đăng Nguyễn

2 Answers

0
votes

I wrote a simple example for you. It draws image and resizes it with help of imgscalr library. Also you can save image. Try it, I think it helps you.

public class Example extends JPanel{

    private static BufferedImage scaledImg;

    public static void main(String[] args) {
        Example e = new Example();
        JFrame f = new JFrame();
        f.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        f.add(e,c);

        c.fill = GridBagConstraints.NONE;
        c.weightx = 0;
        c.weighty = 0;
        c.gridx = 1;
        JButton b = new JButton("save");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                File outputfile = new File("c:\\image.png");
                try {
                    ImageIO.write(scaledImg, "png", outputfile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        f.add(b,c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics arg0) {
        super.paintComponent(arg0);
        Graphics2D g = (Graphics2D) arg0;
        BufferedImage img = null;
        try {
            img = ImageIO.read( Example.class.getResource("/res/3_disc.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        scaledImg = Scalr.resize(img,getSize().height,getSize().width,new BufferedImageOp[]{});
        g.drawImage (scaledImg, null, 0, 0);
    }

}

here "/res/3_disc.png" is image in my project

"c:\\image.png" output for image

0
votes

I fixed my code, I add setBounds() method and setLayout() method, here my code after fixed:

setLayout(null);
        imageic = new ImageIcon(image);
        height1 = imageic.getIconHeight();
        width1 = imageic.getIconWidth();
        picLabel = new JLabel(new ImageIcon(image));
        picLabel.setSize(width1, height1);
        picLabel.setBounds(0, 0, width1, height1);
        panel_im.setSize(width1, height1);
        panel_im.setBounds(-5, -5, width1, height1);
        panel_im.add(picLabel);
        this.pack();

tks for all