Sunteți pe pagina 1din 9

All applets are subclasses of Applet. All applets must import java.applet and java.awt packages.

Applets are not executed by the consolebased Java run-time interpreter. They are executed by either a Web browser or an applet viewer. Execution of an applet does not begin at main( ). User I/O is not accomplished with Javas stream I/O classes.

// A minimal applet. import java.awt.*; import java.applet.*; public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Java makes applets easy",20,20); } } /* <applet code=FirstApplet width=200 height=60> </applet> */
3

Step-1: Edit a Java source file. Step-2: Compile your program. Step-3: Run it. (There are 2 ways to run an applet)
Executing the applet within a Java-compatible Web

browser.(By locating html part in separate html file) Using an applet viewer, such as the standard SDK tool, appletviewer.(C:\>appletviewer FirstApplet.html [or] C:\>appletviewer FirstApplet.java)
4

init()
start()
do some work

init ( )
Called when applet is loaded onto

start ( )

users machine. Prep work or onetime-only work done at this time. (page called up). Called every time applet becomes visible.

Called when applet becomes visible

stop() destroy()

stop ( )

Called when applet becomes hidden

destroy ( )

(page loses focus).


shuts down.

Guaranteed to be called when browser


5

Needed if you do any drawing or painting other than just using standard GUI Components Any painting you want to do should be done here, or in a method you call from here Call repaint( ) when you have changed something and want your changes to show up on the screen
6

A Graphics is something you can paint on Hello

g.drawString(Hello,20,20);

g.drawRect(x, y, width, height);

g.fillRect(x, y, width, height);


g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red);

System.out.println(String s) showStatus(String s) URL getCodeBase( ) URL getDocumentBase( ) void setBackground(Color)

void setForeground(Color)

< APPLET [CODEBASE = codebaseURL] CODE = appletFile [ALT = alternateText] [NAME = appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels] > [< PARAM NAME = AttributeName VALUE = AttributeValue>] [< PARAM NAME = AttributeName2 VALUE = AttributeValue>] . . . [HTML Displayed in the absence of Java] </APPLET>
9

S-ar putea să vă placă și