Applets
Download
1 / 27

APPLETS - PowerPoint PPT Presentation


  • 478 Views
  • Uploaded on

APPLETS CSC 171 FALL 2004 LECTURE 6 APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure Applets Applets are Java prgrams that can be embedded in Hypertext Markup Language (HTML) documents

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'APPLETS' - adamdaniel


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Applets l.jpg

APPLETS

CSC 171 FALL 2004

LECTURE 6


Applets2 l.jpg
APPLETS

  • Graphical Java programs

  • Run inside web browser

  • Platform-neutral

  • Easy deployment--loads when needed

  • Secure


Applets3 l.jpg
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



Slide5 l.jpg
HTML

  • Text and tags:Java is an <i>object-oriented</i> programming language

  • Browser renders the tags:Java is an object-oriented programming language

  • Bulleted list (like this one) is defined by tags<ul><li>. . .</li><li>. . .</li><li>. . .</li></ul>

  • Use &lt;&gt; for <> symbols


Simple html l.jpg
Simple HTML

<html>

<head>

<title> Ted Pawlicki's CSC 171 HTML</title>

</head>

<body>

Hello Ted!

</body>

</html>


Applet methods l.jpg
Applet Methods

  • Applets have no “main()”

  • Rather, it has key methods that deal with the unique situation of applets


Key applet methods l.jpg
Key Applet Methods

  • init

  • start

  • paint

  • stop

  • destroy


Key applet methods9 l.jpg
Key Applet Methods

  • public void init()

  • public void start()

  • public void paint(Graphics g)

  • public void stop()

  • public void destroy()


Public void init l.jpg
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)


Public void start l.jpg
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


Public void paint graphics g l.jpg
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


Public void stop l.jpg
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


Public void destroy l.jpg
public void destroy()

  • Called when the applet is removed from memory

  • Performs tasks to de-allocate resources


Repaint l.jpg
repaint()

  • Paint is normally called by the applet container

  • We sometimes change the applet’s 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


Some html l.jpg
Some HTML

<html>

<head>

<title> Ted Pawlicki's CSC 171 First Applet</title>

</head>

<body>

<applet code=“FirstApplet.class" width = 256 height = 550 >

</applet>

</html>


Some java l.jpg
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);

}

}


Cooler graphics l.jpg
Cooler Graphics

class MyApplet extends Applet {public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; // add drawing operations. . .}

}


Graphical shapes l.jpg
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)



Lines l.jpg
Lines

  • Line2D.Double segment = new Line2D.Double(x1, x2, y1, y2);

  • More object-oriented to use Point2D.Double for the end points:Point2D.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 lines:g2.setStroke(new BasicStroke(4.0F)); // 4 pixels


Colors l.jpg
Colors

  • Specify red, green, blue between 0.0F and 1.0FColor magenta = new Color(1.0F, 0.0F, 1.0F)

  • Standard colorsColor.blackColor.yellowColor.pink. . .

  • Set color in graphics context:g2.setColor(Color.pink);

  • Then draw or fill shapesg2.fill(easterEgg);


Fonts l.jpg
FONTS

  • Specify text and base point:g2.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));




The whole object has a location l.jpg
The whole object has a location

public Car(double x, double y){

      xLeft = x;

      yTop = y;

 }


The object is drawn relative to the whole object s location l.jpg
The object is drawn “relative” to the whole object’s location

public void draw(Graphics2D g2)   {

Rectangle2D.Double body    = 

new Rectangle2D.Double(xLeft, yTop+10, 60, 10);   

Ellipse2D.Double rearTire         = 

new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);   

…..