omavideniz
asked on
Loading images from the Constructor of a Component
hi,
when i try to load images from the constructor of a component, images are not loaded. i think the imageproducer not loaded at the moment the constructor called and while its in execution. so from which method should i load images. currently i'm loading from paint() like,
public void paint( Graphics g )
{
if ( img1 == null)
{
img1 = getToolkit().getImage( "theimg.gif" );
while ( getToolkit().prepareImage( img1, -1, -1, this ) ) ;
}
g.drawImage( img1, 0, 0, 50, 50, this );
}
imho its not staight forward and i'm looking for the real solution.
thanks.
when i try to load images from the constructor of a component, images are not loaded. i think the imageproducer not loaded at the moment the constructor called and while its in execution. so from which method should i load images. currently i'm loading from paint() like,
public void paint( Graphics g )
{
if ( img1 == null)
{
img1 = getToolkit().getImage( "theimg.gif" );
while ( getToolkit().prepareImage(
}
g.drawImage( img1, 0, 0, 50, 50, this );
}
imho its not staight forward and i'm looking for the real solution.
thanks.
ASKER
so how can i determine if peer loaded. would be it be right to load image from addNotify() method?
I have designed a class that loads an Image from the jar of the applet. It can be used anywhere in your code as it uses a static method. Here it is:
public class ResourceLoader
{
/**
* Returns an Image from the applet's jar.
*/
public static Image getJarImage(String name)
{
Image image = null;
InputStream in = null;
MediaTracker mt = new MediaTracker(new Panel());
try {
in = Class.forNam ("ResourceLoader").getReso urceAsStre am(name);
if (in == null)
{
System.out.printl ("Image '"+name+"' not found.");
return null;
}
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
image = Toolkit.getDefaultToolkit( ).createIm age(buffer );
mt.addImage(image, 1);
mt.waitForAll(10000);
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch(Exception e) {}
}
return image;
}
}
public class ResourceLoader
{
/**
* Returns an Image from the applet's jar.
*/
public static Image getJarImage(String name)
{
Image image = null;
InputStream in = null;
MediaTracker mt = new MediaTracker(new Panel());
try {
in = Class.forNam ("ResourceLoader").getReso
if (in == null)
{
System.out.printl ("Image '"+name+"' not found.");
return null;
}
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close();
image = Toolkit.getDefaultToolkit(
mt.addImage(image, 1);
mt.waitForAll(10000);
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch(Exception e) {}
}
return image;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
omavideniz:
You have many open questions:
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20222327
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20261522
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20255833
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20248161
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247596
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247451
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239442
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20235419
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20234355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20230738
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20227630
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20210091
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163508
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20156064
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20151257
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20149551
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107807
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107283
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20096836
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095000
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20094242
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20081432
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20068029
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040399
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040320
To assist you in your cleanup, I'm providing the following guidelines:
1. Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.
2. Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.
3. When grading, be sure to read:
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.
4. Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.
5. If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.
Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!
thanks!
amp
community support moderator
2/6
You have many open questions:
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20222327
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20261522
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20255833
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20248161
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247596
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247451
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239442
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20235419
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20234355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20230738
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20227630
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20210091
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163508
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20156064
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20151257
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20149551
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107807
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107283
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20096836
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095000
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20094242
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20081432
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20068029
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040399
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040320
To assist you in your cleanup, I'm providing the following guidelines:
1. Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.
2. Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.
3. When grading, be sure to read:
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.
4. Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.
5. If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.
Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!
thanks!
amp
community support moderator
2/6
you can try using
Toolkit.getDefaultToolkit(