creating and running applet

View previous topic View next topic Go down

creating and running applet

Post by BIT0122-Amit on Thu May 27, 2010 3:06 pm

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..
Very Happy
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 Very Happy

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 Very Happy
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! Very Happy

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 Smile

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" Smile
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

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: creating and running applet

Post by BIT0107-Toma on Thu May 27, 2010 3:40 pm

I want the .html code for this one to run
avatar
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

View user profile

Back to top Go down

Re: creating and running applet

Post by BIT0107-Toma on Thu May 27, 2010 3:45 pm

Oh sorry...my net is doing problem..so I could not see the .html code Sad
avatar
BIT0107-Toma
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 280
Points : 453

View user profile

Back to top Go down

Re: creating and running applet

Post by BIT0122-Amit on Thu May 27, 2010 3:50 pm

Razz it is not your net toma :p I had to edit this post about 7 times to show it properly.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: creating and running applet

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum