Welcome to Dream.In.Code
Become a Java Expert!

Join 150,041 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,594 people online right now. Registration is fast and FREE... Join Now!




Applet init() method ?

 
Reply to this topicStart new topic

Applet init() method ?, init() method called more than once..

mensahero
4 Jun, 2008 - 07:06 AM
Post #1

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
Good Day to all.. after reading more about Applets Here.

QUOTE

the applet can be initialized once and only once....


with that said. I assumed that the init() method will only be called once. But through back and forth testing in firefox and IE. (i.e. Clearing Cookies, browsing history in every trial) ... I've noticed the init() method is always called every time I revisit the site.

Heres what I've been doing:

1. Open a Fresh Browser ( Cleared Cookies and everything )
2. Go to mySite where I've uploaded the Applet for testing.
3. Wait until the progress bar finished while looking at the JAVA CONSOLE for the loading images status.

4. After the game finally start. I clicked the browser Go Back Button.
5. After #4 I clicked the Browser Go Forward Button.

After those this is what I've observed:

1. The Browser status display a applet started which is correct because thats what it supposed to do.

2. The JAVA CONSOLE display IMAGES BEING DOWNLOADED AGAIN

3. Because of #2 I'm Assuming that the APPLET is CALLING the INIT() method again.[/b] NOW THAT IS MY PROBLEM.

I can't let user re download all images again after revisiting the site without even closing the browser..

Here is My Site where I uploaded the applet.

Here is my APPLET CLASS
java

public class HelloJava3 extends JApplet
implements ActionListener
{

Timer t, t2;
ImageIcon bg, bgLayout;
JLabel bgLabel, bgLayoutLabel;
HelloComponent3 gameEngine;
JPanel panel;
JProgressBar pgBar;
int progress = 0, sliderUp = 0, sliderDown = 200;

Boolean animateIntrance =false;
AnimationComponent entranceUp, entranceDown;
AnimationComponent countImage = new AnimationComponent();

public AnimationComponent st, sr, sr2;


SwingWorker worker = new SwingWorker(){

protected HelloComponent3 doInBackground(){
AnimationComponent countImage = new AnimationComponent();
gameEngine = new HelloComponent3();
System.out.println(countImage.getImageLoadedCount());
return gameEngine;
}

public void done(){
gameEngine.setBounds(0,0,500,400);
panel.add (gameEngine);
gameEngine.requestFocus() ;
panel.repaint();
}
};

public void init(){
progress = 48;
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI(); }
});
} catch (InterruptedException e) {}
catch (InvocationTargetException e) {}
worker.execute();

t = new Timer( 1, this);

t.start();

entranceUp = new AnimationComponent("sliderWindowUp.png");
entranceDown = new AnimationComponent("sliderWindowDown.png");
entranceDown.setBounds(0,sliderDown,500,200);
entranceUp.setBounds(0,sliderUp,500,200);
panel.add(entranceUp);
panel.add(entranceDown);
}

public void start(){

}

public void stop(){}

public void destroy(){}

public void createGUI(){

bgLabel = new JLabel("NOW LOADING", JLabel.CENTER );

pgBar = new JProgressBar(0, progress);
pgBar.setValue(0);
pgBar.setStringPainted(true);
pgBar.setBounds(150,180,200,20);

panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,500,400);
bgLabel.setBounds(145,160,200,20);

panel.add( bgLabel );
panel.add( pgBar );
getContentPane().add(panel);
}


public void actionPerformed(ActionEvent e){
pgBar.setValue(countImage.getImageLoadedCount());
if(countImage.getImageLoadedCount() == progress && !animateIntrance){
t.stop();
t2 = new Timer(1, this);
t2.start();
pgBar.setVisible(false);
bgLabel.setVisible(false);
animateIntrance = true;
}

if(animateIntrance){
entranceDown.setLocation(0,sliderDown++);
entranceUp.setLocation(0,sliderUp--);
if(sliderDown > 400){
animateIntrance = false;
entranceDown.setVisible(false);
entranceUp.setVisible(false);
t2.stop();
}
}
}

