Title: Applets
1Applets
- Applets are programs that run inside a web
browser. - The code for an applet is stored on a web server
and downloaded into the browser whenever you
access a web page with an applet. - Applets are programmed as a class.
2Applets Security
- Applets run in a sandbox, where it can display
information and get user input, but cant read or
touch anything on the users computer. - You can give an applet more privileges (reading
and writing local files), if you notify your
browser that the applet is trusted. - Applets can carry a certificate or signatures
from an authentication firm (VeriSign) to tell
you where the applet came from.
3Difference between Applets and Console
Applications
- Console
- Runs from command line
- Text based
- Terminal window
- public static void main(String args)
- Applets
- Runs from browser
- Web based
- Graphical
- No main() method
- Reserved methods for Applets.
- paint() method is called whenever the surface of
the applet needs to be filled in.
4javax.swing Package
- import javax.swing.JApplet
- public void paint(Graphics g)
-
- (This class is the Swing version of its
superclass java.applet.Applet)
5java.awt
- java.awt package (abstract windowing toolkit)
- Defines many classes for graphics programming,
mechanism for event handling, and a set of user
interface components. - java.awt.Graphics
- java.awt.Graphics2D
6Writing Text to the Screen
- java.awt.Graphics2D
- method
- drawString()
7import javax.swing.JApplet
import java.awt.Graphics
public class MyApplet extends
JApplet
import java.awt.Graphics2D
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
g2.drawString ("Floor plan of a diner", 145, 20)
8Graphics2D Class
- Use the drawString () method to draw a string,
starting at its basepoint. - Use draw() method of the Graphics2D class to draw
shapes such as rectangles, ellipses, line
segments, polygons, and arcs.
9Applet Display Properties
- Applet canvas is measured width and height
(defined in ltappletgt tag in HTML) - ltapplet code MyClass.class width300
height300gt lt/appletgt - Screen coordinates (x, y) and canvas size are
measured in pixels. - A pixel (picture element) is a dot on the screen.
- (10, 20) or (x, y ) is 10 pixels to the right
and 20 pixels down from the top left corner of
the panel.
10My Floor plan
Basepoint
11Graphical Shapes
- Rectangles
- Ellipses
- Lines
- Arcs
12import javax.swing.JApplet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
JApplet
import java.awt.Rectangle
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
Rectangle outerborder new Rectangle (5, 30,
400, 300)
g2.draw(outerborder)
13import javax.swing.JApplet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
JApplet
import java.awt.geom.Ellipse2D
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
Ellipse2D.Double seat1 new Ellipse2D.Double
(50, 125, 20, 20)
g2.draw(seat1) g2.drawString("S1",55,138)
14import javax.swing.JApplet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
JApplet
import java.geom.Line2D
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
Line2D.Double myline new Line2D.Double(5,50,400,
50)
g2.draw(myline)
15import javax.swing.JApplet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
JApplet
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
g2.drawArc(300, 100, 50, 50, 90, 90) // x, y, w,
h, start angle, arc angle
16About Web Pages
- Web pages are written in Hypertext Markup
Language (HTML) - HTML files are made up of tags to format text
(and include images, applets, audio and video
files) that tell the browser how to render the
text.
17HTML
- Most HTML tags come in pairs with an opening and
closing tag. - Each pair applies to the text between the two
tags. - The closing tag is just like the opening tag, but
it is prefixed by a slash (/).
18Sample HTML Page
- lthtmlgt
- lttitlegtmy first applet lt/titlegt
- lth3gtText Herelt/h3gt
- ltbodygt
- lt/bodygt
- lt/htmlgt
19Running an Applet
- To run an applet you need an HTML page with the
applet tag. - ltapplet code .class width heightgt
lt/appletgt - The applet tag include the applet in a web page
- Tell the browser where to find the applet code
20HTML Page with Embedded Applet
- lthtmlgt
- lttitlegtmy first applet lt/titlegt
- lth3gtText Herelt/h3gt
- ltbodygt
- ltapplet code .class width heightgt
lt/appletgt - lt/bodygt
- lt/htmlgt