Instructions for Getting Applet Onto Your Webpage

Instructions for Getting Applet Onto Your Webpage

Instructions for getting applet onto your webpage:

1.  I put Ken’s BufferedApplet.java and his code(listed below) into a local directory on my machine. I then used the javac compiler to create all the .class files for all the java sources.

import java.awt.*;

public class Applet2 extends BufferedApplet {

Font myFont = new Font("Helvetica", Font.BOLD | Font.ITALIC, 60);

public Applet2() {

}

int[] X = { 200-40, 300-40, 300, 200 }; // X-COORDS OF THE POLYGON

int[] Y = { 200-40, 200-40, 200, 200 }; // Y-COORDS OF THE POLYGON

public void render(Graphics g) {

int w = getWidth();

int h = getHeight();

// BACKGROUND

g.setColor(Color.white);

g.fillRect(0, 0, w, h);

// TOP OF THE CUBE

g.setColor(Color.black);

g.fillPolygon(X, Y, 4);

// FRONT OF THE CUBE

g.setColor(Color.gray);

g.fillRect(200, 200, 100, 100);

// MAKE A NICE TEXT LABEL

g.setColor(Color.blue);

g.setFont(myFont);

g.drawString("This is my cube", 10, 50);

}

}

2.  I also created appletTest.html in the root directory with the following code:

<Html>

<Head>

<Title>Applet2 Example</Title>

</Head>

<Body>

Here is my applet<br>

<br>

<Applet Code="Applet2.class" width="640" Height="480">

</Applet>

</Body>

</Html>

3.  So now I have this directory structure on my local machine (all files are in a directory called Applet2):

<Applet2> à directory

{

Applet2.class

Applet2.java

appletTest.html

BufferedApplet$Event.class

BufferedApplet.class

BufferedApplet.java

}

4.  From the unix or DOS command lines, you can test the applet by typing > “appletviewer appletTest.html”. A new window should open with your applet. It if doesn’t then there are perhaps issues with the applet code itself and hopefully the java runtime will print out any errors it encounters.

5.  Now open up files in your NYUHome. Open your public folder. Click upload. Then click “Advanced Upload” (a small hyperlink somewhere towards the top of the page) and run the java plugin. Now you can just drag and drop the entire folder (mine is Applet2) into the public directory. Submit to start the upload. My directories now look like this:

Inside public:

Inside Applet2:

6.  Now we need to put a link on your index.html (or whatever your homepage is) linking to the new html file that contains your applet. My index.html looks like this (the important line is in red):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>jjt322's Personal Homepage</title>

<style type="text/css">

body {background-color: #f4f2e8;}

div {width: 400px; margin: 100px auto ; padding: 20px; border: 1px solid #ccc5a2; background-color: #fff;}

h1 {font-size: 16px; font-family: georgia,times,serif;}

p, li {font-size: 12px; font-family: "Lucida Grand",verdana,arial,sans-serif;}

ul {padding: 0; margin-left: 20px;}

</style>

</head>

<body>

<div>

<h1>Jonathan Tompson's NYU homepage</h1>

<p>To find out all about me see my personal homepage <a href="http://www.jonathantompson.com"> here</a>.</p>

<p>My NYU email is <p>

<p<b>IA classes:</b>

<ul>

<li<a href="http://cs.nyu.edu/~perlin/courses/fall2011/">CSCI-UA.0101-003 Intro To Computer Science</a</li>

<li<a href="Applet2/appletTest.html"> Applet Test!</a</li>

</ul>

</p>

</div>

</body>

</html>

7.  Now hopefully when you open index.html and when you click on the link the applet should open!

One Caveat:

A possible issue that could arise (especially if it runs in appletviewer but not on your webpage) is if you compile the applet using a new version of java, but then the browser tries to run the applet in an old version of the java runtime engine. Particularly, if the java console gives you this error (I got it the first time I tried):

java.lang.UnsupportedClassVersionError: Applet2 : Unsupported major.minor version 51.0

(or something similar)

A fix to this is to compile using the –target and –source flags to specify what version of java runtime engine the code will compile to. I did this:

> javac *.java –target 1.6 –source 1.6

I then had to clear the browser cache (I was using chrome) and I had to force the java runtime engine to close. I did this in Windows 7 by right clicking on the desktop -> “Start Task Manager” -> “Processes” tab -> Then click on any running instances of “Java.exe” and choose “End Process”