Applets - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Applets

Description:

import java.applet.Applet; public void paint(Graphics g) java.awt ... import java.awt.geom.Ellipse2D; public class MyApplet extends Applet ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 20
Provided by: kristens
Category:
Tags: applets | reimport

less

Transcript and Presenter's Notes

Title: Applets


1
Applets
  • 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.

2
Applets 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.

3
Difference 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.

4
java.applet Package
  • import java.applet.Applet
  • public void paint(Graphics g)

5
java.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

6
Writing Text to the Screen
  • Console
  • (java.io.PrintStream)
  • method
  • println()
  • Applets
  • (java.awt.Graphics2D
  • method
  • drawString()

7
import java.applet.Applet
import java.awt.Graphics
public class MyApplet extends
Applet
import java.awt.Graphics2D
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
g2.drawString ("Floor plan of a diner", 145, 20)
8
Graphics2D 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.

9
Applet 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.

10
My Floor plan
Basepoint
11
Graphical Shapes
  • Rectangles
  • Ellipses
  • Lines
  • Arcs

12
import java.applet.Applet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
Applet
import java.awt.Rectangle
public void paint (Graphics g)
Graphics2D g2 (Graphics2D) g
Rectangle outerborder new Rectangle (5, 30,
400, 300)
g2.draw(outerborder)
13
import java.applet.Applet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
Applet
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)
14
import java.applet.Applet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
Applet
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)
15
import java.applet.Applet import
java.awt.Graphics import java.awt.Graphics2D
public class MyApplet extends
Applet
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
16
About 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.

17
HTML
  • 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 (/).

18
Sample HTML Page
  • lthtmlgt
  • lttitlegtWeek 7 Example lt/titlegt
  • lth3gtText Herelt/h3gt
  • ltbodygt
  • lt/bodygt
  • lt/htmlgt

19
Running 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
Write a Comment
User Comments (0)
About PowerShow.com