# Display image in java

• Java
I know this is probably a simple question but I want to make a program that uses an animation but don't know how to display an image like a jpeg in a program. I found a couple answers to this online but don't really know what's going on in the code. Would someone mind writing some sample code to display an image and explain what is going on in the code?

Borg
Gold Member
I know this is probably a simple question but I want to make a program that uses an animation but don't know how to display an image like a jpeg in a program. I found a couple answers to this online but don't really know what's going on in the code. Would someone mind writing some sample code to display an image and explain what is going on in the code?

Here's a link with an explanation:
http://www.roseindia.net/java/example/java/swing/DisplayImage.shtml"

Last edited by a moderator:
Thanks a lot :D I had googled it but nothing I found was as succinct as that. Kept on finding irrelevant stuff or stuff that was way more complicated than what I was looking for.

Borg
Gold Member
Thanks a lot :D I had googled it but nothing I found was as succinct as that. Kept on finding irrelevant stuff or stuff that was way more complicated than what I was looking for.

No problem. Glad to help. I got it on my first Google search but, I have also felt the pain of fruitless Google searches.

I'm sorry to bother but I have another question. If I have a class that is supposed to display and image would this code be right for the class?

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

public class Display extends Panel
{

BufferedImage image;

public void ShowImage(String fileName)
{
try
{
File input = new File(fileName);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}

public void paint(Graphics g)
{
g.drawImage(image, 0, 0, null);
}

static public void main(String args[]) throws
Exception
{
JFrame frame = new JFrame("Display image");
Panel panel = new Display();
frame.setSize(500, 500);
frame.setVisible(true);
}
}

And if it is right how would I use it from the main with an image called "test"

Borg
Gold Member
I'm sorry to bother but I have another question. If I have a class that is supposed to display and image would this code be right for the class?

Code:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Display extends Panel
{

BufferedImage image;

public void ShowImage(String fileName)
{
try
{
File input = new File(fileName);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}

public void paint(Graphics g)
{
g.drawImage(image, 0, 0, null);
}

static public void main(String args[]) throws
Exception
{
JFrame frame = new JFrame("Display image");
[B]Display display = new Display();
display.ShowImage("C:\\someDirectory\\yourfilename.jpg");[/B]
frame.setSize(500, 500);
frame.setVisible(true);
}
}

And if it is right how would I use it from the main with an image called "test"
I've wrapped your code in the code tags.

To compile your code, you have to have Java in your classpath. You can test this with a DOS window by typing this:
java -version

If that's working, then you should be able to compile and run:
Compile:
In a DOS window, go to the directory where Display.java is located and type this:
javac Display.java

Run:
From the same window and directory, you can now run the program:
java Display

You are very close on the program. Your program runs but there is nothing that calls the ShowImage() method. Also, by creating it as an instance of Panel, it won't even know about the ShowImage() method in the Display class. I've bolded the two lines that I changed in your program and it works as long as you can compile it.

I've wrapped your code in the code tags.

To compile your code, you have to have Java in your classpath. You can test this with a DOS window by typing this:
java -version

If that's working, then you should be able to compile and run:
Compile:
In a DOS window, go to the directory where Display.java is located and type this:
javac Display.java

Run:
From the same window and directory, you can now run the program:
java Display

You are very close on the program. Your program runs but there is nothing that calls the ShowImage() method. Also, by creating it as an instance of Panel, it won't even know about the ShowImage() method in the Display class. I've bolded the two lines that I changed in your program and it works as long as you can compile it.

So how would I call it form my main? Do i need it to extend main and not panel? If so will that mess it up? And what code would I use in the main to call it?

Oh, never mind. I figured it out. Thanks for the help :D

Sorry to bother again but I've been having an issue with loading pictures in the jpanel. I can get them to load but no matter what I do I always have the same image over the other and cannot control which is on top even if I change the order that I display them. Like they will both print but one will always cover the other. here is my code.

public static void main(String[] args)throws
Exception
{
JFrame frame = new JFrame("Display image");
Display display = new Display();
display.ShowImage("background");
frame.setSize(500, 500);
frame.setVisible(true);
UI ui=new UI();
ui.ShowPlayer("red.jpg");

frame.repaint();
frame.setVisible(true);
}

heres the UI class

BufferedImage player;

public void ShowPlayer(String playerAnimation)
{

try
{
File input = new File(playerAnimation);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}

public void paint(Graphics g)
{
g.drawImage(player, x, y, null);
}

and here is the display class

BufferedImage image;

public void ShowImage(String fileName)
{
try
{
File input = new File(fileName);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}

public void paint(Graphics g)
{
g.drawImage(image, 0, 0, null);
}

Borg
Gold Member
Your drawImage call is putting all images in the same position (0,0) each time. If you want to display two images at the same time, you will have to put the second image in a different position. If you want them side by side, you can modify your method to take the width of the first image in order to set the display of the second image's starting point on that axis. Keep in mind also that the coordinate system that is used it not the same as what you may be used to from geometry. If I remember correctly, y is positive in the downward direction.

Oh, sorry forgot to include declarations of x and y for UI which are each 250. so shouldn't it be at 250,250? and I am trying to get the image from UI displayed on top of the image from display but I can't even if i switch every time the two are used in the main

Borg
Gold Member
Oh, sorry forgot to include declarations of x and y for UI which are each 250. so shouldn't it be at 250,250? and I am trying to get the image from UI displayed on top of the image from display but I can't even if i switch every time the two are used in the main

I don't know what the length and width of your images are so I don't know where you're getting the 250, 250 numbers from. In any case, the main point is that you do not want to set the same coordinate for both images. For example, let's say that you have two images that are each 100 by 100. If you want them side by side, you will have to pass 0,0 for one and 100, 0 for the other. If you send the same coordinates each time, they will overwrite.

Borg
Gold Member
Another way to solve this is to create and add a second display to your frame's content pane. Then, the two displays won't be on top of each other.

the ui image is 5x5 and the display image is 500x500. The thing is that I want them to overlap. I want the 5x5 image to be printed in the middle of the other image. But the display image is always over the ui instead of the ui being over the display.

Borg
Gold Member
the ui image is 5x5 and the display image is 500x500. The thing is that I want them to overlap. I want the 5x5 image to be printed in the middle of the other image. But the display image is always over the ui instead of the ui being over the display.

I'll ask the obvious. Have you tried changing the order in which you load them?

EDIT: Never mind. I see the problem now. I'll have to look that one up.

Last edited:
Borg
Gold Member
This isn't too difficult after all. I'm not sure why you have three different classes but they're not necessary. What you are effectively doing with the two display classes is creating two different images and telling it to replace one with the other on your display (two completely different paint methods).

All you really need to do is update the code to create a second image in the ShowImage method and then display both in the paint method. From the original example, using one class:

Code:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Display extends Panel
{

BufferedImage image;
[B]BufferedImage secondImage;[/B]

public void showImage(String fileName, [B]String secondFilename[/B])
{
try
{
File input = new File(fileName);
[B] input = new File(secondFilename);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}

public void paint(Graphics g)
{
g.drawImage(image, 0, 0, null);
[B]g.drawImage(secondImage, 20, 20, null);[/B]
}

static public void main(String args[]) throws
Exception
{
JFrame frame = new JFrame("Display image");
Display display = new Display();
display.showImage("C:\\someDirectory\\yourfilename.jpg", [B]"C:\\someDirectory\\yourSecondFilename.jpg"[/B]);
frame.setSize(500, 500);
frame.setVisible(true);
}
}

Last edited:
oh, ok thanks, I tried that but the line

keeps getting an error and says "cannot find symbol symbol: variable panel location: class Display"

Borg
Gold Member
oh, ok thanks, I tried that but the line