Upload
vbjj
View
3
Download
0
Tags:
Embed Size (px)
DESCRIPTION
good applet ppt
Citation preview
Object
Component
ButtonTextComponent
Checkbox ChoiceContainer
Label
TextArea TextField
ScrollBar Canvas
Panel ScrollPaneWindow
Part of the AWT Class Hierarchy
Dialog FrameApplet
List
Client
Web Server
Internet
TCP/IP(HTTP)
TCP/IP(HTTP)
Java Security ModelJava Security Model
TCP/IP(socket)
TCP/IP(socket)
Sambar ServerSambar ServerHTML files(Applet Tags)HTML files(Applet Tags)
Applets Applets
Netscape(JVM)Netscape(JVM) Loads HTML fileLoads HTML file Loads Java AppletLoads Java Applet
Java Security ModelJava Security Model
•Restricted Access to Local File SystemRestricted Access to Local File System
•Restricted Access to Outside ServersRestricted Access to Outside Servers
•Restricted Access to ThreadsRestricted Access to Threads
Java AppletJava Applet
public class className extends java.applet.Applet {
public void init() {}
public void paint(Graphics g) {}}
Java AppletJava Applet
Applets will have the following methods:Applets will have the following methods:
publicpublic voidvoid init ( ) { init ( ) { //in here you will //in here you will
//set fonts –may be done in//set fonts –may be done in paint paint instead instead
//set initial background and foreground collors//set initial background and foreground collors
//initialize attributes (if other than their defaults)//initialize attributes (if other than their defaults)
//add components and/or event listeners//add components and/or event listeners
}}
publicpublic voidvoid actionListenerType (EventType e) { actionListenerType (EventType e) {
////needed for event driven applets – may occur in a component subclass
//do some action that will change the applet display//do some action that will change the applet display
repaint();repaint();
}}
publicpublic voidvoid paint(Graphics g) { paint(Graphics g) {
//only implicitly called by repaint//only implicitly called by repaint
}}
Additional Applet MethodsAdditional Applet Methods
publicpublic voidvoid start ( ) { start ( ) {
//called after init and before the first invocation of paint//called after init and before the first invocation of paint
//if applet is stopped and restarted, start is called//if applet is stopped and restarted, start is called
//Netscape calls start and stop when the browser window is resized//Netscape calls start and stop when the browser window is resized
//IE and appletviewer don’t//IE and appletviewer don’t
}}
publicpublic voidvoid stop() { stop() {
//called when user leaves page containing the applet//called when user leaves page containing the applet
}}
publicpublic voidvoid destroy() { destroy() {
//called when applet is about to be permanently destroyed –//called when applet is about to be permanently destroyed –
//when browser shuts down or when ir removes the applet from memory//when browser shuts down or when ir removes the applet from memory
//Called by IE when user leaves the page containing the applet//Called by IE when user leaves the page containing the applet
}}
import java.awt.Graphics; import java.awt.Font; import java.awt.Color; import java.applet.Applet;
public class HelloWorldApplet1 extends Applet {
Font f = new Font("TimesRoman", Font.BOLD, 36); String name,greeting;
public void init() { name = ”Roger L. Norton"; greeting = new String("Hello " + name + "!"); }
public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString(greeting, 5, 40); }}
HelloWorldApplet1.javaHelloWorldApplet1.java
Java HTML APPLET Tag Java HTML APPLET Tag
<APPLET CODE= “…” WIDTH = xxx HEIGHT= yyy …><APPLET CODE= “…” WIDTH = xxx HEIGHT= yyy …>
……..</APPLET></APPLET>
Options:Options:
CODEBASECODEBASE
ALIGNALIGN
HSPACEHSPACE
VSPACEVSPACE
NAMENAME
ARCHIVEARCHIVE Designates an archive of classes to preloadDesignates an archive of classes to preload
Designates the applet alignment choice Designates the applet alignment choice (Left, Right, Top, Bottom, Middle)(Left, Right, Top, Bottom, Middle)
Designates the empty space at the right and left of the appletDesignates the empty space at the right and left of the applet
Designates the empty space above and below the appletDesignates the empty space above and below the applet
Designates the name of the appletDesignates the name of the applet
Designates the base URLDesignates the base URL
<HTML> <HEAD> <TITLE> Hello World Example! </TITLE> </HEAD> <BODY> <H2> Event Driven Software Java Lecture 2 </H2> <P> <APPLET code="HelloWorldApplet1.class" width="500" height="50"> Hello World! </APPLET> </P> <P> <A href="HelloWorldApplet1.java">The Source</A> </P> </BODY></HTML>
Java HTML APPLET Tag Java HTML APPLET Tag
<APPLET CODE= “…” WIDTH = xxx HEIGHT= yyy …><APPLET CODE= “…” WIDTH = xxx HEIGHT= yyy …>
Some text to be displayed if browser does not support javaSome text to be displayed if browser does not support java
<PARAM NAME = “…” VALUE = “…”><PARAM NAME = “…” VALUE = “…”>
<PARAM NAME = “…” VALUE = “…”><PARAM NAME = “…” VALUE = “…”>
</APPLET></APPLET>
<HTML> <HEAD> <TITLE> Hello World Example! </TITLE> </HEAD> <BODY> <H2> Event Driven Software Java Lecture 1 </H2> <P> <APPLET code="HelloWorldApplet.class" width="500" height="50"> <PARAM name="name" value="Roger L. Norton"> Hello World! </APPLET> </P> <P> <A href="HelloWorldApplet.java">The Source</A> </P> </BODY></HTML>
HellowWorldApplet2.htmlHellowWorldApplet2.html
<HTML> <HEAD> <TITLE> Hello World Example! </TITLE> </HEAD> <BODY> <H2> Event Driven Software Java Lecture 1 </H2> <P> <APPLET code="HelloWorldApplet.class" width="500" height="50"> Hello World! </APPLET> </P> <P> <A href="HelloWorldApplet.java">The Source</A> </P> </BODY></HTML>
HelloWorldApplet3.htmlHelloWorldApplet3.html
import java.awt.Graphics; import java.awt.Color;
public class ColorBoxes extends java.applet.Applet {
void delay(int time){ for(int i =1;i<=time;i++); }
public void paint(Graphics g) { int rval, gval, bval; String tempDelay = getParameter("delay"); int finalDelay = Integer.parseInt(tempDelay);
for (int k = 1;k<=100;k++){ for (int j = 30; j < (size().height -25); j += 30) for (int i = 5; i < (size().width -25); i += 30) { rval = (int)Math.floor(Math.random() * 256); gval = (int)Math.floor(Math.random() * 256); bval = (int)Math.floor(Math.random() * 256);
g.setColor(new Color(rval,gval,bval)); g.fillRect(i, j, 25, 25); g.setColor(Color.black); g.drawRect(i, j, 25, 25); }; delay(finalDelay); }; };}
An Interesting ExampleAn Interesting Example
<HTML> <HEAD> <TITLE> Colored Boxes </TITLE> </HEAD> <BODY> <H2> Event Driven Software Example #3 </H2> <H2> Colored Boxes </H2> <P> <APPLET code="ColorBoxes.class" width="600" height="300"> <PARAM name="delay" value="10000000"> </APPLET> </P> <P> <A href="ColorBoxes.java">The Source</A> </P> </BODY></HTML>
ColorBoxes.htmlColorBoxes.html
Events in JavaEvents in Java
In Java most objects within the AWT class hierarchy have the ability to observe events In Java most objects within the AWT class hierarchy have the ability to observe events occurring with respect to itself (e.g. a occurring with respect to itself (e.g. a buttonbutton can observe that it has been pressed, or a can observe that it has been pressed, or a textbox textbox can detect that it has been written to). The object can then send this can detect that it has been written to). The object can then send this information to any other object that has indicated it is interested in knowing about information to any other object that has indicated it is interested in knowing about such happeningssuch happenings. . These notified objects can then react to these events.These notified objects can then react to these events.
ButtonButton ButtonPanelButtonPanel
ActionEventActionEvent
addActionListener(this)addActionListener(this)
actionPerformed(ActionEvent)actionPerformed(ActionEvent)
buttonPressedbuttonPressedgetObjecct()getObjecct()
Events in JavaEvents in Java
ButtonButton ButtonPanelButtonPanel
ActionEventActionEvent
addActionListeneraddActionListener(this)(this)
(ActionEvent actionPerformed)(ActionEvent actionPerformed)
buttonPressedbuttonPressedgetObjecctgetObjecct()()
A listener object:A listener object:
implements a “listener interface”implements a “listener interface”
can be registered as a listener to various event sourcescan be registered as a listener to various event sources
An event source:An event source:
can register listener objectcan register listener object
can send listeners “event objects”can send listeners “event objects”
An event object:An event object:
contains information about the event that occurredcontains information about the event that occurred
can be queried to receive this informationcan be queried to receive this information
Event Listeners for Mouse and Keyboard EventsEvent Listeners for Mouse and Keyboard Events
MouseListenerMouseListener
public void mouseEntered(MouseEvent e)
public void mouseExited (MouseEvent e)
public void mousePressed (MouseEvent e)
public void mouseReleased (MouseEvent e)
public void mouseClicked (MouseEvent e)
KeyListenerKeyListener
public void keyPressed(KeyEvent e)
public void keyReleased(keyEvent e)
public void keyTyped(keyEvent e)
MouseMotionListenerMouseMotionListener
public void mouseMoved (MouseEvent e)
public void mouseDragged (MouseEvent e)
Pressed and released at same location
Keyboard and Mouse EventsKeyboard and Mouse Events
AWTEvent contains four important methods:
consume //delete the event
isConsumed //returns boolean – true if consumed by another
//listener on same source
getID //and int represening the event type
getSource //the Object that the event came from
KeyEvent adds the following methods:
getKeyChar //returns character typed
setKeyChar //replace the character with a different one
getKeyCode //returns an integer value which can be passedto
//getKeyText
isActionKey //differentiates function and arrow keys from normal key
getModifiers setModifiers //retrieve/replace modifier keys
isAltDown, isShiftDown, isControlDown
Keyboard and Mouse EventsKeyboard and Mouse Events
MouseEventMouseEvent methods methods:
getX, getY //determine location of the mouse event
getClickCount //differentiates between single and double clicks
getModifiers //to determine which button was pressed
Event Handling ExamplesEvent Handling Examples
Clicks Example 1Clicks Example 1
Clicks Example 2Clicks Example 2
Clicks Example 3Clicks Example 3
Colors ExampleColors Example
Text ExampleText Example