[java] displaying images
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
to load an image:
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.
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 { tracker.waitForAll(); } 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.
www.develop-games.com, it will be there somewhere.
Jiim Jonez
[edited by - Jiim on April 20, 2003 2:16:47 PM]
Jiim Jonez
[edited by - Jiim on April 20, 2003 2:16:47 PM]
hi
you can also use
public void loadTitleImage(){
mt = new MediaTracker(this);
im = getToolkit().getImage("ADDRESS OF THE IMAGE");
mt.addImage(im,1);
try{ // wait until the image is loaded
mt.waitForID(1);
}
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
g.drawImage(im,XPOS,YPOS,XSIZE,YSIZE,this);
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)???
thanks
manu
you can also use
public void loadTitleImage(){
mt = new MediaTracker(this);
im = getToolkit().getImage("ADDRESS OF THE IMAGE");
mt.addImage(im,1);
try{ // wait until the image is loaded
mt.waitForID(1);
}
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
g.drawImage(im,XPOS,YPOS,XSIZE,YSIZE,this);
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)???
thanks
manu
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,
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
--
www.develop-games.com
[edited by - Jiim on April 20, 2003 2:55:10 PM]
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
--
www.develop-games.com
[edited by - Jiim on April 20, 2003 2:55:10 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement