Java - Applets For Object Oriented Programming
Java - Applets For Object Oriented Programming
What is an applet?
applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages to life with interactive content, multimedia, games, and more the feature of Java that is primarily responsible for its initial popularity users can run applets simply by visiting a web page that contains an applet program (if they have the Java runtime environment installed on their computer)
2
Applets
Graphical Java programs Run inside web browser or AppletViewer Platform-neutral
Applet byte code file sent down from server and interpreted by browser ..
Applets
An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page to tell the browser about the applet For security reasons, applets run in a sandbox: they have no access to the clients file system
What is an applet
You write an applet by extending the class Applet Applet is just a class like any other; you can even use it in applications if you want When you write an applet, you are only writing part of a program The browser supplies the main method
class javax.swing.JApplet
java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet
Applet methods
public public public public public Also: public public public public void void void void void init () start () stop () destroy () paint (Graphics)
init()
start()
do some work
stop() destroy()
15
... browser shuts down browser calls destroy on the applet, once
repaint( )
Call repaint( ) when you have changed something and want your changes to show up on the screen repaint( ) is a request--it might not happen When you call repaint( ), Java schedules a call to update(Graphics g)
update( )
When you call repaint( ), Java schedules a call to update(Graphics g) Here's what update does:
public void update(Graphics g) { // Fills applet with background color, then paint(g); }
Hello
public class HelloWorldApplet extends JApplet { public void paint(Graphics g) { super.paint(g); g.drawString("Hello World!", 30, 30); } }
28
29
An applet is accessed by an HTML file. HTML is a mark-up language (it adds to text content by marking it up with tags) Browsers display the text and process (render) the tags For example, a file might begin with the line: IS C313 <i> Homework </i> Assignment The browser would display: IS C313 Homework Assignment
<HTML> <u> here comes an applet </u> <applet code= file.class width =500 height=500> </applet> </HTML> When a browser renders this HTML file, it will display underlined text and the applet whose byte code is in the file file.class
<BODY>
<applet code=" HelloWorldApplet.class width=150 height=100> </applet> </BODY> </HTML>
33
JApplet restrictions
can't read or write any files on user's hard disk can't make network connections to computers other than web server hosting applet can't execute other programs can't read much information about system on which it is running any window popped up by applet will have a warning at the bottom
34
35
36