GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Image in a Panel.. need help urgently..!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image in a Panel.. need help urgently..!!" Watch "Image in a Panel.. need help urgently..!!" New topic
Author

Image in a Panel.. need help urgently..!!

Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
I have a class which extends Panel..
I have put textfields, buttons and labels in the form. Now I want to insert an Image in it..
I know how to insert an image in an Applet ..using getImage() method..
but am unable to put an image in a Panel..
Which method of Panel should I use.. tried produceImage() but wasnt successfull..
Any help and an example would be really helpfull..
Awaiting ur reply...
Kajol Shroff
[ May 09, 2002: Message edited by: Kajol Shroff ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Toolkit.getDefaultToolkit().getImage(..);

hope it helps.
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
first get the image from an image file.
use MediaTracker to ensure that the image is loaded fully before the panel is displayed.
next draw the image on the applet using the drawImage method of Graphics g inside paint()
ex.
Image img;
MediaTracker tracker;
img=Toolkit.getDefaultToolkit().getImage("some.gif/jpg");
tracker=new MediaTracker(this);
tracker.addImage(img,1); // here 1 is the ID of the image, give any number
try{
tracker.waitForAll(); //or tracker.waitForID(id no)
}
catch(InterruptedException ie){}

//...

public void paint(Graphics g){
g.drawImage(img,0,0,this); //if the image width and height are smaller than the panel size, u use the following drawImage method
g.drawImage(img,0,0,getWidth(),getHeight(),this);
}
thats all !!
hope it works
}
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Hi Shashi,
I tried the code which you had sent... and it worked fine when I viewed it with Appletviewer and it worked fine ...but if I view it in the html page...it gives me SecurityException..
I think its because I have compiled the file on a different drive and am checking the file on a different path..is it because of that...
I want to make it portable..i.e. the file will be available in the same folder only the absolute path will change...
How do I handle this..?
I am checking the java.net.* package...wish em luck.. and if anybody can help me out ...please let me know...
Kajol Shroff
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
Hi Kajol,
yes, you r right, my code won't work when u put the panel in an html page! i thought u were talking abt creating a stand alone application... well thats another thing.. then
since you are going to put the panel in an applet (isn't it, bcoz u can't embed a panel alone in an html page, without an applet class) u can use the method u have been knowing !!
since u r going to use on the web, u have to use it like this
Image img=getImage(getDocumentBase(),"some.gif");

or if u r using the panel alone without adding it to an applet (though i don't know how u r going to do that in an HTML page! )
Image img=java.applet.Applet.getImage(getDocumentBase(),"some.gif");
both the examples asssume that u r putting the image file in the same directory as the HTML file.
otherwise, give the url of the image.
i'm sure this time there won't be any exception when the image is getting loaded !
rgds,
Shashi
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Hi Sashi,
I cant do Image im = java.applet.Applet.getImage(getDocumentBase(),"image.gif");
because getImage() is a non-static method and a non-static method cant be referenced from a static method...,I also get an error saying getDocumentBase() method not found cannot resolve the symbol..and thats because i am extending Panel and nto applet ..although I have imported java.applet.*;
Can u tell me where I am going wrong...?
Need help very urgently...
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
Hi Kajol,
yeah, u r right! i'm sorry abt tha java.awt.applet.getImage() part!! that was foolishness on my part!
from ur last post, it looks like u r using it as an application, and using main() to initialise ur panel.
can u just send ur code?! if its big, u send the
code for the panel subclass and the part where u r creating an instance of that.
only after seeing i can tell u which method to use
rgds
Shashi
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
and one more thing if u want to try use another method inside the constructor of ur class
Image img=Toolkit.getDefaultToolkit().createImage("some.gif"); // asssuming ur image file is in the same location as ur class file.
or if u want to access from a url,
Image img=Toolkit.getDefaultToolkit().createImage("http://somesite.com/some.gif");
still i say, if u send me the code, i can see what to use, bcoz i don't know whether urs is a standalone application or going to be on the web.
rgds
Shashi
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
as for the second example we should first create an url like (after importing java.net.*)
URL url=new URL("http://somesite.com./some.gif");
and use in the image as
Image img=Toolkit.getDefaultToolkit().createImage(url);
but its not working when u r reffering to some of the websites!! i don't know, bcoz no exceptions r thrown!!
will see and get back to u
see u
rgds
Shashi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Image in a Panel.. need help urgently..!!