advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

JavaBoutique: Articles: Tutorials:

Contents
Getting Started
Java Classes and Methods
The Code, Boss...the Code!
A Touch of Class
Packaging up your Code
Running a Java Application
Creating Java Applets
Embedding Applets in your HTML Pages
Adding Interaction
Conclusion

Java Programming
from the Grounds Up

Creating Java Applets

Java applets are similar to standalone Java applications, but are embedded within Web pages. We will examine two simple applets in order to see how HTML, Java, and its classes can be combined to deliver executable content. Figure 3 shows the Java code for a Variable Hello applet. A variation of the standard programmer's "Hello, World" program, the Variable Hello applet shows the basic structure of an applet and its interaction with HTML and the browser environment.

FIGURE 3
The Variable Hello Java Applet.

 1:  /**
 2:    A simple Java applet to issue an 
 3:    HTML-specified greeting
 4:    @author Mark C. Reynolds
 5:    @version 1.0
 6:  */
 7:  
 8:  import java.awt.*;
 9:  import java.net.*;
10:  
11:  public class VH extends java.applet.Applet 
12:         implements Runnable {
13:  
14:    String msg1;
15:    String msg2;
16:  
17:    public void init() {
18:      String who;
19:       String where;
20:      URL uwhere;
21:  
22:      who = getParameter("who");
23:      if ( who == null ) who = "world";
24:      uwhere = getDocumentBase();
25:      where = uwhere.toString();
26:     msg1 = "Hello, " + who + ",";
27:      msg2 = "from " + where;
28:    }
29:  
30:    public void paint(Graphics g) {
31:       g.setColor(Color.blue);
32:      g.drawString(msg1, 10, 15);
33:      g.drawString(msg2, 10, 30);
34:    }
35:  
36:    public void run() {
37:      resize(preferredSize());
38:     repaint();
39:    }
40:  }

The code begins with two import statements. These alert the Java compiler that this applet will use classes from the java.awt and java.net packages. The asterisk is a wildcard character indicating that everything within those packages should be imported. For simplicity's sake, we've used the wildcard instead of being more selective and designating only those classes that will be required.

In line 11, we declare a public class VH as a subclass of the Applet class with the extends clause. For this applet to function, we need to add executability functionality to the VH class. Since this is not something inherited by the Applet class--and remember, Java only has single inheritance--we will use the implements clause to fill in missing elements from a Java interface named Runnable. Interfaces such as Runnable are a useful workarounds for situations where single inheritance is a drawback.

Applet programmers must provide an init() method and a run() method within their Applet subclass. A class declaration that extends Applet and implements Runnable is mandatory in all applets; it is the equivalent of the main() entry point for applications.

The VH class has two class variables, msg1 and msg2, and three methods. The init() method is called once when an instance of VH is created. The run() method is called whenever the applet is activated. The paint() method is what displays the graphics. A Web browser will call init() when it first loads the applet's page, and it will call run() whenever the page is visited or revisited. The paint() method is called whenever the applet needs to draw something. There is a default paint() method, unlike init() and run(), but many applets will want to provide their own version of paint in order to perform their own specific graphical operations.

The VH init() method starts out by calling getParameter() to obtain the value of a parameter named who. getParameter() is a Java method for obtaining information from the HTML which defines a Web page.

NEXT


Mark Reynolds is a network protocol designer, Java programmer, computer animator, and fanatic mountaineer. He currently consults to Adaptive Optics Associates, a United Technologies company.

Reprinted from Web Developer® magazine, Vol. 2 No.1 Spring 1996 (c) 1996 internet.com Corporation. All rights reserved.


How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs