Java Applets: Section 3.3 (JIA's) Section 4.7 (JIA's) Section 5.5 (JIA's) Appendix A (JIA's)
Java Applets: Section 3.3 (JIA's) Section 4.7 (JIA's) Section 5.5 (JIA's) Appendix A (JIA's)
Java applets
Compiled Java class files
Run within a Web browser (or an appletviewer)
Loaded from anywhere on the Internet
security restrictions!
Java Basic Concepts
Source Code converted to Byte code
Byte code -machine code of JVM (Java Virtual
Machine)
Each real machine must have own JVM
Interpretation
JIT compilation
Direct Execution
Java Byte Code consists of
1 Byte opcode
1 or more operands
Capabilities and Limitations of
Applets
Build full-featured graphical user interfaces
(suitable for the Web)
Communicate over the Internet to a host server
(support Client-Server architecture)
Communicate with other applets on a form
Environment-neutral (any platform)
Limitations on Java applets to ensure client security
Capabilities and Limitations of
Applets
Bytecode verification
Forces loaded Java applets to undergo a rigorous set of checks in order to
run on the local system
The verifier checks each bytecode before it is executed to make sure that it
is not going to perform an illegal operation
Client-side precautions
Most Web browsers preclude Java applets from doing file access or
communicating with any computer on the Internet other than the computer
that the applet was loaded from
Enforced by the client Web browser (or other applet loader) but done by a
part of the Java runtime engine known as the class loader
First Java Applet
import java.awt.*; //Contains all of the classes for creating user interfaces
//and for painting graphics and images
import java.applet.Applet;
public class HelloFromVenus extends Applet {
To run:
a) Use the appletviewer from JDK
appletviewer Venus.html
The methods
defined Applet class
Except for paint() in class java.awt.Container
Any painting you want to do should be done here, or in a method you call
from here
g.setColor(Color.red);
Drawing Strings
g.drawString("A Sample String", x, y)
The java.awt.Color Class
• new Color(r, g, b)
• Range of 0 to 255
• Set of constants defined in java.awt.Color
The java.awt.Font Class
}
Example Applet
public void stop(){
Status += "--Stopping!";
showStatus("The applet is stopping!");
JOptionPane.showMessageDialog(this,Status);
repaint();}
Dimension d = getSize();
g.setColor(Color.orange);
g.fillRect(0,0,d.width,d.height);
g.setFont(theFont);
g.setColor(Color.blue);
g.drawString("Author:"+getParameter("FName")+" "+getParameter("LName"),50,50);
g.drawString("URL of the applet : " + getCodeBase(), 50, 100);
g.drawString("URL of document : " + getDocumentBase(), 50, 150);
<body>
<applet code="HiWorld.class" width=300
height=200>
<param name="arraysize" value="10">
</applet>
</body>
</html>
HTML Source
<!--Clock.html-->
<html>
<head>
<title>Clock</title>
</head>
<body bgcolor=white>
<h1>The Digital Clock Applet</h1><p>
<applet code= DigitalClock.class
width=400 height=100>
</applet>
<p><hr>
<a href= LifeCycleApplet.java>The source</a>
</body>
</html>
The <APPLET> Tag
The syntax for using the <APPLET> tag is the following:
<APPLET attributes>
<applet_parameter_tags>
alternate_content
</APPLET>