Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.

Similar presentations


Presentation on theme: "1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded."— Presentation transcript:

1 1 Java Applet Basics Chapter Eight

2 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded in a Web page using an HTML tag l

3 3 When you load a web page with an applet What happens? l browser downloads the applet and executes it on the local system. –Netscape 4.0 Communicator = 32 bit very good –Netscape 3.0 Gold not as good = 16 bit –Viewer that comes with the text ~ 3.0 l What you want is 32 bit l Microsoft Explorer J++ should be great

4 4 Why run on a browser? l An existing window l an event-handling and graphics context l surrounding user interface (UI) –java applications can create all this but they don’t require it (wait to chapter 14 applet- like graphics)

5 5 Applet Restrictions l Can not read/write to reader’s file system EXCEPT specific directories (user defines default EMPTY) l Can not communicate with server other that the one that it was originally stored on (May be configurable) l Can Not run any program on reader system l Can not load programs native to local platforms, shared libraries, DLLs.

6 6 Applet signiture l public class myClass extends java.applet.Applet {... }

7 7 Five Major Applet Activities l Initialization public void init( ) {... } l Starting public void start( ) {... } l Stopping public void stop( ) {... } l Destroying public void destroy ( ) {...} l Painting l public void paint(Graphics g) {... } l Painting is how an applet does stuff on screen must import java.awtGraphics

8 8 Hello Again Applet l import java.awt.Graphics; l import java.awt.Font; l import java.awt.Color; l public class HelloAgainApplet extends l java.applet.Applet { l Font f = new Font(“TimesRoman”, Font.BOLD,36);

9 9 l public void paint(Graphics g ) { –g.setfont(f); –g.setcolor(Color.red); –g.drawString(“Hello again!”, 5, 50); –}// position 5, 50 l }// 0,0 top left

10 10 List 8.2 An applet l l This page has an applet l l My second java applet says: l

11 11 l l There would be an applet here if your browser supported Java. l

12 12 Just a note the class file has got to be in the same directory as the HTML file- So when running your authors applets C:\tyjava\applets\...is where they normally reside CODEBASE is used to specify applets in specific directory

13 13 To View your applet l write your applet in Java - save it l compile a class should result l write the HTML code include –tags l invoke browser –also known as viewing a file YOU key in the name of the HTML file - include path –If satisfied put it on the WWW (HTML AND compiled class files)

14 14 Tag How to ALIGN on a web page Tag How to ALIGN on a web page l LEFTBOTTOM l RIGHT ABSBOTTOM l TOP// example: l TEXTTOP//ALIGN=TEXTTOP l MIDDLE// aligns applet with top of l ABSMIDDLE // tallest text in the line l BASELINE// See Fig 8.4 “ALIGN

15 15 YOU CONTROL THE HORIZONTAL YOU CONTROL THE VERTICAL: l... l ALIGN LEFT VSPACE=50 HSPACE=10> l THAT’S 50 DOWN FROM THE LEFT TOP OF CRT AND 10 (IN PIXELS)

16 16 CODE and CODEBASE or Just where did I store that Applet? l <APPLET CODE=“myclass.class” CODEBASE=“C:\tyjava\applets\frogj ump” l say classes was in C:\tyjava\applets l OR remember to store you HTML code same place l CODE is just the name of the class that holds the applet

17 17 Passing Parameters to Applets You Need: l A special parameter tag in HTML l Code in your applet to parse those parameters l l A java applet appears here - We hope...

18 18 List 8.3 and List 8.4 use this l import java.awt.Graphics; l import java.awt.Font; l import.awt.Color; l public class MoreHelloApplet extends –java.applet.Applet { l Font f = new Font(“TimesRoman”, Font.BOLD, 36); l String name;

19 19 l public void init( ) { –name = getParameter(“name”); –if (name = = null) »name = “Laura”; »name = “Hello “ + name + “!”; l } l public void paint(Graphics g) { –g.setFont(f); –g.setColor(Color.red); –g.drawString(name, 5, 50); –}

20 20 l <HTML l l hello! l l Hello to whoever you are! l


Download ppt "1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded."

Similar presentations


Ads by Google