Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


[java] displaying images

This topic is 5717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do u display an image without using applets? i cant figure this out. every example i see uses applets. i dont know how to load one, or display it in a container. and whats the ImageObserver class for? -stephen

Share this post

Link to post
Share on other sites
to load an image:

public Image loadImage(String fileName) {
return loadImage(fileName, new Container());

public Image loadImage(String fileName, Container con) {
logger.loadingImage(getClass(), fileName);

Image loadedImage = loadedImage = Toolkit.getDefaultToolkit().getImage(fileName);
Image returnImage = null;

MediaTracker tracker = new MediaTracker(con);
tracker.addImage(loadedImage, 0);
try {
catch (InterruptedException e) {
logger.error(getClass(), "Error loading image from file: " + fileName);

returnImage = new BufferedImage(loadedImage.getWidth(null), loadedImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
returnImage.getGraphics().drawImage(loadedImage, 0, 0, null);

return returnImage;

to draw it you need something to draw your image onto, like a Panel for instance. to draw it do panel.getGraphics().drawImage(image, 0, 0, null) or something similiar.

Share this post

Link to post
Share on other sites

you can also use

public void loadTitleImage(){
mt = new MediaTracker(this);
im = getToolkit().getImage("ADDRESS OF THE IMAGE");
try{ // wait until the image is loaded
catch (InterruptedException e){
System.err.println("Error loading image");

where mt is an instance of MediaTracker class
and im an instance of Image class
you have to change "address of the image" for a proper URL.

after that, i call


in the paint method (or paintComponent method) of the object where i will draw the image

im : the image
XPOS : YPOS XSIZE YSIZE coordinate bounds for your image
this : reference to the object where you will draw the image

But I have also a question. Can someone know how to modify a loaded image in order to resize it (like stretching it in an OS desktop)???



Share this post

Link to post
Share on other sites
There are a number of ways to do it, it depends on exactly what you want to do.

You can of course just change the width and height in the drawImage() function, this would be OK if speed isn't an issue. If speed is an issue, do something like,

  Component c;
Image img;
c.getGraphicsConfiguration().createCompatibleImage(newWidth, newHeight);
img.drawImage(source, 0, 0, newWidth, newHeight, c);

This basically uses the drawImage() function to scale the image to an off-screen surface, you would then cache this and just use this version every time you need that particular scaling.

One last note, to use this, the component c must be non-null and must actually be present on the screen at that point.

Jiim Jonez

[edited by - Jiim on April 20, 2003 2:55:10 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!