Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying images on frames 3

Status
Not open for further replies.

Raywall

Programmer
Oct 1, 2003
51
RO
I have an application that is frame based( displays a frame ) and on the frame surface I have to display some images.Since I don't use applet I can't use getImage() and I don't
know other methods that displays images(jpg-s for example from HDD).
Please tell me how can this be done.
Thanks.
 
Depends on the type of image you wish to display - list all the types you need ...
 
I need to display jpg-s. The program is something like the "hearts" game and it will be played through network, so what I need to display are the cards,wich I have them in
.jpg format.
 
You will need to use either JAI, or the 1.4 SDK image-io package to load the image :

Something like :
Code:
RenderedOp ro = JAI.create("filestore", "C:\image.jpg");
BufferedImage bi = ro.getAsBufferedImage();
Graphics2D g2 = //get your graphics object from whatever Swing component you are using
g2.drawImage(bi);
 
JAI.create("filestore"

should be

JAI.create("fileload
 
I tryed using imageio, but the drawImage() function requires ImageObserver as parameter, and I could't find some method wich can initialize the ImageObeserver object.This is how I tryed:
Code:
BufferedImage image;
try
{
image=im.read(new File("D:\\somepicture.JPG")
}catch(IOException e){} 
public void paint(Graphics2D g)
{
    g.drawImage(image,g.getTransform(),this);
}
It doesn't work, and I believe that the problem is the last parameter ImageObserver.
After all I think that the main class will be applet :)
 
Yes, you are going about it slightly incorrectly. The below class is a standalone program (ie not an applet) but you should be able to adapt it for your needs.

Code:
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;

public class DispTest extends Canvas {

	BufferedImage image;

	public DispTest() {
	try {
		image = ImageIO.read(new File("C:/coastline2.jpg"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		setBackground(Color.cyan);

		WindowListener l = new WindowAdapter() {
	    		public void windowClosing(WindowEvent e) {System.exit(0);}
	    		public void windowClosed(WindowEvent e) {System.exit(0);}
		};
		Frame f = new Frame();
		f.addWindowListener(l);
		f.add(this);
        	f.pack();
		f.setSize(new Dimension(image.getWidth(), image.getHeight()));
		f.setLocation(0, 0);
		f.setVisible(true);
	}


	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		g2.drawImage(image, 0, 0, this);
	}

	public static void main (String args[]) {
		DispTest d = new DispTest();

	}
}
 
Yes it worked.I'll take a closer look at canvas because I never used it by now.
Thank you.
 
If you are using an applet, there is no need to extend the Canvas object, because the paint() method comes with the Applet object.
 
Yes of course. I was about to extend the applet in order to have acces to getImage(URL,picture name)which was the only function I new before talking with you that would return an Image object.Now that I have the example you gave me it won't be necessary.Thanks again.
 
Will this way of loading an image avoid lazy-(pain-in-the-***)-loading of images ?



Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
If you mean only loading the portion which is currently displayed in the view port, then I doubt it.

--------------------------------------------------
Free Database Connection Pooling Software
 
Well, I don't exactely know how it works but I'm also making a game in Java, and when the client starts up, I want it to load all its images so that the rest of the game would run smoothly.
I put them all in an array of images. The first screen displays fine(only contains 4 different images of the +-30 I put in the array).
But for level edditing I have a tree in wich you can select your image and then place it on the screen on the position you want. So when the user clicks the 'level edit' button, I want to show the frame with the tree, and I pass it the array of images. The frame then puts all the images in the tree, but not all images are displayed in the tree. When I close the frame and click again, then all the images are shown. Just like they only get loaded once they have to be painted for the first time.

I have a solution for the problem but its not a beatiful one..when the program starts up, I let it paint al its images to a small buffer so that the images start loading.
between 2 paints, I put a small sleep (50ms) to give it time to load, otherwise it doesn't work.

Since I work differently (use imageIcon to load image), I hoped that your way of working would actually load the file when you ask it to.

Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
Using the ImageIO class will defintely load the images into memory, but whether this will be quicker when painting and repainting I couldn't be sure of...

--------------------------------------------------
Free Database Connection Pooling Software
 
well, its not about quickness of painting I'm talking because that is fast enough. Its about wanting to paint an image but getting a nullpointer exception because the image isn't ready yet.
The painting happens the same way, with drawImage.
All I want is when I tell the image to load itself, it actually gets loaded. (I took this for granted but apperantly it is not that way)
So I'm happy with ImageIO :)

Thanks sedj!

Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

"Those who say they understand chess, understand nothing"

-- Robert HUBNER
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top