** I was building a program that is supposed to paint an image in a JPanel. I should be able to select an image (such as a JPEG) from the Open item in the File drop down menu and then it should be visible in a new window. However, the images are not displaying. Please tell me if you can find that mistake in my code, I haven't found it after substantial searching.

**

    import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

public class GUI extends JFrame implements ActionListener {

    private JMenuItem item1 = new JMenuItem("Open");
    private JMenuItem item2 = new JMenuItem("New Window");
    private JMenuItem item3 = new JMenuItem("Quit");

    public GUI(BufferedImage imageData, String title)
    {

        JFrame F = new JFrame(title);
        JMenuBar menubar = new JMenuBar();
        JMenu menu1 = new JMenu ("File");

        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);

        menu1.add(item1);
        menu1.add(item2);
        menu1.add(item3);   

        menubar.add(menu1);

        F.setJMenuBar(menubar);
        F.setSize(300, 100);
        F.setVisible(true);

        if(imageData != null){
            F.add(new Draw(imageData));
        }
    }

    class Draw extends JPanel {
        BufferedImage graphic = null;
        public Draw(BufferedImage imgIn) {
            setBorder(BorderFactory.createLineBorder(Color.black));
            graphic = imgIn;
        }

        public Dimension getPreferredSize() {
            return new Dimension(250,200);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            g = graphic.getGraphics();
            g.drawImage(graphic,0,0,null);
        }   
    }
    public void actionPerformed(ActionEvent E)
    {
        if(E.getSource() == item1){
            JFileChooser F = new JFileChooser("");
            F.setFileSelectionMode(JFileChooser.FILES_ONLY);
            F.showOpenDialog(null);
            File file = F.getSelectedFile();
            createWindow(imageCopy(file.getPath()),file.getPath());
        }
        if(E.getSource() == item2){
            createWindow();
        }
        if(E.getSource() == item3){
            System.exit(0);
        }
    }
    //create a new blank window
    public static void createWindow(BufferedImage imageData){
        new GUI(imageData,"");
    }
    public static void createWindow(BufferedImage imageData, String title){
        if(title == null) title = "";
        new GUI(imageData, title);
    }
    public static BufferedImage imageCopy(String path){
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(path));
        } catch (IOException e) {
        }
    return img;
    }

    //create a new window with an imported image object
    public static void createWindow(){
        new GUI(null,"");
    }
    public static void main(String args[])
    {
        createWindow(imageCopy("/Untitled.jpeg"),"Untitled.jpeg");
    }
}

.

        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            g = graphic.getGraphics();
            g.drawImage(graphic,0,0,null);
        } 

You re-use the g variable on the third line here, thus losing your reference to the component's Graphics, so you draw the imge back onto iteself on the fourth line.

I guess it really was that simple. This is what I changed it to:

        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            Graphics g2 = graphic.getGraphics();
            g.drawImage(graphic,0,0,null);
        } 

Thanks!

Well yes, but since you never use g2 you may as well delete that whole line. All it's doing is confusing anyone who reads it.

:)

ps: Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

If/when there is an error you just told Java that you didn't want to know anything about it, and please discard the detailed error message that Java just created for you.
ALWAYS put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

Will do. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.