1

I am new to coding, and am having troubles displaying a JPEG using the paintComponent(); method to a JFrame in java. My code looks like this:

import java.awt.*;
import javax.swing.*;

class SimpleGuiMe {
    MyDrawPanel imageex;

    public static void main (String [] args){
        SimpleGuiMe gui = new SimpleGuiMe();
        gui.go();
    }

    public void go() {
        JFrame frame = new JFrame();
        imageex = new MyDrawPanel();

        frame.getContentPane().add(imageex);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    class MyDrawPanel extends JPanel {

        public void paintComponent(Graphics g) {
            Image image = new ImageIcon("WOW(1).jpg").getImage();
            g.drawImage(image,3,4,this);
        }
    }
}

I'm not sure if I need to save the JPEG file that I want to display in a certain directory in order to use it.

Also I'm using a somewhat outdated textbook to get the code shown here. It is post Java 5.0, but was only published in 2005.

Any help will be greatly appreciated!

  • Possible duplicate of http://stackoverflow.com/questions/1242581/display-a-jpg-image-on-a-jpanel – Parker Hoyes Dec 11 '15 at 04:07
  • It's not a duplicate of that post as it's related to the location of image. – 11thdimension Dec 11 '15 at 04:33
  • 1) Application resources (like the image) will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 2) NEver try to load image in a paint method! They should be loaded once at start-up, and be attributes of the class. 3) When overriding a paint method, call the super method first. – Andrew Thompson Dec 11 '15 at 06:50
  • `ImageIcon(String)` expects that the `String` will represent a file path, either and absolute or relative path (from the context of the programs execution), so, based on your example, the image file MUST reside within the same directory that the program is executed in. This can be tricky, as depending on how you execute the program, the execution context can change, a better solution is to embed these resources within the program itself – MadProgrammer Dec 11 '15 at 08:17

1 Answers1

2

Your Panel class should be something like below

class MyDrawPanel extends JPanel {
    private Image image; 
    public MyDrawPanel() {
        image = new ImageIcon("C:/..path to file../marilyn monroe.jpg").getImage();
    }
    public void paintComponent(Graphics g) {
        
        g.drawImage(image,3,4,this);
    }
}

ImageIcon is using FileInputStream internally from Sun's awt API which can take absolute or relative path to a file. If you use absolute path then there's no concern, it will work every time without fail.

If you want to use relative path then you'll have to figure out which directory is current directory of java process invoking your code.

Suppose we invoke following command

C:/users/meuser>java SimpleGuiMe

Then current directory of the java process would be C:/users/meuser

If you put image in C:/users/meuser or in a sub directory in this directory, then it would be accessible via relative path like below.

//directly inside C:/users/meuser
image = new ImageIcon("marilyn monroe.jpg").getImage();

or

//in a sub directory like Downloads
image = new ImageIcon("Downloads/marilyn monroe.jpg").getImage();

Instead of using ImageIcon you can use use javax.imageio.ImageIO to read image like below.

image = ImageIO.read(new File("path to file/filename.jpg"));

Difference between ImageIO and ImageIcon is that ImageIcon has a constructors supporting URL parameter for image location, whereas ImageIO is reading images from either File or InputStream.

Relative and absolute path concepts remain the same in this case too.

Note: paintComponent method is called whenever the UI is updated like window is resized or any new element is drawn/redrawn. Image reading should be done outside this method as it may take some time to read the, you don't want the paintComponent() method to block UI till the image is read. Also it needs to be read only once.

Community
  • 1
  • 1
11thdimension
  • 10,333
  • 4
  • 33
  • 71