Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation.

Similar presentations


Presentation on theme: "1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation."— Presentation transcript:

1 1 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

2 2 Applet Use Case Diagram Browser (Firefox) Applet Server Via Internet Scenario: Run Applet

3 3 Applet Class Diagram JApplet AppletName + init( ) : void + start( ) : void + stop( ) : void + destroy( ) : void (Override parent methods) + paint(screen : Graphics) : void Graphics + clearRect( … ) : void + setColor( … ) : void + drawString( … ) : void + drawRect( … ) : void

4 4 Applet Sequence Diagram UserBrowserServer Open Page Obtain HTML file and Applet class file JApplet app = new JApplet(); app.start ( ) app.init ( ) app.paint (Graphics screen) Applet Display via calls to Graphics methods Display Page Close Page app.stop ( ) app.destroy ( ) app = null; (Garbage)

5 5 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()

6 6 Applet Web Page Coding (HTML) Applet Name

7 7 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 }

8 8 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 }

9 9 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); }

10 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 Reference point is at lower left corner Hello World! Visible Area of screen display (looks blank)

11 11 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. Reference point is at upper left corner Hello World!

12 12 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

13 Course Evaluation Need a student volunteer to collect evaluations 13


Download ppt "1 Applets and Course Evaluation Applet Example - Sailing Applet UML Diagrams Applet Web Page Encoding (HTML) Applet Java Coding – Hello World Course Evaluation."

Similar presentations


Ads by Google