creating and running applet
2 posters
Page 1 of 1
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 3:53 pm; edited 5 times in total
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
it is not your net toma :p I had to edit this post about 7 times to show it properly.
Similar topics
» How to get screen size in Applet
» Compiling and running files in a package
» Creating JAR file
» Creating PDF and XPS in MS Offfice
» Need help creating using encrypted Rar files
» Compiling and running files in a package
» Creating JAR file
» Creating PDF and XPS in MS Offfice
» Need help creating using encrypted Rar files
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford