• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Toolkit.getDefaultToolkit().getImage(..);

hope it helps.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic