CSC 171 LAB #2

FALL 2001

 

GOAL:

1.        Become familiar Java Applets

 

TASKS:

·         Demonstrate a program which prompts the user for input and displays output.

·         Specifically, convert your quadratic solver from a java application to a Java applet.

 

 

BACKGROUND:

 

Applets are Java programs that run on the world wide web. Web pages deliver content by using HyperText Markup Language (HTML). It is typical to use HTML to transfer information from a server computer to a client computer. Users (i.e. people) often use a browser on the client machine to view content. The browser is a computer program running on the client computer that makes requests for content to the sever machine. The browser interprets HTML and formats content for the user to view. A lot of the time information is static – text and graphics. However it is also possible for servers to send whole programs to the clients and have the clients run the programs. Applets are programs of this form. References to applets are embedded into HTML files on the server computer. Browsers contain copies of the JVM, they can download the bytecode files and run them in the browser’s JVM. Browsers enforce security, so that hackers cannot violate the client machine through applets.

 

 

STEPS:

 

1.        Begin by reading chapter 3. Pay special attention to Figure 3.12, because the applet in that figure will be built around a similar structure.

 

2.        compile and run your Java application from the previous lab – just to make sure everything still works.

 

3.        There are 3 major sub-steps to follow in converting your application to an applet. Not all conversions follow this process, but it demonstrates the basics.

 

1.        Change the application to an applet

2.        Convert the main method to the init method

3.        Include a paint method for output display

 

4.        First, your applets needs an HTML file to be embedded into. (You need to write two files for this lab – your source file and your HTML file). Look at figure 3.7 in the text and make a similar file. For simplicity’s sake, use the same names. Suppose  your java applet class will be called “MyFirstApplet”. You know you must have it in MyFirstApplet.java. Use MyFirstApplet.html for the HTML file and include a reference to “MyFirstApplet.class” in the code attribute of the applet tag inside the HTML file. You can load MyFirstApplet.htm into the browser at this point, but it won’t do much because you don’t have the applet compiled yet. Nonetheless, write the HTML file first, because you need it for testing. For now, keep both your .html and your .java file in the same directory.

 

5.        Change your class definition so that your class “extends JApplet”.  (See page 115 of the text for example and discussion). Remember the new include files.

6.        Applets (typically) don’t have “main” methods. Rather, they have “init” methods to get everything started. Change your main header to an init header. Try to follow the example in Fig 3.12. You don’t need to touch the body of the method.

 

7.        Compile your applet and fix any faults.

 

8.        Run your applet. However instead of using “run application”, like you did in the last lab use “run applet”. Alternately, you can invoke the appletviewer from a command line prompt.

 

9.        At this point, your applet runs, just like your application runs. But you are not done yet. You need to write a new method for your applet. This method will be the “paint()” method. In the application, you displayed your output on a pop up JOptionPane; in this lab you will display it on a web page.

 

10.     Following the example in Fig 3.12, write a paint method that uses the g.drawstring command to display the result of your calculation on the web page rather than on the pop up.

 

11.     Note that your “init()” method is now gathering input and your “paint()” method is displaying output. These methods need a way of tranfering information. This transfer is accomplished by the use of an instance variable, which is not declared inside any method. It is declared in the body of the class definition.

1.        It might make the most sense to have your instance variable be a reference to a String type. Inside of the “init()” method, you can set this new variable to the String which you would have put in the JOptionPane output. Inside your paint method, use the reference to this instance variable in the g.drawstring command.

 

12.     Comment out the old output inside the init() method.

 

13.     Draw a box around your ouput string in the paint method (just like in Fig 3.12). This will give you a small taste of the graphics capabilities of applets.

 

14.     Compile, run, and fix your program.

 

15.     Pretty cool, eh? Your program should be running in the appletviewer, but what happens when you try to run it in a browser?

1.        Fire up the browser

2.        Invoke File->open

3.        Browse to your .html file

4.        Load it in and observe the results

5.        Welcome to the WWW wars – more about that in the history minutes

16.     Save a backup copy of your .html file. Then invoke the HTML converter with Start->programs->programming->HTMLConverter

 

17.     Convert your .html file following the instructions in section 3.6.2 of the text.

 

18.     Load the converted file into the browser. Now you are on the WWW.

 

19.     One more step – use FTP to transfer your .html and your .class file to the web section of your troi account. DO NOT publish the source on the web by making it available on your troi account.

 

20.     Ok, you are done to get credit for all this work you need to get a piece of paper ready for your lab TA. This paper must have 2 things on it.

1.        Your name and lab section

2.        Your final version of the modified program – running from your troi account.

 

21.     When you get the paper ready call your lab TA over and demo the program for her/him. You MUST demo it served from you troi account – it is NOT enough to run it from the local machine in the lab. Give the paper to your TA. This completes the hand in process. The deadline is one week – no late assignments accepted.