使用 swing 显示另一个类的 BufferedImage

我创建了一个 GUI,它使用以下类打开文件选择器:


 public class FileSelector {

    File fp;

    BufferedImage selectedFile;


    public void SelectFile() {

        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

        jfc.setDialogTitle("Select an image");

        jfc.setAcceptAllFileFilterUsed(false);

        FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG and jpeg images", "png", "jpg", "jpeg");

        jfc.addChoosableFileFilter(filter);


        int returnValue = jfc.showOpenDialog(null);

        if (returnValue == JFileChooser.APPROVE_OPTION) {

            fp = jfc.getSelectedFile();

            System.out.println(fp);


        }


    }


    public BufferedImage createBufferedImage() throws IOException {

        ImageFileHandler img_handler = new ImageFileHandler();


        if (fp.isFile() && fp.exists()) {

            selectedFile = ImageIO.read(fp);

            System.out.println(selectedFile);

        }


        BufferedImage bimage = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB);


        // Draw the image on to the buffered image

        Graphics2D bGr = bimage.createGraphics();

        bGr.drawImage(selectedFile, 0, 0, null);

        System.out.println(bimage);

        bGr.dispose();


        JFrame frame = new JFrame("Image from Desktop");

        JLabel picLabel = new JLabel(new ImageIcon(bimage));


        JPanel jPanel = new JPanel();

        jPanel.add(picLabel);

        frame.setSize(new Dimension(400, 300));

        frame.add(jPanel);

        frame.setVisible(true);

        return bimage;

    }

}

然后我有另一个类使用swing 来构建GUI。当前,所选图像在单独的 JFrame 中打开。我希望图像显示在主界面中displayPanel


Helenr
浏览 137回答 1
1回答

慕神8447489

下面演示如何绘制BufferedImage从另一个类中检索到的。它也可以用作MCVE的示例。MCVE 应该演示问题,而不是您的应用程序,并且不依赖于不可用的资源。以下代码可以复制粘贴到一个文件(Interface.java)中并运行:import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import javax.imageio.ImageIO;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import javax.swing.border.EtchedBorder;public class Interface {    private JFrame frame;    public Interface(){        initialize();    }    private void initialize() {        frame = new JFrame();        frame.setBounds(100, 100, 450, 300);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Button panel with open file and draw digit buttons        JPanel buttonPanel = new JPanel();        frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);        DrawPanel displayPanel = new DrawPanel();        JButton openFileButton = new JButton("Open File");        openFileButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                BufferedImage image = new FileSelector().createBufferedImage();                displayPanel.setImage(image);                frame.pack();            }        });        buttonPanel.add(openFileButton);        frame.getContentPane().add(displayPanel, BorderLayout.CENTER);        frame.setVisible(true);    }    public static void main(String[] args) {        SwingUtilities.invokeLater(()-> new Interface());    }}class DrawPanel extends JPanel{    private BufferedImage image;    public DrawPanel() {        setPreferredSize(new Dimension (150, 150));        setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));    }    @Override    public void paintComponent(Graphics g){        super.paintComponent(g);        if(image != null) {            g.drawImage(image, 0,0, null);        }    }    void setImage(BufferedImage image) {        this.image = image;        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));    }}class FileSelector {    public BufferedImage createBufferedImage(){        try {            URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");            return ImageIO.read(url);        } catch ( IOException ex) { ex.printStackTrace();}        return null;    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java