使用swing

时间:2019-03-01 12:27:22

标签: java swing

我创建了一个GUI,该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

的主界面中
 public class Interface {

    JFrame frame;

    /**
     * Create the application.
     * @throws IOException 
     */
    public Interface() throws IOException {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     * @throws IOException 
     */
    private void initialize() throws IOException {
        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);

        JButton openFileButton = new JButton("Open File");
        openFileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                 FileSelector FileSelector = new FileSelector();
                    FileSelector.SelectFile();
                    try {
                        FileSelector.createBufferedImage();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

            }
        });
        buttonPanel.add(openFileButton);

        JButton drawDigitButton = new JButton("Draw Digit");
        drawDigitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        buttonPanel.add(drawDigitButton);

        //kNN
        JPanel kNNPanel = new JPanel();
        frame.getContentPane().add(kNNPanel, BorderLayout.SOUTH);

        JButton kNNButton = new JButton("kNN");
        kNNButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                kNNCalculation kNN = new kNNCalculation();

                try {
                    kNN.Calculation();
                }
                catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
        });
        kNNPanel.add(kNNButton);


        //Display image here????

        JPanel displayPanel = new JPanel();
        displayPanel.setPreferredSize(new Dimension (150, 150));
        displayPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        frame.getContentPane().add(displayPanel, BorderLayout.CENTER);
    }

}

但是我不太了解如何以面向对象的方式完成此任务?

1 个答案:

答案 0 :(得分:1)

以下内容演示了如何绘制从另一个类检索的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;
    }
}