Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: applet loading time

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default applet loading time

    My applet takes about 1 minute to load which I think is too much!? It is a 180kb application with about fourty 1kb GIF files, and a total of 70 classes all in one jar file. About half of the classes are inner classes mostly as action listeners for menu items. I started using too many of those after I read it is a good OO design. Should I go ahead and try to reduce the number of classes? Would it even be a significant improvement? I am not sure if it is wise to spend time on this because I think using JNLP solves my problem (it loads in less than a second). But I wonder what is the best practice regarding relative number of inner classes?

    My second question is about the keyword "protected". What exactly does it do ? I thought it wouldn't allow non-derived classes access but it seems to me it is acts like the public keyword ?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: applet loading time

    For your latter question, see
    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Protected has its role in certain scenarios, and is much different than public

    Your first question is hard to address without a specific example. You might want to test whether applets on your system in general load slowly. If not, strip your application down to minimal components, then build it back up to test which components might cause the delay.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    Quote Originally Posted by copeg View Post
    For your latter question, see
    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Protected has its role in certain scenarios, and is much different than public

    Your first question is hard to address without a specific example. You might want to test whether applets on your system in general load slowly. If not, strip your application down to minimal components, then build it back up to test which components might cause the delay.
    Thank you. I did some measurements with the applet and most of the loading time is consumed in one class which loads the images. It is good I din't start merging the classes. It takes about 53 secs on both firefox/chrome to load the images . And when I move that part out of the applet's init() and put in in start(), loading time reduces to about 7-8 secs. But if the user quickly invokes some commands to create a window which needs the images, then he has to wait until the images are fully loaded i.e full one minute. That was why I wanted to do everything in init(), but i don't understand why it takes that much.
     
    static void load_() {}
     
    static void getBufImage (String image_name,Integer index,BufferedImageOp op) {
    	    try {
    	    	java.net.URL imageURL = BoardPanel.class.getResource(image_name);
    			Image image = new ImageIcon(imageURL).getImage();
    			BufferedImage bimage = new BufferedImage(image.getWidth(null), 
    		    		image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    	    	bimage.getGraphics().drawImage(image, 0, 0, null);
    	    	bimage = op.filter(bimage, null);
    	    	IMAGES.put(index,bimage);
    	    } catch (Exception e) {
    	    	JavaBoard.printMessage("Error :" + e.getMessage());
    	    }
    }
    I use a dirty trick to initiate loading of the images during init() by calling an empty function of that particular class [Am sure there is a better method but I don't know it]. The load_() method signals that the class need to be loaded (thereby its static function getBufImage() which starts loading the images. When I move that to start() it loads the frame and other windows in 7-8 secs which is good. Only 26 images (a to z) are loaded at startup _BUT_ i have about 152 gifs in the package if the user wishes to use them later, but those are NOT loaded in the static function. May be it is opening connections for those 152 images too ? I don't understand class loading that well. I read somewhere that a jar file is loaded at once but it seems it is done incrementally i.e those classes needed for start up are loaded.
    public void init() {
    	BoardPanel_.load_();
            ....
    }
    public void start() {
         //BoardPanel_.load_();
    }

    About the protected keyword: Ok I understand now. I did not notice the difference because I never used more than one package. But is there something that can be used to allow access to a class and its sub-classes only. This seems more important to me than the current definition.

    TY
    Last edited by dabdi; September 20th, 2011 at 11:07 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: applet loading time

    Are all the images in the jar file?
    Are you running it from local files or over the internet?

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    Quote Originally Posted by Norm View Post
    Are all the images in the jar file?
    Are you running it from local files or over the internet?
    The images are in a sub directory inside the jar file. In local mode the images are loaded instantly hence no problem.
    That is also the case when opening it from a browser as long as I use local files. However uploading the one jar file on google
    sites and loading the applet takes about a minute. About 53 seconds is consumed within the class that loads the images.
    My internet connection is pretty fast. Deploying the applet as a JNLP takes about a second to load an execute... I think if I can force the jar file to be loaded at once, maybe it will help reduce applet loading time. I found a discussion with slow image loading in applets even when run in local mode here Image loading very slow from URL (Applets forum at JavaRanch) . But in my case that happens only when loaded over the internet.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: applet loading time

    Try to load images using the ImageIO utility class as opposed to loading the image through the ImageIcon (which not only creates an unused object, but uses Toolkit.createImage method that according to the API goes through security checks and on my system this is considerably slower)

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: applet loading time

    I don't know why the browser wouldn't load the jar file with all the images before the program started and from then on would be as fast as if it had loaded the jar file locally.
    I wouldn't think the browser would be able to get data from a partially loaded jar file. But maybe it can get some data out of the jar file without having read the whole jar file.???

    Do you have a server you can use that would show the interactions with the browser while the jar is being loaded to see how many times the browser calls the server while loading the applet's files?

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    Quote Originally Posted by copeg View Post
    Try to load images using the ImageIO utility class as opposed to loading the image through the ImageIcon (which not only creates an unused object, but uses Toolkit.createImage method that according to the API goes through security checks and on my system this is considerably slower)
    Indeed that helped reduce applet loading time to 40 secs. But I can not apply filters on the buffered image because it is indexed (from error message), so I did the test without that. It may go back up once that is added though.
    static void getBufImage (String image_name,Integer index,BufferedImageOp op) {
        try {
        	InputStream is = BoardPanel.class.getResourceAsStream(image_name);
        	BufferedImage bimage = ImageIO.read(is);
                   IMAGES.put(index,bimage);
        } catch (Exception e) {
        	JavaBoard.printMessage("Error :" + e.getMessage());
        }
    }
    One other thing , Google sites does not allow java scripts and applets so I am using a gadget to embed them.
    Maybe that adds to the security issue.
    More later.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: applet loading time

    Google sites does not allow java scripts and applets so I am using a gadget to embed them.
    Can you explain how that works?

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    Quote Originally Posted by Norm View Post
    Can you explain how that works?
    I have a google gadget XML file that I use to run java scripts and applets on that server. It is a hack I found here. The XML file is as follows
    <?xml version="1.0" encoding="UTF-8" ?>
    <Module>
    <ModulePrefs title="Custom Gadget" /> 
    <Content type="html"><![CDATA[ 
     
        <script type="text/javascript" src="http://www.java.com/js/deployJava.js">
        </script>
     
        <script type="text/javascript">
              var attributes = { 
                code: 'javaboard.JavaBoard',
                archive: 'https://sites.google.com/site/dshawul/javaboard.jar',
                width: 970, 
                height: 700
              };
              var parameters = { };
              var version = '1.5';
              deployJava.runApplet(attributes, parameters, version);
         </script>
     
    ]]></Content> 
    </Module>
    I now notice that the link to the jar file is a secure https..
    Then I insert the gadget by url link to the xml file once that is uploaded on the server.

    TY

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: applet loading time

    Thanks for that.

  12. #12
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    The loading time is further reduced and now is about 20-23 sec. Earlier I was loading the same image twice before applying a filter. So I load some image and apply Orange & Green filter to it to get two separate images. Well I now merged those two and the loading time is about halved. But you would expect the class loader will cache the image , but surprisingly that is not the case even within the same application.
    A minor problem is that the filtering results in slightly different color than expected. I think it is because I am working on the same bufferedimage for the second filtering operation. Is there a way to do "deep cloning" on bufferedimage ?
    static void getBufImage (String image_name,Integer index,BufferedImageOp[] op) {
        try {
        	java.net.URL imageURL = BoardPanel.class.getResource(image_name);
    		Image image = new ImageIcon(imageURL).getImage();
    		BufferedImage bimage = new BufferedImage(image.getWidth(null), 
    	    		image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
     
        	bimage.getGraphics().drawImage(image, 0, 0, null);
        	bimage = op[0].filter(bimage, null);
        	IMAGES.put(index + 'A',bimage);
        	bimage = op[1].filter(bimage, null);
        	IMAGES.put(index + 'a',bimage);
        } catch (Exception e) {
        	JavaBoard.printMessage("Error :" + e.getMessage());
        }
    }

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: applet loading time

    Is there a way to do "deep cloning" on bufferedimage ?
    Create a new BufferedImage of equal size, get its Graphics and draw the first BufferedImage using Graphics.drawImage (you might want to look into calling dispose() on the Graphics object returned by getGraphics or createGraphics - see the API for this method)

  14. #14
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    One final push may result in fastest applet loading. If I load just one image and use it every where the others are needed, the applet starts in maximum_ of 2-3 seconds! The gifs are 50x50 pixels each so I am trying to combine 152 of those files into one big gif file, and then load sections of that image into a buffered image. I was googling for tools to do just that but luckily java has it already. Always surprises me how many libraries java has stacked in it!
    try {
    	BufferedImage bimage = new BufferedImage(50 * 26, 
    	   		50 * 26, BufferedImage.TYPE_INT_ARGB);
    	Graphics g = bimage.getGraphics();
    	for(int i = 0;i < 26;i++) {
    		BufferedImage bi = IMAGES.get(i + 'A');
    		g.drawImage(bi,50 * i,0,null);
    	}
    	File outputfile = new File("saved.png");
    	    ImageIO.write(bimage, "png", outputfile);
     
    	} catch (IOException e) {
            }

    @copeg, Yes that should work I think. But now that I am using one gif file I may not need
    to make a copy. Thanks.

  15. #15
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: applet loading time

    Yep that worked really well. Applet loads in 1-3 seconds with all the images loaded. AND applet size also reduced by 40 kb. It is now a 140kb jar file. All round success! I produced a 600x650 pixel png file (12 raws by 13 columns 50pixel icons for all the 152 images).Then I loaded sections of the image by the following code which now takes care of cloning issue as well
    static void getBufImage (String image_name,BufferedImageOp[] op) {
    	    try {
    	    	java.net.URL imageURL = BoardPanel.class.getResource(image_name);
    			Image image = new ImageIcon(imageURL).getImage();
    			for(int j = 0;j < 2;j++) {
    				for(int i = 0;i < 26;i++) {
    					int x1 = 50 * (i / 12);
    					int y1 = 50 * (i % 12);
    					BufferedImage bimage = new BufferedImage(50,50, 
    							BufferedImage.TYPE_INT_ARGB);
    					Graphics g = bimage.getGraphics();
    					g.drawImage(image,
    							0, 0, 50, 50,
    							x1, y1, x1 + 50, y1 + 50,
    							null);
    					g.dispose();
    					bimage = op[j].filter(bimage, null);
    					IMAGES.put(i + ((j == 0) ? 'A' : 'a'),bimage);
    				}
    			}
    	    } catch (Exception e) {
    	    	JavaBoard.printMessage("Error :" + e.getMessage());
    	    }
    	}

    Thank you Norm and Copeg for your help.
    Last edited by dabdi; September 21st, 2011 at 09:00 PM.

Similar Threads

  1. Saving and Loading
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 01:31 PM
  2. GUI Not Loading
    By ADAMWD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2011, 01:22 PM
  3. Loading array
    By joshft91 in forum Collections and Generics
    Replies: 3
    Last Post: January 19th, 2011, 11:30 AM
  4. Loading in images
    By eXcellion in forum Java Theory & Questions
    Replies: 3
    Last Post: January 17th, 2010, 12:13 PM
  5. Prob in Loading Textures
    By sikriyogesh in forum Java Theory & Questions
    Replies: 0
    Last Post: December 15th, 2009, 05:59 AM