Applets and Course Evaluation

Download Report

Transcript Applets and Course Evaluation

Applets and Course Evaluation
•
•
•
•
•
•
Applet Example - Sailing
Applet UML Diagrams
Applet Web Page Encoding (HTML)
Applet Java Coding – Hello World
Course Evaluation
Reading for this lecture: L&L 2.7-2.9, App G
1
Applet Use Case Diagram
Browser
(Firefox)
Scenario:
Run Applet
Server
Via
Internet
Applet
2
Applet Class Diagram
JApplet
+ init( ) : void
+ start( ) : void
+ stop( ) : void
+ destroy( ) : void
AppletName
(Override parent methods)
+ paint(screen : Graphics) : void
Graphics
+ clearRect( … ) : void
+ setColor( … ) : void
+ drawString( … ) : void
+ drawRect( … ) : void
3
Applet Sequence Diagram
User
Browser
Server
Open Page
Obtain HTML file and Applet class file
Display Page
JApplet app = new JApplet();
app.init ( )
app.start ( )
app.paint (Graphics screen)
Applet Display via calls to Graphics methods
Close Page
app.stop ( )
app.destroy ( )
app = null;
(Garbage)
4
JApplet Class
• JApplet is a class in javax.swing package
• It is a top level GUI container like a JFrame
• Differences:
– Not a standalone application – no main method
– It is instantiated by an Internet browser
– Browser calls the constructor: JApplet( )
– Browser controls the screen display
– No JFrame methods such as:
setSize()
setVisible()
setDefaultCloseOperation()
5
Applet Web Page Coding (HTML)
<HTML>
<HEAD>
<TITLE>Applet Name</TITLE>
</HEAD>
<BODY>
<APPLET code=“AppletName" width=700
height=350>
<PARAM NAME="Author" VALUE="Bob Wilson">
</APPLET>
</BODY>
</HTML>
6
Applet Java Source Code
import javax.swing.*;
public class AppletName extends JApplet
{
public void init() // override
{
// called when browser loads Applet
}
public void start() // override
{
// called when Applet started/restarted
}
7
Applet Java Source Code
public void stop()
// override
{
// called when Applet is stopped
}
public void destroy()
// override
{
// called when browser (tab) is closed
}
public paint(Graphics screen)
// override
{
// called to paint/repaint the screen
// e.g., when part of screen is uncovered
}
}
8
Paint Method for “Hello World”
public void paint (Graphics screen)
{
// look up/study the Graphics class methods
// clear the Applet screen area
screen.clearRect(0,0,this.getWidth(),
this.getHeight());
// pick up a red pen for drawing
screen.setColor(new Color(255,0,0));
// Ubiquitous “Hello World” in upper left
screen.drawString(“Hello World!”, 0, 10);
// and surround it with a rectangle
screen.drawRect(0, 0, 100, 10);
9
}
Graphics Methods
• Look carefully at the reference point for
drawing something with each graphics
method or you can have a problem
Example: drawString(“Hello World”, 0, 0)
• If drawn at 0,0: The text box will be above
the visible area of the screen display
Hello World!
Reference point is
at lower left corner
Visible Area of screen display (looks blank)
10
Graphics Methods
• Use drawString (“Hello World!”, 0, 10)
• But to surround the text with a rectangle,
use drawRect(0, 0, … )
• If drawn at 0,10: The rectangle will be
below the text – not around it.
Hello World!
Reference point is
at upper left corner
11
Applets in Local Files
• You don’t need files stored on a server
• You can store the HTML file and applet
.class file on any local disk and the browser
can open the file as a web page
– The applet operates normally in the page
• You can also run it in DrJava AppletViewer
menu: Tools -> Run Document as Applet
– The applet runs in an otherwise empty frame
• Try it yourself in Lab 10
12
Course Evaluation
• Need a student volunteer to collect evaluations
13