I'm learning GUI, can anyone show me how to display an image on a panel. So far I have this:

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


public class JlabelDemo
{
    public static void main(String[] args) 
    {
        // create an image icon, put the icon on a button, then create a panel and a frame
        ImageIcon imageIcon1 = new ImageIcon("red.jpg");
        JButton button1 = new JButton("Press me!", imageIcon1);
        JPanel panel1 = new JPanel();
        JFrame frame1 = new JFrame("Image Display"); 
        
        // add button1 to panel1, now add panel1 to frame1
        panel1.add(button1);
        frame1.add(panel1);
        
        frame1.setSize(400, 300);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);
    }
}

I tried to create an image like this:
Image image1 = new Image("red.jpg");
but it says its abstract.
If possible, try to show me how I can create an image, set it to "red.jpg", and display my image on the panel.

Recommended Answers

All 7 Replies

Image is abstract. Try using BufferedImage. So you don't want the image on the JButton, right? You want to draw it on the JPanel, right? If so, you can add this to the end of your main statement (note, don't add the JButton and don't resize the frame since the image will not repaint after the resizing). Here's the updated program. Not sure if you wanted to keep the JButton and whether it's supposed to have the image on the button or what:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.*;
import javax.swing.*;
import java.applet.*;


public class jLabelDemo
{
    public static void main(String[] args)
    {
        // create an image icon, put the icon on a button, then create a panel and a frame
        ImageIcon imageIcon1 = new ImageIcon("red.jpg");
        JButton button1 = new JButton("Press me!", imageIcon1);        
        JPanel panel1 = new JPanel();
        JFrame frame1 = new JFrame("Image Display");

        // add button1 to panel1, now add panel1 to frame1
//        panel1.add(button1);
        frame1.add(panel1);

        frame1.setSize(400, 300);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);

        File file = new File ("red.jpg");
        BufferedImage bi = null;
        try
        {
            bi = ImageIO.read(file);
        }
        catch (IOException ex)
        {
            System.out.println ("Couldn't open file.");
            System.exit (1);
        }

        panel1.getGraphics().drawImage(bi, 0, 0, null);
    }
}

Note that line 23 is commented out and lines 30 onward are new.

The code you suggested works. However, the image displays for a split second and disappears, any suggestions on how I can make it stay, or would I need to keep calling redraw?

Hmm. It wasn't doing that before, but yes, it is doing it now. That happens to me a lot and I end up experimenting around. I'm not 100% sure why it happens sometimes, but not others. Anyway, I tried making a class that extends JPanel and drawing it on an object of that class and it seems to not disappear now. I don't think that's necessary, but it works. This one has a paintComponent function, so you can resize it now.

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


public class jLabelDemo
{
    public static void main(String[] args)
    {
        // create an image icon, put the icon on a button, then create a panel and a frame
        ImageIcon imageIcon1 = new ImageIcon("red.jpg");
        JButton button1 = new JButton("Press me!", imageIcon1);
        button1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println ("I just got pressed");
            }
        });
        
        JFrame frame1 = new JFrame("Image Display");
        frame1.setSize(400, 300);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        MyPanel panel1 = new MyPanel ("red.jpg");
        frame1.add(panel1);
        frame1.setVisible(true);
    }
}


class MyPanel extends JPanel
{
    BufferedImage bi = null;

    public MyPanel (String filename)
    {
        File file = new File (filename);
        try
        {
            bi = ImageIO.read(file);
        }
        catch (IOException ex)
        {
            System.out.println ("Couldn't open file.");
            System.exit (1);
        }
    }


    @Override
    public void paintComponent (Graphics g)
    {
        g.drawImage(bi, 0, 0, null);
    }
}

Thank you very much, it works great now, it's very neatly done and simple for me to understand.

Thank you very much, it works great now, it's very neatly done and simple for me to understand.

You are welcome. Perhaps someone else knows why the earlier version quickly disappears sometimes, but not others?

Most likely the graphics context in the earlier version that you obtained from the getGraphics() call and drew on got updated by a repaint operation - so no more image. Overriding paintComponent() as you did in the second example is the proper way to do it.

Most likely the graphics context in the earlier version that you obtained from the getGraphics() call and drew on got updated by a repaint operation - so no more image. Overriding paintComponent() as you did in the second example is the proper way to do it.

Thanks. Good to know.

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.