Download What Are Applets? - UTRGV Faculty Web

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Pwn2Own wikipedia , lookup

Class (computer programming) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Java syntax wikipedia , lookup

Scala (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
CSCI/CMPE 3326
Object-Oriented Programming in Java
1.  Applet
Dongchul Kim
Department of Computer Science
University of Texas – Rio Grande Valley
your name
What Are Applets?
•  An applet is a special Java program
that can be embedded in HTML
documents.
•  It is automatically executed by
(applet-enabled) web browsers.
•  In Java, non-applet programs are
called applications.
2
your name
Application vs. Applet
•  Application
–  Trusted (i.e., has full access to system resources)
–  Invoked by Java Virtual Machine (JVM, java), e.g.,
java HelloWorld
–  Should contain a main method, i.e.,
public static void main(String[])
•  Applet
–  Not trusted (i.e., has limited access to system resource to
prevent security breaches)
–  Invoked automatically by the web browser
–  Should be a subclass of class java.applet.Applet
3
your name
Life Cycle of an Applet
• 
Four methods in the Applet class give you the framework on which you build any
serious applet:
• 
init: This method is intended for whatever initialization is needed for your applet. It
is called after the param tags inside the applet tag have been processed.
• 
start: This method is automatically called after the browser calls the init method. It
is also called whenever the user returns to the page containing the applet after
having gone off to other pages.
• 
stop: This method is automatically called when the user moves off the page on
which the applet sits. It can, therefore, be called repeatedly in the same applet.
• 
destroy: This method is only called when the browser shuts down normally.
Because applets are meant to live on an HTML page, you should not normally
leave resources behind after a user leaves the page that contains the applet.
• 
paint: Invoked immediately after the start() method, and also any time the applet
yourfrom
name
needs to repaint itself in the browser. The paint() method is actually inherited
Examples
•  HelloWorld.java
public class HelloWord {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
•  HelloWorldApplet.java
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}
5
your name
First Java Applet
•  Java source in HelloWorldApplet.java
import java.awt.*;
import java.applet.Applet;
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
Dimension d = getSize();
g.setColor(Color.BLACK);
g.fillRect(0, 0, d.width, d.height); // paint background
g.setFont(new Font("San-serif", Font.BOLD, 24));
g.setColor(new Color(255, 215,0));
g.drawString("Hello, world!", 60, 40);
g.drawImage(getImage(getCodeBase(), “sample.jpg"),
20, 60, this);
}
}
6
your name
import javax.swing.*;
// Swing GUI classes are defined here.
import java.awt.event.*; // Event handling class are defined here.
public class HelloSwing extends JApplet implements ActionListener {
public void init() {
JButton button = new JButton("Click Me!");
button.addActionListener(this);
getContentPane().add(button);
}
public void actionPerformed(ActionEvent evt) {
String title = "Greetings";
// Shown in title bar of dialog box.
String message = "Hello from the Swing User Interface Library.";
JOptionPane.showMessageDialog(null, message,
title,JOptionPane.INFORMATION_MESSAGE);
}
}
your name
Graphics Coordinate
(0,0)
x
height
y
width
8
your name
Embedding Applet into
HTML
•  HTML source in HelloWorld.html
<!--HelloWorld.html-->
<html>
<head>
<title>HelloWord</title>
</head>
<body>
<center>
<applet code="HelloWorldApplet.class"
width=300 height=350></applet>
</center>
<a href="HelloWorldApplet.java">The source.</a>
</body>
</html>
9
your name
Compiling and Running
•  To compile
javac HelloWorldApplet.java
Produces
HelloWorldApplet.class
•  To run
–  Open page HelloWorld.htmlfrom web
browser or
–  Use appletviewer of JDK
appletviewer HelloWorld.html
10
your name
Elements of Applets
•  Superclass: java.applet.Applet
•  No main method
•  paint method to paint the picture
•  Applet tag: <applet> </applet>
–  code
–  width and height
11
your name