creating and running applet
Page 1 of 1 • Share •
creating and running applet
Hi there! As Toma requested to make a tutorial about Java applet in forum (though she didn't request in forum's Java help zone :p ) and I was planning to make one as well..

I am going to show you the basic of the basics about how to run an applet. You can run
it using two ways:
1. using eclipse
2. using terminal/console
Using eclipse:
It is pretty easy
Lets take an example from book. I followed Sample.java From Herbert Schlidt's Java the complete reference, chapter 22.
I used the Class name as AppleSkel, so the output would be AppleSkel.class.
We don't have to worry about packages, as eclipse will manage it for you
But I kept the AppletSkel.java file in a package called Apple Very Happy
now, if you right click on AppleSkel.java and select run as applet like in the image, it will display the applet for you using the default parameters 200 x 200 pixels.

Now if you want the display settings changed, just right click on the java file, select Run Configurations, go to parameters (make sure java applet is selected in left menu)

And then you can change the height and width of the appletviewer set inside Eclipse. That's about it!
Running Applet using Terminal/Console:
This is pretty much same in both windows and Ubuntu, but you need to be careful.
First of all, make sure that terminal / console recognizes \"javac\", \"java\", \"appletviewer\" command. If you enter the commands and don't see anything like \"command not found\", then you may proceed.
Now, lets set the folders. Create the java file (same code above) and keep it in a folder called \"apple\" .Compile it using the command:
javac AppleSkel.java
inside apple.
Move the apple folder in a folder called appleOut(appleOut name doesn't matter, apple does).
Now create a html file called myAppleHtml.html inside appleOut folder.
open it using notepad / gedit and write the following code:
now, just type
appletviewer myAppletHtml.html
and it will open the applet you just created if you did everything correctly (or it will simply open an applet window with nothing inside :p )
Also, if you open the html file in a browser, you would be able to see your applet
Questions that might arise:
Ques:
What if I don't want to keep java file in a package?
Ans:
It is pretty easy, remove the package apple; line from java file,
then in the html file, change "apple.AppleSkel" to just "AppleSkel"
that will do.
Ques:
I can view my applet using AppletViewer, but nothing comes up in the browser!!
Ans:
Your browser is probably incapable of viewing java elements. go to this page for verifying.

I am going to show you the basic of the basics about how to run an applet. You can run
it using two ways:
1. using eclipse
2. using terminal/console
Using eclipse:
It is pretty easy

Lets take an example from book. I followed Sample.java From Herbert Schlidt's Java the complete reference, chapter 22.
I used the Class name as AppleSkel, so the output would be AppleSkel.class.
We don't have to worry about packages, as eclipse will manage it for you

But I kept the AppletSkel.java file in a package called Apple Very Happy
- Code:
package apple;
import java.awt.*;
import java.applet.*;
/*
<applet code ="AppleSkel" width = 300 height =100>
</applet>
*/
public class AppleSkel extends Applet {
String msg;
String newline = System.getProperty("line.separator");
public void init() {
setBackground(Color.cyan);
setForeground(Color.RED);
msg = "Inside init()....";
}
public void start() {
msg+="Inside start() --";
}
public void stop() {
}
public void destroy() {
}
public void paint(Graphics g) {
msg += "Inside paint(). ";
g.drawString(msg, 10, 30);
}
}
now, if you right click on AppleSkel.java and select run as applet like in the image, it will display the applet for you using the default parameters 200 x 200 pixels.

Now if you want the display settings changed, just right click on the java file, select Run Configurations, go to parameters (make sure java applet is selected in left menu)

And then you can change the height and width of the appletviewer set inside Eclipse. That's about it!

Running Applet using Terminal/Console:
This is pretty much same in both windows and Ubuntu, but you need to be careful.
First of all, make sure that terminal / console recognizes \"javac\", \"java\", \"appletviewer\" command. If you enter the commands and don't see anything like \"command not found\", then you may proceed.
Now, lets set the folders. Create the java file (same code above) and keep it in a folder called \"apple\" .Compile it using the command:
javac AppleSkel.java
inside apple.
Move the apple folder in a folder called appleOut(appleOut name doesn't matter, apple does).
Now create a html file called myAppleHtml.html inside appleOut folder.
open it using notepad / gedit and write the following code:
- Code:
<html>
<body>
<applet code="apple.AppleSkel" height =300 width = 400>
</applet>
</body>
</html>
now, just type
appletviewer myAppletHtml.html
and it will open the applet you just created if you did everything correctly (or it will simply open an applet window with nothing inside :p )
Also, if you open the html file in a browser, you would be able to see your applet
Questions that might arise:
Ques:
What if I don't want to keep java file in a package?
Ans:
It is pretty easy, remove the package apple; line from java file,
then in the html file, change "apple.AppleSkel" to just "AppleSkel"
that will do.
Ques:
I can view my applet using AppletViewer, but nothing comes up in the browser!!
Ans:
Your browser is probably incapable of viewing java elements. go to this page for verifying.
Last edited by BIT0122-Amit on Thu May 27, 2010 11:53 am; edited 5 times in total
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Re: creating and running applet
I want the .html code for this one to run

BIT0107-Toma- Programmer

- Course(s):
- BIT
Blood Group: B+
Posts: 280
Points: 453
Re: creating and running applet
Oh sorry...my net is doing problem..so I could not see the .html code 

BIT0107-Toma- Programmer

- Course(s):
- BIT
Blood Group: B+
Posts: 280
Points: 453
Re: creating and running applet
_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.
|About me|My Blog|

BIT0122-Amit- Founder

- Course(s):
- BIT
Blood Group: O+
Posts: 4187
Points: 6601

Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Active Topics!

» Understanding Timestamp Based Protocol
» Hi from Newbie
» Compare an image from a list of images in the sql database....tell which image is matching
» Sending email without email ID
» teach yourself c by herbert schildt pdf
» .NET MVC3: Unable to find the requested .Net Framework Data Provider. It may not be installed.
» SAD Slides: 4.1, 4.2
» QA topic Presentation - Urgent Notice