Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  monitoring loading progress in Applet  (Read 5164 times)
0 Members and 1 Guest are viewing this topic.
Offline phu004

JGO Knight


Medals: 19
Projects: 10
Exp: 10 years


NoSuchPersonException


« Posted 2008-11-10 22:40:13 »

Many people have suggested that images should be contained in a single jar file to improve loading efficiency. But
I also want to give the user a general idea how many image files have been downloaded.  Is there any way I can accomplish
both?

Regards,

Pan
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-11-10 22:44:45 »

Download the ZIP (containing the images) from within your applet. That way you can count the bytes and show a progress bar, If your server sends the ContentLength (which it probably does) you can use it to calculate the percentage using URLConnection.getContentLength()

Downside is that that ZIP will never end up on your browser's cache.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #2 - Posted 2008-11-10 22:51:00 »

images are useally very staticy and your code changes in comparison a lot, I wouldn't package them together. The additional request you save pales in comparison with the additional download.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline brackeen

Junior Devvie





« Reply #3 - Posted 2008-11-10 22:52:53 »

Downside is that that ZIP will never end up on your browser's cache.
Java 6 will cache it. I get 304 "Not Modified" responses in my apache logs all the time.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2008-11-10 23:58:54 »

Fancy stuff. Good to know!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline phu004

JGO Knight


Medals: 19
Projects: 10
Exp: 10 years


NoSuchPersonException


« Reply #5 - Posted 2008-11-11 00:07:47 »

Thanks for the reply guys. However I don't have my own server, I use a free file server called freewbes.com to host my applet.
It is rather inconvenience to use cause I can only upload one file at a time (at least it is true last time I tried).  This is another reason
why I want to pack all the image files into a single jar/zip.

I think  riven 's  approach sounds like a good idea. Could you show me some of the code of how to do this?
You can assume that all my class files are packaged in a sigle jar, and all the image files are packaged
in another jar.

Many thanks

Pan
Offline thijs

Junior Devvie




Lava games rock!


« Reply #6 - Posted 2008-11-11 08:59:55 »

Java 6 will cache it. I get 304 "Not Modified" responses in my apache logs all the time.

And if you're stuck with jre < 1.6 compatibility, you could always just zip your images and rename the zip to .class. f you download it over an urlconnection with the property useCache(true) it will cache your zip...

Below is an excerpt I copy pasted from my IO lib (some helper methods might be missing, but you get the point), the download method would return a byte[] of the zipped data in your case, which you can then feed to the decompress method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
   public synchronized static byte[] downloadData(String resource, Class loader, ResourceInfo info, ProgressListener plistener, boolean cache)
   {
      try
      {
         InputStream is = openStream(resource, loader, info, cache);
         DataInputStream dis = new DataInputStream(is);
         
         if (dis==null) {
            Log.log(this, "Resource not found: " + resource);
            return null;
         }

         int length=dis.available();
         //read min blocks of 8kb to force frequent updates for plistener (=~1update/sec on a 56k modem)
         if (length <= 0) length = 8192;
         //read max blocks of 100kb to prevent downloading all data in one sweep
         if (length >= 10240) length = 10240;

         byte[] chunk=new byte[length];
         ByteArrayOutputStream bos = new ByteArrayOutputStream();

         //set the progresslistener if available
         //if (plistener != null) plistener.next(plistener.DOWNLOAD);

         //read in bytes from the stream & update the progresslistener if available
         int bytesRead = 0;
         while((bytesRead = dis.read(chunk,0,length)) != -1) {
            //System.out.println("bytes read from bis: "+bytesRead);
            bos.write(chunk,0,bytesRead);
            //if(plistener!=null) plistener.update(bytesRead);
         }

         bos.flush();
         closeStream(dis);
         closeStream(is);
         return bos.toByteArray();
      }
      catch(Exception e) {
         Log.log(this, e);
         return null;
      }
   }

   public synchronized static void decompress(byte[] data, ProgressListener plistener)
   {
      //check how many zipentries the data contains
      int size=getNrEntries(data),index=0;

      sizes = new String[size*2];

      //check if there are any entries to process and tell the progress listener
      if(size>0) {
         Log.log(this, "Decompressing: "+size+" zipentries found");
         //if(plistener!=null) plistener.init(size);
      }
      else
      {
         Log.log(this, "Decompressing: no zip entries in data");
         return;
      }

      try
      {
         ZipEntry ze;
         ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(data));

         while ((ze=zis.getNextEntry())!=null)
         {
            if (ze.isDirectory()) {
               continue;
            }
           
            size=(int)ze.getSize();
           
            sizes[index++]=ze.getName();
            sizes[index++]=""+size;
           
            Log.log(this, "Decompressing: zip entry: "+ze.getName()+","+"size="+size);
             
            //if this is a valid entry, decompress it
            if (size>-1)
            {
               //set the progresslistener for this entry
               //if(plistener!=null) plistener.next(plistener.DECOMP);

               byte[] decompData = new byte[(int)size];
               int bytesRead=0;
               int chunk=0;
               while ((size - bytesRead) > 0)
               {
                  chunk=zis.read(decompData,bytesRead,(int)size - bytesRead);
                  if (chunk==-1) {
                     break;
                  }
                  bytesRead+=chunk;
                 
                  //if(plistener!=null) plistener.update(chunk);
               }
           
               //cache the entry in the game cache
               GameContext.addObject(ze.getName(),decompData);
               Log.log(this, "Decompressing: zip entry: "+ze.getName()+"  bytes read="+bytesRead+",size="+size+",compressed size="+ze.getCompressedSize());
            }
            else
            {
               Log.log(this, "Decompressing: zip entry: "+ze.getName()+" has unknown size");
               continue;
            }
         }
      }
      catch (NullPointerException e) {
         Log.log(this, e);
      }
      catch (FileNotFoundException e)   {
         Log.log(this, e);
      }
      catch (IOException e) {
         Log.log(this, e);
      }
   }

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Offline phu004