class gameLayout extends JPanel{

}

}



The init method execute a swing worker that loads up my gameEngine and download all the images need for the game.

I badly need to resolve this performance issue first before actually continuing on my game proper. I'm kind of stuck here. crazy.gif crazy.gif

This post has been edited by mensahero: 4 Jun, 2008 - 07:11 AM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Applet Init() Method ?
4 Jun, 2008 - 07:41 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
The init method runs once refers to the life-cycle of an Applet. This takes part every time the page with the applet is loaded.
I'd suggest putting the images into the jar file of the applet, so they would be cached together with the classes of the applet (and oyu would have to load them slightly differently).

User is offlineProfile CardPM
+Quote Post

mensahero
RE: Applet Init() Method ?
4 Jun, 2008 - 07:54 AM
Post #3

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(1lacca @ 4 Jun, 2008 - 08:41 AM) *

The init method runs once refers to the life-cycle of an Applet. This takes part every time the page with the applet is loaded.
I'd suggest putting the images into the jar file of the applet, so they would be cached together with the classes of the applet (and oyu would have to load them slightly differently).


Thanks for the quick reply sir.

Yes I've read that article and it did say once thats why I cant understand why it keeps on loading the images again since everything is done in the init() method. It would be better if the applet wouldn't do anything when I return to the page because that will proved that the init() is indeed called only once and I left my start() method blank but the console says differently. The console still keep on downloading images.

Hmm I'm kind of interested in that jar file. May I have a more detailed information about that file? well I'm guessing that's an archive like a zip file. but how does it exactly work.

well my classes are actually uploaded individually in my sites file manager and all images are put on there respective directory folder.

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Applet Init() Method ?
4 Jun, 2008 - 08:22 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
A new applet is created every time you enter (or go back) to that page. When you leave the page, the applet is destroyed, and lost forever. So the next time it will be created, inited, and so on.

About jars. If you use an IDE, the build command will usually jar your application, in NetBeans it is located in the dist folder. It is a simple zip file, with quite similar features (compression, etc.)
So to create one with hand, you could use the jar utility, available in the SDK.
(simply run it for usage, or a more in-depth manual is here)
User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Init() Method ?
4 Jun, 2008 - 09:46 AM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
To extract the Applet from a .jar file in your .html instead of

CODE

<applet code="package.Applet.class" height="600" width="800"> </applet>


do

CODE

<applet code="package.Applet.class" archive="MyJar.jar" height="600" width="800"> </applet>


And this one is in "My favourites":

http://java.sun.com/docs/books/tutorial/deployment/jar/


This post has been edited by pbl: 4 Jun, 2008 - 10:09 AM
User is online!Profile CardPM
+Quote Post

mensahero
RE: Applet Init() Method ?
4 Jun, 2008 - 10:36 AM
Post #6

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
THANK YOU SO MUCH!!!!! biggrin.gif biggrin.gif biggrin.gif biggrin.gif biggrin.gif biggrin.gif

I've read that jar files are actually like zip files.. To test this I add all my files (i.e: images, classes, etc) into one zip file using winzip and rename the file into .jar. and it worked GREAT..

the applet used to load for about 40-60 secs now the loading time is almost 3-5 seconds only.. thats awesome.. I almost never noticed my progress bar.. lmao..


The most important information I've learned is that..

1. Jar files are more faster to download than individually uploaded files because the browser will only set a single download session connection rather than setting different download session for each file. Which will greatly increase applet download performance.

This provides greater range of development ideas.. thanks a lot for helping me guys.. biggrin.gif biggrin.gif biggrin.gif icon_up.gif icon_up.gif icon_up.gif tongue.gif tongue.gif tongue.gif
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Applet Init() Method ?
4 Jun, 2008 - 10:44 AM
Post #7

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
No problem smile.gif

