Eduardo Reis
Eduardo Reis

Reputation: 1971

Java Draw Image JPanel: Working but weird

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);
    }
}

Upvotes: 1

Views: 1398

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47608

The problem is that your Panel class does not override getPreferredSize() and so its preferred size is actually (0,0) and the FlowLayout will therefore allocate a size of (0,0) to your Panel.

Anyway, I would consider replacing your Panel class by simple JLabel's which will do the exact same thing and handle the preferred size for you.

  • Calling setSize() is useless when you also use LayoutManager's (which you should). In general, simply forget about setSize/setLocation/setBounds/setPreferredSize. The answer is always the same: "Use an appropriate LayoutManager"
  • For custom painting, override paintComponent rather than paint

Upvotes: 1

Related Questions