1

I know that everywhere there is a tutoring in how to draw an image. Usually people suggest showing it adding an object that loads that image. But in my case, I don't wanna instantiate a new object every time that I change something in the image.

So, I'm using the Graphics class to do it. Also, I'm doing it using the MVC approach.

Problem: As we see, there is only a small region of the image that is drawn, if I load another image, this small regions changes according to the picture. Then, I presume that the Buffered image is been load correctly.

So, I looking for where would be the problem: This is my code:

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class DisplayView extends JFrame implements Observer {

    private static final long serialVersionUID = 1L;
    /**
     * @param args
     */
    private static DisplayView instance;
    private DisplayControl control;
    private JFileChooser fileChooser;

    Panel imageLeft, imageRight;

    private DisplayView() {

        JMenuItem exit = new JMenuItem("Exit");
        exit.setMnemonic('E');
        exit.setToolTipText("Exit Application");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new ImageFileFilter());

        JMenuItem loadImage = new JMenuItem("Load Image");
        loadImage.setMnemonic('O');
        loadImage.setToolTipText("Loads an Image to Process");
        loadImage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                int ret = fileChooser.showDialog(DisplayView.getInstance(),
                        "Open file");

                if (ret == JFileChooser.APPROVE_OPTION) {
                    System.out.println(fileChooser.getSelectedFile());
                    control.onFileChoose(fileChooser.getSelectedFile()
                            .getAbsolutePath());
                }
            }
        });

        JMenu file = new JMenu("File");
        file.setMnemonic('F');
        file.add(loadImage);
        file.add(exit);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(file);

        imageLeft = new Panel();
        imageLeft.setSize(500, 500);
        imageLeft.setVisible(true);

        imageRight = new Panel();

        this.setLayout(new FlowLayout());
        this.add(imageLeft);
        // this.add(imageRight);

        this.setTitle("Test");
        this.setSize(300, 200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setJMenuBar(menuBar);
    }

    static public DisplayView getInstance() {
        if (instance == null)
            instance = new DisplayView();
        return DisplayView.instance;
    }

    public void setControl(DisplayControl control) {
        this.control = control;
    }

    @Override
    public void update(Observable o, Object arg) {
        // TODO Auto-generated method stub
        if (o instanceof DisplayModel) {
            this.imageLeft.setImage(((DisplayModel) o).getOriginalImage());
            // this.imageRight.setImage(((DisplayModel) o).getProcessedImage());

        }
    }

}

class Panel extends JPanel {

    BufferedImage image;

    public void setImage(BufferedImage image) {
        if (image != null)
            this.image = image;
        this.repaint();
    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        if (image != null)
            g.drawImage(image, 0, 0, this);
    }
}
4

1 回答 1

1

问题是您的Panel类没有覆盖getPreferredSize(),因此它的首选大小实际上是 (0,0),FlowLayout因此将分配 (0,0) 的大小给您的Panel.

无论如何,我会考虑用Panelsimple JLabel's 替换您的类,这将做完全相同的事情并为您处理首选尺寸。

  • setSize()当您还使用 LayoutManager(您应该这样做)时,调用是无用的。一般来说,只需忘记setSize/setLocation/setBounds/setPreferredSize. 答案总是一样的:“使用适当的LayoutManager
  • 对于自定义绘画,覆盖paintComponent而不是paint
于 2013-04-24T15:02:14.083 回答