I'd like to add one more thing: you can specify several jars in the archive attribute, so you can put your pictures and other resources that are not needed right away (in the init method), so they can be downloaded separately, thus the startup time might shorter.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Applet Init() Method ?
4 Jun, 2008 - 01:34 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(1lacca @ 4 Jun, 2008 - 11:44 AM) *

No problem smile.gif

I'd like to add one more thing: you can specify several jars in the archive attribute, so you can put your pictures and other resources that are not needed right away (in the init method), so they can be downloaded separately, thus the startup time might shorter.

Ouf 1licca never thought of that one... do you have an example
User is online!Profile CardPM
+Quote Post

mensahero
RE: Applet Init() Method ?
5 Jun, 2008 - 12:18 AM
Post #9

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(pbl @ 4 Jun, 2008 - 02:34 PM) *

QUOTE(1lacca @ 4 Jun, 2008 - 11:44 AM) *

No problem smile.gif

I'd like to add one more thing: you can specify several jars in the archive attribute, so you can put your pictures and other resources that are not needed right away (in the init method), so they can be downloaded separately, thus the startup time might shorter.

Ouf 1licca never thought of that one... do you have an example


yes.. A simple example would be great.. biggrin.gif biggrin.gif

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Applet Init() Method ?
5 Jun, 2008 - 01:38 AM
Post #10

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I'm a lazy ass, so no complete example, just two bricks into the wall (but should be enough).

Using several archives:
QUOTE
Combining an Applet's Files into a Single File

If your applet has more than one file, you should consider providing an archive file that bundles the applet's files into a single file. Whether archive files make sense for your applet depends on several factors, including your applet's size, performance considerations, and the environment you expect your users to have.

Archive files reduce your applet's total download time. Much of the time saved comes from reducing the number of HTTP connections that the browser must make. Each HTTP connection can take several seconds to start. This means that for a multifile applet, connection time can dwarf transfer time. You can further reduce transfer time by compressing the files in your archive file.

If you specify one or more archive files, then the applet class loader looks for the archive files in the same directory that it would search for the applet class file. The applet class loader then looks for the applet's class files in the archive files. If a file isn't in the archive, then the applet class loader generally tries to load it in the browser just as it would if the archive file weren't present.

The standard Java archive format, called JAR, was introduced in JDK 1.1 and is based on the ZIP file format. You specify JAR files using the ARCHIVE attribute of the <APPLET> tag. You can specify multiple archive files by separating them with commas:

<APPLET CODE="AppletSubclass.class" ARCHIVE="file1, file2"
WIDTH=anInt HEIGHT=anInt>
</APPLET>

Unfortunately, not all browsers understand the same archive format or use the same HTML code to specify the applet archive. Watch this page for the latest information about browser support for archives. To learn how to create a JAR file, see Creating a JAR File.



Loading resources from jar files (it's from the webstart documentation, but I'm pretty sure, it's the same mechanism, just substitute jars specified in the JNLP file with jars specified in the archive attribute of the applet tag):
QUOTE
Retrieving Resources from JAR files

Java Web Start only transfers JAR files from the Web server to the client machine. It determines where to store the JAR files on the local machine. Thus, an application cannot use disk-relative references to resources such as images and configuration files.

All application resources must be retrieved from the JAR files specified in the resources section of the JNLP file, or retrieved explicitly using an HTTP request to the Web server. Storing resources in JAR files is recommended, since they will be cached on the local machine by Java Web Start.

The following code example shows how to retrieve images from a JAR file:

// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create icons
Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif"));
Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif"));
...

The example assumes that the following entries exist in one of the JAR files for the application:

images/save.gif
images/cut.gif




However it naturally depends on the java plugin's loading strategy, if it downloads all archives at once, then it will actually slow down things.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month