开发者

Show Image selected from JFileChooser in a set sized JPanel (image must be re-scaled)

I am trying to fit an image in a set sized JPanel (picture panel--- black border line) (MUST FIT THE JPANEl "Picture panel"). When i click the upload button, i am able to see the JFilechooser in a new JFrame and select the picture i want, however after the 'open' button click nothing happens.

import classes.BackgroundPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileNameExtensionFilter;


public class Test
{

    public static void main(String[] args) {
        final JFileChooser chooser = new JFileChooser();
        JButton button = new JButton();
        button.setText("Upload");
        JFrame frame = new JFrame("My Frame");
        final JFrame imageFrame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFileChooser fc = new JFileChooser();
        final Test_Image t = new Test_Image();
        JPanel panel = new JPanel();
        JPanel picturePanel = new JPanel();
     //  chooser.showOpenDialog(null);
          Dimension d = new Dimension(1261, 765);
          Dimension d2 = new Dimension(1300, 900);
          picturePanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
          Dimension d3 = new Dimension(343, 247);
          picturePanel.setSize(d3);
     开发者_开发知识库       //picturePanel.setSize(d);
          panel.add(button);
          panel.setSize(d3);


        //panel.setVisible(true);
       //panel.add(picturePanel);

       button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                if(chooser.showOpenDialog(imageFrame) == JFileChooser.APPROVE_OPTION) {

                    try {
                        Image bi = ImageIO.read(
                        chooser.getSelectedFile());
                        BackgroundPanel bp = new BackgroundPanel(bi);
                        if (bi != null)
                        bp.setImage(bi);
                        else
                        JOptionPane.showMessageDialog(imageFrame,
                        "File is not an image!");
                        } catch (IOException ioe) {
                        JOptionPane.showMessageDialog(imageFrame,
                        "Error Reading File!");
}                
                }                              

            }
        });      


      frame.setSize(d2);
      frame.add(picturePanel).setLocation(100, 100);
      frame.add(panel);
      frame.setVisible(true);

    }

}


BackgroundPanel bp = new BackgroundPanel(bi); 

You don't add the BackgroundPane to the frame anywhere. The basic code for dynamically adding components is:

panel.add( someComponent );
panel.revalidate();
panel.repaint();

If you have already added the BackgroundPanel to the frame then you should be able to just invoke the setImage() method on the panel. So now you need to change your code to use ImageIO.read(...) to read in the image after you've selected the path from the file chooser.


Again, I don't see anywhere where you add the BackgroundPanel to the picturePanel JPanel. You will need to set the picturePanel's layout to BorderLayout and then add the BackgroundPanel, bp to the picturePanel in the BorderLayout.CENTER position, and then call revalidate and repaint as camickr shows you.

We also don't see the code for your BackgroundPanel class even though we've requested it. Again, I hope that you're drawing the image in its paintComponent method and using the proper drawImage overload, but til you show the code, we won't know for sure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