JGO Knight


Medals: 19
Projects: 10
Exp: 10 years


NoSuchPersonException


« Reply #7 - Posted 2008-11-17 02:28:32 »

Quote
the download method would return a byte[] of the zipped data in your case, which you can then feed to the decompress method

Thanks for posting the code, however I have a question:
Suppose now I have the raw data of a jpg image in the format of byte[],  how do I create an Image object from it?


Offline moogie

JGO Ninja


Medals: 16
Projects: 6
Exp: 10 years


Java games rock!


« Reply #8 - Posted 2008-11-17 04:58:56 »

You could use the ByteArrayInputStream to convert the byte array into a stream and the use ImageIO to create an image from the stream.

Java4k RIP 2014
Offline thijs

Junior Devvie




Lava games rock!


« Reply #9 - Posted 2008-11-17 11:33:40 »

Thanks for posting the code, however I have a question:
Suppose now I have the raw data of a jpg image in the format of byte[],  how do I create an Image object from it?

Ah, thats the easy part Wink

Image = Toolkit.getDefaultToolkit().createImage( imagebyte[] );

Or

BufferedImage image = ImageIO.read ( new ByteArrayInputStream ( imagebyte[] ) );

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline phu004

JGO Knight


Medals: 19
Projects: 10
Exp: 10 years


NoSuchPersonException


« Reply #10 - Posted 2008-11-17 23:01:02 »

Quote
Image = Toolkit.getDefaultToolkit().createImage( imagebyte[] );
May be a silly question:
How could the it tell whether the  byte data contains a jpeg image or image in other format ?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #11 - Posted 2008-11-18 09:15:59 »

ImageIO won't care about format if it's not reading the image from a file. It uses its own format types which you can access via myImage.getType(). If you want to have a specific type of image (like one with transparency and color values from 0 to 255, or a custom type, or a compressed type, etc.) then you can just create a new image of that type and then copy the ImageIO one over.

Either way just assume ImageIO is magic and that elves give you a useable image, regardless of whether it was PNG, JPG, or whatever.

See my work:
OTC Software
Offline phu004

JGO Knight


Medals: 19
Projects: 10
Exp: 10 years


NoSuchPersonException


« Reply #12 - Posted 2008-11-19 20:39:05 »

Thanks for the help. Didn't know that java image library is that powerful.  Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (439 views)
2019-09-04 15:33:17

hadezbladez (5294 views)
2018-11-16 13:46:03

hadezbladez (2213 views)
2018-11-16 13:41:33

hadezbladez (5562 views)
2018-11-16 13:35:35

hadezbladez (1156 views)
2018-11-16 13:32:03

EgonOlsen (4592 views)
2018-06-10 19:43:48

EgonOlsen (5467 views)
2018-06-10 19:43:44

EgonOlsen (3127 views)
2018-06-10 19:43:20

DesertCoockie (4023 views)
2018-05-13 18:23:11

nelsongames (4740 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!