Lab: Java Applet
Instructions:
Java applets let you run some Java on your web page in many web browsers. For this lab, you will be
adding a Java applet to your website. There are some special instructions for this assignment. Read them carefully BEFORE
beginning the assignment!
- Select an example Java applet from this website.
- Click on "execution" to see the picture
- Click on "source code" to see the source code associated with the Java applet
- Save the source code for the Java applet
- Bring up a terminal window
- Ensure that you are in the directory for your web pages, i.e.
- cd ~your_login_name/public_html
- use an editor, such as gedit or emacs, to create a file with the Java applet source code with a file name that
matches the word after "public class", for example, gedit SnowMan.java &
- copy and paste your selected Java applet source code into this file.
- save the source code
- Compile your Java applet in your terminal window
- for example, javac SnowMan.java
- this will create a new file called SnowMan.class
- Create a small html file to use in viewing your applet
- for example, gedit sample.html &
- put the following html code in this file:
- <html>
<body bgcolor="silver">
<APPLET CODE="YourClassfile.class" WIDTH=350 HEIGHT=350 >
</APPLET>
</body>
</html>
- replace YourClassfile with the name of your classfile such as, SnowMan.class
- save and close
- in a terminal window, ensure YourClassfile (such as SnowMan.class) and html file is readable by all,
- ls -l YourClassfile.class sample.html
- Can everyone read them? i.e. do you see three 'r's?
- If not, chmod 644 YourClassfile.class sample.html
- ls -l YourClassfile.class sample.html (do you see three 'r's now?)
- View your applet by doing the following from your terminal window:
- Now modify the picture in some simple way (change a color, for instance), recompile (step #3), and view it again (step #5).
- Now that you know how to make changes, plan a bigger change to the picture, like adding or substantially changing a shape
or structure (e.g. buttons on the snowman; hat on the stick man, etc.). Figure out what you need to do,
and modify your program. Include at least one new method (e.g. oval, rectangle, polygon, ...). Recompile, look at it again with
the appletviewer, and repeat until the picture looks the way you want it to. Some helpful hints for adding a new method:
- - 0,0 top left corner in the coordinate system
- units are in terms of pixels
drawLine (x1, y1, x2, y2): x1, y1 start of line and x2, y2 is end of line
- drawRect (x1, y1, w1, h1): x1, y1 top left corner, w1 is width, h1 is height
- drawOval (x1, y1, w1, h1): x1, y1 top left corner, w1 is width, h1 is height
- Use online Java documentation to find at least one new function ("method" in Java) that is not presently in your applet.
Add it to your applet. To do this:
- Go to the online Java documentation.
- There are three major windows. Find the link that says (exactly) "Graphics" in the lower left window. The easy
way to do this is to use the browser find function (Ctrl-f) to find the link that says "Graphics" (you will need to
"find next" until it shows up). Pick this link.
- A new page will appear in the main window (center plus right side of screen). It will start with an overview of
the topic followed by many new methods.
- Read through these, pick one that is not presently in your applet but will be able to be seen (such as drawing arcs
or polygons) and modify your applet using this new method.
- Note: save change, recompile (step #3), and view applet (step #5).
- Now add your Java applet to your website. One possibility is to use the following tag, where YourClassfile is name of your file such as SnowMan:
- <APPLET CODE="YourClassfile.class" WIDTH=450 HEIGHT=450> </APPLET>
- Submit the following via ramct
- URL for your website with your modified Java applet, such as http://www.cs.colostate.edu/~bartlett,
- A description of what you changed
- The new method or methods you added to your Java Applet.
- Who reviewed your Java Applet
- If done early, continue to work on your website per the Lab 3 HTML assignment.