*
*
|
|
Java Applet Sample Code
When viewing applet code, notice that the HTML to invoke the applet is shown first, followed by the Java code for the applet.
Minimalist Applet
|
|
<!-- Do not change the next three lines -->
<APPLET
CODEBASE = "/jeSessions/"
CODE = "Applet_<%=request.getSession().getId()%>.class"
WIDTH = 50
HEIGHT = 50
>
</APPLET>
public class <%=appletName()%> extends java.applet.Applet
{
public void init()
{
add(new java.awt.Button("OK"));
}
} |
Paint Splats
This applet uses JDK 1.1 methods and calls, and is compatible with most popular browsers today.
|
|
<!-- Do not change the next three lines -->
<APPLET
CODEBASE = "/jeSessions"
CODE = "Applet_<%=request.getSession().getId()%>.class"
WIDTH = 400
HEIGHT = 300
>
</APPLET>
public class <%=appletName()%> extends java.applet.Applet implements Runnable {
int width, height;
boolean more;
public void init() {
new Thread(this).start();
}
public void run() {
more = true;
while (more) {
repaint();
try {
Thread.currentThread().sleep(250);
} catch(java.lang.InterruptedException e) { /* no problem, end of wait */ }
}
}
public void paint(java.awt.Graphics g) {
width = getSize().width;
height = getSize().height;
g.setColor(java.awt.Color.getHSBColor((float)Math.random(),
(float)Math.random(),
(float)Math.random()));
g.fillOval((int)(Math.random()*width/2),
(int)(Math.random()*height/2),
(int)(Math.random()*width/2),
(int)(Math.random()*height/2));
}
public void stop() { more = false; }
} |
Sample Output

Paint Splats with Java Plugin
The web page ensures that the Java plugin is downloaded and installed before
running the applet. Caution: the Java Plugin is over 5MB! Long lines were folded in the code below so the page would not be too wide when displayed.
|
|
<SCRIPT LANGUAGE="JavaScript"><!--
var _info = navigator.userAgent; var _ns = false;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0
&& _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0
&& ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0
&& java.lang.System.getProperty("os.version").indexOf("3.5") < 0)
|| (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0)));
//--></SCRIPT></COMMENT>
<SCRIPT LANGUAGE="JavaScript"><!--
if (_ie == true) document.writeln('<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = 400 HEIGHT = 300
codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
<NOEMBED><XMP>');
else if (_ns == true) document.writeln('<EMBED type="application/x-java-applet;version=1.3"
CODE = "Applet_<%=request.getSession().getId()%>.class" CODEBASE = "/jeSessions/"
WIDTH = 400 HEIGHT = 300 scriptable=false
pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED><XMP>');
//--></SCRIPT>");
<APPLET CODE = "Applet_<%=request.getSession().getId()%>.class" CODEBASE = "/jeSessions/"
WIDTH = 400 HEIGHT = 300></XMP>
<PARAM NAME = CODE VALUE = "Applet_<%=request.getSession().getId()%>.class" >
<PARAM NAME = CODEBASE VALUE = "/jeSessions/" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="false">
</APPLET>
</NOEMBED></EMBED></OBJECT>
<!--
<APPLET
CODE = "Applet_<%=request.getSession().getId()%>.class"
CODEBASE = "/jeSessions/"
WIDTH = 400
HEIGHT = 300>
</APPLET>
// Don't change <%=appletName()%> in the next line.
public class <%=appletName()%> extends java.applet.Applet implements Runnable {
int width, height;
boolean more;
public void init() {
new Thread(this).start();
}
public void run() {
more = true;
while (more) {
repaint();
try {
Thread.currentThread().sleep(250);
} catch(java.lang.InterruptedException e) { /* no problem, end of wait */ }
}
}
public void paint(java.awt.Graphics g) {
width = getSize().width;
height = getSize().height;
g.setColor(java.awt.Color.getHSBColor((float)Math.random(),
(float)Math.random(),
(float)Math.random()));
g.fillOval((int)(Math.random()*width/2),
(int)(Math.random()*height/2),
(int)(Math.random()*width/2),
(int)(Math.random()*height/2));
}
public void stop() { more = false; }
} |
Sample Output
Same as in the prior example
Random strings
|
|
// Don't change <%=appletName()%> in the next line.
public class <%=appletName()%> extends java.applet.Applet implements Runnable {
int width, height;
boolean more;
public void init() {
new Thread(this).start();
}
public void run() {
more = true;
while (more) {
repaint();
try {
Thread.currentThread().sleep(250);
} catch(java.lang.InterruptedException e) { /* no problem, end of wait */ }
}
}
public void paint(java.awt.Graphics g) {
width = getSize().width;
height = getSize().height;
g.setColor(java.awt.Color.getHSBColor((float)Math.random(),
(float)Math.random(),
(float)Math.random()));
g.drawString("X", (int)(Math.random()*width),
(int)(Math.random()*height));
}
public void stop() { more = false; }
}
|
Add New Code Samples Here
If you have code samples that don't fit into any of the topics above which you would like to contribute,
please put them here.
We'll sort them out into their own sections as appropriate later.
|
| to add a new posting or reply to an existing posting. |
|
|