如何从 JFileChooser 放置图像? [英] How to put an image from a JFileChooser?

查看:27
本文介绍了如何从 JFileChooser 放置图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Graphics 将图像放入 jPanel 时遇到问题.我有下一个代码:

I have a problem using Graphics to put an image in a jPanel. I have the next code:

    JFileChooser navegador = new JFileChooser();
    if (navegador.showOpenDialog(null) == 0) {
        try {
            BufferedImage imagenAbrir = ImageIO.read(navegador.getSelectedFile());
            lienzo.paintComponents(imagenAbrir.getGraphics());
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Ocurriò un error al guardar la imàgen");
        }
    }

代码是按钮的一部分,用户可以从任何路径选择图像,但它不会在面板中显示任何内容.可能是什么问题?

The code is part of a button, the user could select an imagen from any path, but it doesn't show anything in the panel. What could be the problem?

推荐答案

我认为您必须使用 BufferedImage 对象而不是 Graphics 对象更改方法

I think you have to change you method with BufferedImage object not a Graphics object

@Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser navegador = new JFileChooser();
                if (navegador.showOpenDialog(null) == 0) {
                    try {
                        BufferedImage imagenAbrir = ImageIO.read(navegador.getSelectedFile());
                        //drawPan.paintComponents(imagenAbrir.getGraphics());
                        drawPan.drawImage(imagenAbrir);
                    } catch (IOException ie) {
                        JOptionPane.showMessageDialog(null, "Ocurriò un error al guardar la imàgen");
                    }
                }
            }

然后,如果要绘制图像,请自行创建自定义 JPanel 类.每次在代码中调用 repaint 方法时,都会自动调用 JComponent 的paintCompoent 方法.

Then, if you want to draw image, create customized JPanel class on your own. The paintCompoent method of JComponent will be invoked automatically, every time you call repaint method in your code.

final class ImagePane extends JPanel {

            private static final long serialVersionUID = 1L;
            private BufferedImage myImage;

            public ImagePane(final BufferedImage myImage) {
                this.myImage = myImage;
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(240, 220);
            }

            public void drawImage(BufferedImage img)
            {
                this.myImage = img;
                repaint();
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (myImage != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    int x = (getWidth() - myImage.getWidth()) / 2;
                    int y = (getHeight() - myImage.getHeight()) / 2;
                    g2d.drawImage(myImage, x, y, this);
                    g2d.dispose();
                }
            }

        }

这里是完整的源代码.

public class JFileChooserTest {

    static Runnable doRun = new Runnable()
    {
        final class ChooseAction implements ActionListener
        {
            final ImagePane drawPan;

            ChooseAction(final ImagePane drawPan)
            {
                this.drawPan = drawPan;
            }


            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser navegador = new JFileChooser();
                if (navegador.showOpenDialog(null) == 0) {
                    try {
                        BufferedImage imagenAbrir = ImageIO.read(navegador.getSelectedFile());
                        //drawPan.paintComponents(imagenAbrir.getGraphics());
                        drawPan.drawImage(imagenAbrir);
                    } catch (IOException ie) {
                        JOptionPane.showMessageDialog(null, "Ocurriò un error al guardar la imàgen");
                    }
                }
            }

        }

        final class ImagePane extends JPanel {

            private static final long serialVersionUID = 1L;
            private BufferedImage myImage;

            public ImagePane(final BufferedImage myImage) {
                this.myImage = myImage;
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(240, 220);
            }

            public void drawImage(BufferedImage img)
            {
                this.myImage = img;
                repaint();
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (myImage != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    int x = (getWidth() - myImage.getWidth()) / 2;
                    int y = (getHeight() - myImage.getHeight()) / 2;
                    g2d.drawImage(myImage, x, y, this);
                    g2d.dispose();
                }
            }

        }

        @Override
        public void run() {
            final JFrame frame = new JFrame();
            //frame.setSize(new Dimension(300,400));

            JPanel lienzo = new JPanel();
            lienzo.setLayout(new BorderLayout());

            ImagePane drawPan = new ImagePane(null);
            JButton drawMe = new JButton("draw me");
            lienzo.add(drawMe, BorderLayout.NORTH);
            lienzo.add(drawPan, BorderLayout.CENTER);

            frame.add(lienzo);

            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            drawMe.addActionListener(new ChooseAction(drawPan));
        }

    };

    public static void main(String[] args)
    {

        SwingUtilities.invokeLater(doRun);

    }
}

希望对你有所帮助.

这篇关于如何从 JFileChooser 放置图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