JPanel doesn't refresh every time when I load an image from folder

51 views Asked by At

I meet a problem. I load an image from a folder and it can be shown the first time in JPanel. But when I try other times, the JPanel doesn't refresh. It always keeps the first image. Really appreciate any help! Thanks!

package com.user_GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.*;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;

/* ResizableComponent.java */

public class TrySomething extends JFrame implements ActionListener {

private JPanel panel = new JPanel(null);
private Resizable resizer;

BufferedImage image = null;
JMenuBar menubar;
JMenu menu;
JMenuItem jitem;
String filePath = null;
String imageName = null;
String parentPath = null;

public TrySomething() throws Exception {

    setLayout(new BorderLayout());

    add(panel, BorderLayout.CENTER);

    menubar = new JMenuBar();
    menu = new JMenu("File");
    jitem = new JMenuItem("Open File...");

    // setLayout(new FlowLayout());
    //this.setJMenuBar(menubar);
    this.add(menubar, BorderLayout.NORTH);

    menubar.add(menu);
    menu.add(jitem);

    jitem.addActionListener(this);

    JPanel area = new JPanel();
    // area.setBackground(getBackground());
    // area.setBackground(null);
    area.setOpaque(false);
    resizer = new Resizable(area);
    resizer.setBounds(50, 50, 200, 150);
    //panel.setOpaque(false);//very important!
    panel.add(resizer);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(350, 300));
    setTitle("Resizable Component");
    setLocationRelativeTo(null);

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me) {

            requestFocus();
            resizer.repaint();
        }
    });
}

public static void main(String[] args) throws Exception {
    TrySomething ts = new TrySomething();
    ts.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    JFileChooser fileChooser = new JFileChooser();

    int status = fileChooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();

        System.out.println(selectedFile.getPath());
        System.out.println(selectedFile.getName());
        System.out.println(selectedFile.getParent());
        filePath = selectedFile.getPath();
        imageName = selectedFile.getName();
        parentPath = selectedFile.getParent();

        try {
            showImage(filePath, imageName, parentPath);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    } else if (status == JFileChooser.CANCEL_OPTION) {
        System.out.println("calceled");

    }

}

public void showImage(String filePath, String imageName, String parentPath)
        throws Exception {
    String[] strings = imageName.split("\\.");

    String input2 = filePath;
    String output2 = parentPath + "/" + strings[0] + ".jpg";

    RenderedOp src2 = JAI.create("fileload", input2);
    OutputStream os2 = null;
    os2 = new FileOutputStream(output2);
    JPEGEncodeParam param2 = new JPEGEncodeParam();
    ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG", os2, param2);
    enc2.encode(src2);
    os2.close();



//      JPanel imagePanel = (JPanel) this.getContentPane();
//      imagePanel.setOpaque(false);
    JLabel label = new JLabel();

    ImageIcon icon = new ImageIcon(output2);

    System.out.println(output2);

    label.setIcon(icon);
    label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());

    System.out.println(label.getIcon().getIconHeight());


    //panel.updateUI();

    Thread.sleep(1000);
    //this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
    panel.add(label);

    Thread.sleep(1000);
    panel.repaint();
    panel.revalidate();

}

}
0

There are 0 answers