Question:

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


Best Answer:


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