Display image in java

  • Java
  • Thread starter Yoyo_Guru
  • Start date
  • #1
33
0
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?
 

Answers and Replies

  • #2
Borg
Science Advisor
Gold Member
1,892
2,488
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?

http://www.google.com/#sclient=psy&...1&bav=on.2,or.r_gc.r_pw.&fp=53fd2b5286cafcf7" is your friend.

Here's a link with an explanation:
http://www.roseindia.net/java/example/java/swing/DisplayImage.shtml"
 
Last edited by a moderator:
  • #3
33
0
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.
 
  • #4
Borg
Science Advisor
Gold Member
1,892
2,488
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.
 
  • #5
33
0
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);
image = ImageIO.read(input);
}
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.getContentPane().add(panel);
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"
 
  • #6
Borg
Science Advisor
Gold Member
1,892
2,488
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);
      image = ImageIO.read(input);
    }
    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.getContentPane().add(panel);
    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.
 
  • #7
33
0
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?
 
  • #8
33
0
Oh, never mind. I figured it out. Thanks for the help :D
 
  • #9
33
0
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.getContentPane().add(display);
frame.setSize(500, 500);
frame.setVisible(true);
UI ui=new UI();
ui.ShowPlayer("red.jpg");

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





heres the UI class



BufferedImage player;

public void ShowPlayer(String playerAnimation)
{

try
{
File input = new File(playerAnimation);
player= ImageIO.read(input);
}
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);
image = ImageIO.read(input);
}
catch (IOException ie)
{
System.out.println("Error:" + ie.getMessage());
}
}

public void paint(Graphics g)
{
g.drawImage(image, 0, 0, null);
}
 
  • #10
Borg
Science Advisor
Gold Member
1,892
2,488
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.
 
  • #11
33
0
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
 
  • #12
Borg
Science Advisor
Gold Member
1,892
2,488
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.
 
  • #13
Borg
Science Advisor
Gold Member
1,892
2,488
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.
 
  • #14
33
0
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.
 
  • #15
Borg
Science Advisor
Gold Member
1,892
2,488
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:
  • #16
Borg
Science Advisor
Gold Member
1,892
2,488
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);
      image = ImageIO.read(input);
     [B] input = new File(secondFilename);
      secondImage = ImageIO.read(input);[/B]
    }
    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.getContentPane().add(panel);
    frame.setSize(500, 500);
    frame.setVisible(true);
  }
}
 
Last edited:
  • #17
33
0
oh, ok thanks, I tried that but the line

frame.getContentPane().add(panel);

keeps getting an error and says "cannot find symbol symbol: variable panel location: class Display"
 
  • #18
Borg
Science Advisor
Gold Member
1,892
2,488
oh, ok thanks, I tried that but the line

frame.getContentPane().add(panel);

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

Yes, that's an error because there's no panel object that has been declared (I accidently copied your original code). What do you think should go there? Is there a panel object that you could use that has been declared? Hint: I half-fixed this earlier.
 
  • #19
33
0
Oh, I got it working now. Thank you so much for your time/patience.
 

Related Threads on Display image in java

Replies
2
Views
2K
  • Last Post
Replies
5
Views
696
Replies
4
Views
3K
  • Last Post
Replies
14
Views
3K
Replies
4
Views
9K
Replies
19
Views
967
  • Last Post
Replies
8
Views
2K
  • Last Post
Replies
2
Views
3K
Replies
6
Views
2K
Top