APPLETS - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

APPLETS

Description:

Then draw or fill shapes. g2.fill(easterEgg); FONTS. Specify text and base point: ... { Rectangle2D.Double body = new Rectangle2D.Double(xLeft, yTop 10, 60, 10) ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 28
Provided by: thaddeusf
Category:
Tags: applets

less

Transcript and Presenter's Notes

Title: APPLETS


1
APPLETS
  • CSC 171 FALL 2004
  • LECTURE 6

2
APPLETS
  • Graphical Java programs
  • Run inside web browser
  • Platform-neutral
  • Easy deployment--loads when needed
  • Secure

3
Applets
  • Applets are Java prgrams that can be embedded in
    Hypertext Markup Language (HTML) documents
  • The browser that executes an applet is
    generically knows as the applet container
  • So, often we use appletviewer

4
How Applets Work
5
HTML
  • Text and tagsJava is an ltigtobject-orientedlt/igt
    programming language
  • Browser renders the tagsJava is an
    object-oriented programming language
  • Bulleted list (like this one) is defined by
    tagsltulgtltligt. . .lt/ligtltligt. . .lt/ligtltligt. .
    .lt/ligtlt/ulgt
  • Use ltgt for ltgt symbols

6
Simple HTML
  • lthtmlgt
  • ltheadgt
  • lttitlegt Ted Pawlicki's CSC 171 HTMLlt/titlegt
  • lt/headgt
  • ltbodygt
  • Hello Ted!
  • lt/bodygt
  • lt/htmlgt

7
Applet Methods
  • Applets have no main()
  • Rather, it has key methods that deal with the
    unique situation of applets

8
Key Applet Methods
  • init
  • start
  • paint
  • stop
  • destroy

9
Key Applet Methods
  • public void init()
  • public void start()
  • public void paint(Graphics g)
  • public void stop()
  • public void destroy()

10
public void init()
  • Called once by appletviewer or browser
  • Called when an applet is loaded
  • Performs initialization
  • Instance variables
  • GUI components
  • Loading sounds images (multimedia)
  • Creation of threads (animation)

11
public void start()
  • Called after the init method completes
  • Called every time the user of the browser returns
    to the HTML page
  • Performs tasks that must be repeated when the
    applet is revisited
  • Restarting an animation

12
public void paint(Graphics g)
  • Called after init() method completes and start()
    has started
  • Draws stuff on the applet
  • The system hands the applet a Graphics object to
    draw stuff on
  • Automatically recalled whenever the applet needs
    to be repainted
  • uncovering a covered window

13
public void stop()
  • Called when the applet should stop executing
  • When the user leaves the HTML page
  • Perform tasks necessary to suspend applet
  • pausing animations

14
public void destroy()
  • Called when the applet is removed from memory
  • Performs tasks to de-allocate resources

15
repaint()
  • Paint is normally called by the applet container
  • We sometimes change the applets appearance based
    on user input
  • We might like to call paint() directly
  • However, in order to call paint, we have to pass
    it a Graphics object
  • But the container owns the Graphics object
  • repaint() invokes update() which invokes paint
    with the appropriate graphics object

16
Some HTML
  • lthtmlgt
  • ltheadgt
  • lttitlegt Ted Pawlicki's CSC 171 First
    Appletlt/titlegt
  • lt/headgt
  • ltbodygt
  • ltapplet codeFirstApplet.class" width 256
    height 550 gt
  • lt/appletgt
  • lt/htmlgt

17
Some Java
  • import java.awt.
  • import java.applet.Applet
  • public class FirstApplet extends Applet
  • public void paint(Graphics g)
  • g.drawString(Hello Ted!", 20, 40)

18
Cooler Graphics
  • class MyApplet extends Applet public void
    paint(Graphics g) Graphics2D g2
    (Graphics2D)g // add drawing operations. .
    .

19
Graphical Shapes
  • Shape classes Ellipse2D.Double, Line2D.Double,
    etc. 
  • import java.awt.geom.Ellipse2D // no .Double
  • Must construct and draw the shape
    Ellipse2D.Double easterEgg new
    Ellipse2D.Double(5, 10, 15, 20)g2.draw(easterEgg
    )

20
Ellipse
21
Lines
  • Line2D.Double segment new Line2D.Double(x1, x2,
    y1, y2)
  • More object-oriented to use Point2D.Double for
    the end pointsPoint2D.Double from new
    Point2D.Double(x1, y1)Point2D.Double to new
    Point2D.Double(x2, y2)Line2D.Double segment
    new Line2D.Double(from, to)
  • Draw thick linesg2.setStroke(new
    BasicStroke(4.0F)) // 4 pixels

22
Colors
  • Specify red, green, blue between 0.0F and
    1.0FColor magenta new Color(1.0F, 0.0F, 1.0F)
  • Standard colorsColor.blackColor.yellowColor.pin
    k. . .
  • Set color in graphics contextg2.setColor(Color.p
    ink)
  • Then draw or fill shapesg2.fill(easterEgg)

23
FONTS
  • Specify text and base pointg2.drawString("Applet
    ", 50, 100)
  • Font object has
  • face name (Serif, SansSerif, Monospaced, ...)
  • style (Font.PLAIN, Font.BOLD, Font.ITALIC )
  • point size (12 point normal size)
  • g2.setFont(new Font("Serif", Font.BOLD, 36))

24
Baseline
25
Local or Relative Coordinate System
26
The whole object has a location
  • public Car(double x, double y)
  •       xLeft  x
  •       yTop  y
  •  

27
The object is drawn relative to the whole
objects location
  • public void draw(Graphics2D g2)   
  • Rectangle2D.Double body     
  • new Rectangle2D.Double(xLeft, yTop10, 60, 10) 
      
  • Ellipse2D.Double rearTire          
  • new Ellipse2D.Double(xLeft  40, yTop  20, 10, 1
    0)   
  • ..
Write a Comment
User Comments (0)
About PowerShow.com