130 likes | 329 Views
Applets and Course Evaluation. Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Reading for this lecture: L&L 2.7-2.9, App G. Applet Use Case Diagram. Server Via Internet. Browser (Firefox). Scenario: Run Applet. Applet.
E N D
Applets and Course Evaluation • Applet Example - Sailing • Applet UML Diagrams • Applet Web Page Encoding (HTML) • Applet Java Coding – Hello World • Reading for this lecture: L&L 2.7-2.9, App G
Applet Use Case Diagram Server Via Internet Browser (Firefox) Scenario: Run Applet Applet
Applet Class Diagram JApplet + init( ) : void + start( ) : void + stop( ) : void + destroy( ) : void AppletName Graphics + clearRect( … ) : void + setColor( … ) : void + drawString( … ) : void + drawRect( … ) : void (Override parent methods) + paint(screen : Graphics) : void
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)
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()
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>
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 }
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 } }
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); }
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! Visible Area of screen display (looks blank) Reference point is at lower left corner
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
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
Review for Exam 3 • Exam 3 is during next class • Practice exam is on-line • Prepare your crib sheet – 1 page, 2 sides • Review the text sections and lecture notes • Ask questions now or via email