Java Student Project         

The Greatest   Button Applet
Ability Level: Beginner 
Estimated Time: 45 minutes
Objectives:
  • To learn how to write an HTML document to host an applet
  • To learn how to use the HTML Editor
  • To learn how to use AppletViewer
  • To learn how to write a simple applet using a Label
  • To learn how to change a label's font
  • To learn how to add TextFields and Buttons to applets
  • To learn about even-driven programming
  • To learn how to add output to an applet
Materials & Resources:
  • C++ Software
  • Textbook
Overview: Create an applet with a Button labeled "Who's the greatest?" 
When the user clicks the Button, display "I am!" 
in a large font.

Instructions: 1. Use the template below to create your button applet.

    In the class method, create your new button and your new label. 
   
    > Create a Button with a message "Who's the greatest?"
    > Feel free to be creative with colors and fonts in this section.
    > Create a label to use for a response.

2. In the init method, use the add to pass the button, the greatest.
     > Also use add to pass the label name.

3. In the actionPerformed method, use name.setText to reply "I am!"

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class greatest extends Applet implements ActionListener
{
  [commands go here ...]

 public void init()
{       
    [commands go here ...]
    theGreatest.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
     [commands go here ...]

 

 
Hints: 1. When you create an applet, you do the following:
     > Create a project and write Java programming code in files with extensions of   .java, just like when you write an application.
     > Design an HTML document that includes a statement to call your compiled Java class.
    > Build the project to compile the applet's .java files into bytecode with extensions of .class, just like when you write an application.
   > Open the HTML document in a Web browser or in AppletViewer.

2. Visual J++ assigns a file extension of .htm to the HTML documents it creates.

3. HTML is not case-sensitive, the Java programming language is case-sensitive.

4. HTML documents are text documents containing formatting instructions, called tags, along with the text that is to be displayed on a Web page.

5. The <APPLET> tag consists of three parts:
   > CODE = and the name of the compiled applet you are calling
   > WIDTH = and the width of the applet on the screen measured in pixels
   > HEIGHT = and the height of the applet on the screen measured in pixels

6. Pixels are the picture elements, or tiny dots of light that make up the image on your video monitor. The maximum size of your applets should be approximately 600 x 400 pixels to make sure that most people can see the entire applet as screens have different displays.

7. Visual Studio has a built-in HTML editor called the HTML Editor window, which is similar to the Text Editor window used for creating Java code files. The HTML Editor window contains three views for working with HTML documents: Design view - used for graphic creations, Source view -- allows editing of the raw HTML code, and Quick View -- used to display an HTML document. The different views may be accessed through the View Menu.

8.  Writing an applet involves three additional sets to writing a Java application.
   >  Add some import tatements.
   >  Learn to use some Windows components and applet methods.
   >  Learn to use the keyword extends.

9. The java.awt package is the Abstract Windows Toolkit, or AWT and contains commonly used Windows components such as labels, menus, and buttons. java.awt is imported so all the individual components don't need to be recreated.

10. The method used to add a component to an applet window is add( ). 
The object of the add( ) method is the applet itself, so when a component is added to a window, it can be written as: this.add( ); in place of add( ).

Example: 

// define a Label
Label greeting = new Label ("Hello. Who are you?");

// place the greeting within an applet 
add (greeting);

11. Applets begin the same way as Java applications, but they also must include the words extends Applet. The keyword extends indicates that your applet will build upon, or inherit, the traits of the Applet class defined in the java.applet package.

12. With an applet, the browser calls many methods automatically. If you fail to write one or more of these methods, Java creates them for you. The methods are: 
   > public void init() -- performs initialization tasks such as setting variables to initial values or placing applet components on the screen
   > public void start() 
   > public void stop()
   > public void destroy()

13. An option to viewing the applet in a browser is to run the applet using AppletViewer, change the name of the applet in the htm document and save it. At the command line, type jview /a filename.htm. 

14. A TextField is a Windows component where a user can type a single line of text data. A TextField object can be constructed using one of several constructors:
  >  public TextField( ) -- creates an empty TextField whith an unspecified length
  > public TextField(int numColumns) -- specifies a width for the field
  > public TextField(String initialText) -- provides initial text in the TextField
  > public TextField(String initialText, int numColumns) - both are specified

15. The setText() method allows changing of the text in a TextField that already has been created. The getText() method allows you to retrieve the String of text in a TextField.

16. When a user encounters a TextField in an applet, the user must position the mouse pointer in the TextField and click to get an insertion point. This causes keyboard focus which means that the next entries from the keyboard will be entered at that location. Automatic insertion point appearance within the TextField without mouse clicking is done with the use of requestFocus().method.  The keyboard can accept keystrokes with the setEditable() method. If conditions change, use the code answer.setEditable(true);.

17. A Button is even easier to create than a TextField. There are only two Button constructors. Use the setLabel() method and the getLabel() method.
  > public Button() - used to create an unlabeled Button
  > public Button (String label) -- used to create a labeled Button

18. An event occurs when someone using the applet takes action on a component, such as clicking the mouse on a Button object. With an event driven program the user might initiate any number of events in any order. The component on which an event is generated is the source of the event. An object that is interested in an event is a listener. 

19. To respond to user events within any applet created:
   > Prepare the applet to accept event messages.
   > Tell the applet to expect events to happen.
   > Tell the applet how to respond to any events that happen.

20. Mouse events use ActionListener as the class header. It is an interface, a set of specifications that let the applet work with the ActionEvents when a button is clicked. Interfaces are identified by writing extends Applet after the class name. To tell the applet to expect ActionEvents, the addActionListener() method is used. The ActionListener interface contains the actionPerformed(ActionEvent e) method specification.  

21.
Import is necessary to include library files.  Be sure to include Applet.

An example : import java.awt.Color;

22. Class must be declared with the name and extends Applet to indicate Applet is being created.               

Example: public class mymessage extends Applet 

23. A method (like a function in C++) is needed for each constructor, which is a block of code whenever a new object is created. Open and close braces enclose the code.

Example: public void init() {setBackground(Color.white); }

24. To use graphics the method must be declared.

Example: public void paint (Graphics g) {code in here }

25. There are many commands available such as g.setFont, g.setColor, and g.drawString which should be used in the paint method.

26. The pipe character ( | ) is used for "or" and allows the use of two different font sets. The pipe is generally the shift of the backslash on most keyboards.

27. Color uses red, green, blue sequentially. The maximum color is 255 and the minimum is 0. Three parameters are necessary.

Example of the parameter: (new Color(255,0,0)

Hints for using J++:

  1. From the start, programs menu, click on  Microsoft Visual J++.

  2. Click on New Project, under the New tab, click Web Pages and Applet on HTML.

  3. Enter the name and location and click open.

  4. After it loads, click file, new file, Java J++ and Java File, and click open.

  5. Write your code.

  6. Click on "Save Java File1 As" and be sure you are in the correct location, enter the name of the class you created with the extension of .java.

  7. An Applet.java is created along with the file.java you created. Click on the Applet.java and press delete to remove it. 

  8. Once this has been done, the file.java which you created copies to the project section. Now you may delete the miscellaneous files by highlighting it and pressing delete. 

  9. Be sure the Page1.htm file includes the name of your class in the code statement. Example: code=mymessage.class

  10. Be sure your settings have the output going to C drive and click the arrow to output your message.