Showing posts with label applets. Show all posts
Showing posts with label applets. Show all posts

Thursday, January 5, 2012

Adding an Applet to an HTML Document

For many element tag pairs, you can specify an element attribute in the starting tag that defines additional or qualifying data about the element. This is how a Java applet is identified in an < applet > tag. Here is an example of how you might include a Java applet in an HTML document:< /applet >

< html >
< head >
< title > A Simple Program < /title >
< /head >
< body >
< hr/ >
< applet code = “MyFirstApplet.class” width = 300 height = 200 >
< /applet >
< hr/ >
< /body >
< /html >

The two shaded lines between tags for horizontal lines specify that the bytecodes for the applet are contained in the file MyFirstApplet.class. The name of the file containing the bytecodes for the applet is specified as the value for the code attribute in the < applet > tag. The other two attributes, width and height, define the width and height of the region on the screen that will be used by the applet when it executes. These always have to be specified to run an applet. Here is the Java source code for a simple applet:

import javax.swing.JApplet;
import java.awt.Graphics;
public class MyFirstApplet extends JApplet {
public void paint(Graphics g) {
g.drawString(“To climb a ladder, start at the bottom rung”, 20, 90);
}
}

Note that Java is case-sensitive. You can’t enter public with a capital P - if you do, the program won’t compile. This applet just displays a message when you run it. The mechanics of how the message gets displayed are irrelevant here - the example is just to illustrate how an applet goes into an HTML page. If you compile this code and save the previous HTML page specification in the file MyFirstApplet.html in the same directory as the Java applet code, you can run the applet using appletviewer from the JDK with the command:

appletviewer MyFirstApplet.html

In this particular case, the window is produced by Internet Explorer under Windows XP. Under other operating systems and browsers it is likely to look a little different. Since the height and width of the window for the applet are specified in pixels, the physical dimensions of the window will depend on the resolution and size of your monitor. This example should work by default with Internet Explorer since the installation process for the JDK will install the Java plug-in for you. If it doesn’t work, check the Internet Options . . . on the Tools menu for Internet Explorer. On the Advanced tab you should find an option titled “Use JRE v1.5.0 for < applet > (requires restart)”; make sure this option is checked. If you use Mozilla 1.x or Netscape 7.x, follow the instruction given in the installation documentation for the JDK to enable the plug-in.

Monday, December 26, 2011

The Hypertext Markup Language

The Hypertext Markup Language, or HTML as it is commonly known, is used to define a web page. When you define a web page as an HTML document, it is stored in a file with the extension .html. An HTML document consists of a number of elements, and each element is identified by tags. The document will begin with < html > and end with < /html >. These delimiters, < html > and < /html >, are tags, and each element in an HTML document will be enclosed between a similar pair of tags between angle brackets. All element tags are case-insensitive, so you can use uppercase or lowercase, or even a mixture of the two, but by convention they are capitalized so they stand out from the text. Here is an example of an HTML document consisting of a title and some other text:

< html >  

< head > 

< title > This is the blog of Java < /title > 

< /head > 

< body > You can put whatever text you like here. The body of a 
document can contain all kinds of other HTML elements, including 
< b > Java applets < /b >. Note how each element always begins 
with a start tag identifying the element, and ends with an end tag 
that is the same as the start tag but with a slash added. The pair 
of tags around ‘Java applets’ in the previous sentence will display 
the text as bold.

< /body >

< /html > 


There are two elements that can appear directly within the < html > element, a < head > element and a < body > element, as in the example above. The < head > element provides information about the document, and is not strictly part of it. The text enclosed by the < title > element tags that appears here within the < head > element will be displayed as the window title when the page is viewed.

Other element tags can appear within the < body > element, and they include tags for headings, lists, tables, links to other pages, and Java applets. There are some elements that do not require an end tag because they are considered to be empty. An example of this kind of element tag is < hr/ >, which specifies a horizontal rule, a line across the full width of the page. You can use the < hr/> tag to divide up a page and separate one type of element from another.

Executing an Applet

The Java compiler in the JDK will compile both applications and applets. However, java applet is not executed in the same way as an application. You must embed an applet in a web page before it can be run. You can then execute it either within a Java 2-enabled web browser, or by using the applet-viewer, a bare-bones browser provided as part of the JDK. It is a good idea to use the applet-viewer to run applets while you are learning. This ensures that if your applet doesn’t work, it is almost certainly your code that is the problem, rather than some problem in integration with the browser. If you have compiled an applet and included it in a web page stored as MyApplet.html in the current directory on your computer, you can execute it by entering the command:

appletviewer MyApplet.html

So how do you put an applet in a web page.

Tuesday, December 20, 2011

What Is Java All About?

Java is an innovative programming language that has become the language of choice for programs that need to run on a variety of different computer systems. First of all, Java enables you to write small programs called applets. These are programs that you can embed in web pages to provide some intelligence. Being able to embed executable code in a web page introduces a vast range of exciting possibilities. Instead of being a passive presentation of text and graphics, a web page can be interactive in any way that you want. You can include animations, games, interactive transaction processing - the possibilities are almost unlimited.

Of course, embedding program code in a web page creates special security requirements. As an Internet user accessing a page with embedded Java code, you need to be confident that it won’t do anything that might interfere with the operation of your computer, or damage the data you have on your system. This implies that execution of the embedded code must be controlled in such a way that it will prevent accidental damage to your computer environment, as well as ensure that any Java code that was created with malicious intent is effectively inhibited. Java implicitly incorporates measures to minimize the possibility of such occurrences arising with a Java applet.

Java’s support for the Internet and network-based applications generally doesn’t end with applets. For example, Java Server Pages (JSP) provides a powerful means of building a server application that can dynamically create and download HTML pages to a client that are precisely customized for the specific request that is received. Of course, the pages that are generated by JSP can themselves contain Java applets.

Java also allows you to write large-scale application programs that you can run unchanged on any computer with an operating system environment in which Java is supported. This applies to the majority of computers in use today. You can even write programs that will work both as ordinary applications and as applets.

Java has matured immensely in recent years, particularly since the introduction of Java 2. The breadth of function provided by the standard core Java has grown incredibly. Java provides you with comprehensive facilities for building applications with an interactive graphical user interface (GUI), extensive image processing and graphics programming facilities, as well as support for accessing relational databases and communicating with remote computers over a network. Just about any kind of application can now be programmed effectively in Java, with the implicit plus of complete portability.

Of course, Java is still developing and growing. Amongst a myriad of other enhancements, release 1.4 of Java added a major additional capability, the ability to read and write XML. Java 5.0, which followed release 1.4, adds further new facilities, including important new language features as well as significant additions to the class libraries. You’ll be learning about all of these in this book.