1 / 22

Applets

Applets. Part of the AWT Class Hierarchy. Object. Component. Button. Checkbox. Choice. List. Label. Canvas. TextComponent. Container. ScrollBar. TextArea. TextField. Panel. Window. ScrollPane. Applet. Dialog. Frame. Netscape(JVM) Loads HTML file Loads Java Applet.

dane
Download Presentation

Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applets

  2. Part of the AWT Class Hierarchy Object Component Button Checkbox Choice List Label Canvas TextComponent Container ScrollBar TextArea TextField Panel Window ScrollPane Applet Dialog Frame

  3. Netscape(JVM) Loads HTML file Loads Java Applet Java Security Model Sambar Server HTML files(Applet Tags) Applets

  4. Java Security Model • Restricted Access to Local File System • Restricted Access to Outside Servers • Restricted Access to Threads

  5. Java Applet publicclass className extends java.applet.Applet {publicvoid init() {}publicvoid paint(Graphics g) {}}

  6. Java Applet Applets will have the following methods: publicvoid init ( ) { //in here you will //set fonts –may be done in paint instead //set initial background and foreground collors //initialize attributes (if other than their defaults) //add components and/or event listeners } publicvoid actionListenerType (EventType e) { //needed for event driven applets – may occur in a component subclass //do some action that will change the applet display repaint(); } publicvoid paint(Graphics g) { //only implicitly called by repaint }

  7. Additional Applet Methods publicvoid start ( ) { //called after init and before the first invocation of paint //if applet is stopped and restarted, start is called //Netscape calls start and stop when the browser window is resized //IE and appletviewer don’t } publicvoid stop() { //called when user leaves page containing the applet } publicvoid destroy() { //called when applet is about to be permanently destroyed – //when browser shuts down or when ir removes the applet from memory //Called by IE when user leaves the page containing the applet }

  8. HelloWorldApplet1.java import java.awt.Graphics;import java.awt.Font;import java.awt.Color;import java.applet.Applet;publicclass HelloWorldApplet1 extends Applet { Font f =new Font("TimesRoman", Font.BOLD,36); String name,greeting;publicvoid init() { name =”Roger L. Norton"; greeting =new String("Hello "+ name +"!"); }publicvoid paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString(greeting,5,40); }}

  9. Java HTML APPLET Tag <APPLET CODE= “…” WIDTH = xxx HEIGHT= yyy …> …. </APPLET> Options: CODEBASE ALIGN HSPACE VSPACE NAME ARCHIVE Designates the base URL Designates the applet alignment choice (Left, Right, Top, Bottom, Middle) Designates the empty space at the right and left of the applet Designates the empty space above and below the applet Designates the name of the applet Designates an archive of classes to preload

  10. <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><Ahref="HelloWorldApplet1.java">The Source</A> </P> </BODY></HTML>

  11. Java HTML APPLET Tag <APPLET CODE= “…” WIDTH = xxx HEIGHT= yyy …> Some text to be displayed if browser does not support java <PARAM NAME = “…” VALUE = “…”> <PARAM NAME = “…” VALUE = “…”> </APPLET>

  12. HellowWorldApplet2.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"> <PARAM name="name" value="Roger L. Norton"> Hello World! </APPLET> </P> <P><Ahref="HelloWorldApplet.java">The Source</A> </P> </BODY></HTML>

  13. HelloWorldApplet3.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><Ahref="HelloWorldApplet.java">The Source</A> </P> </BODY></HTML>

  14. An Interesting Example import java.awt.Graphics;import java.awt.Color;publicclass ColorBoxes extends java.applet.Applet {void delay(int time){for(int i =1;i<=time;i++); }publicvoid 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); }; };}

  15. ColorBoxes.html <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><Ahref="ColorBoxes.java">The Source</A> </P> </BODY></HTML>

  16. Button ButtonPanel ActionEvent Events in Java In Java most objects within the AWT class hierarchy have the ability to observe events occurring with respect to itself (e.g. a button can observe that it has been pressed, or a textbox 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 such happenings. These notified objects can then react to these events. actionPerformed(ActionEvent) addActionListener(this) getObjecct() buttonPressed

  17. Button ButtonPanel ActionEvent Events in Java An event source: can register listener object can send listeners “event objects” A listener object: implements a “listener interface” can be registered as a listener to various event sources (ActionEvent actionPerformed) addActionListener(this) getObjecct() buttonPressed An event object: contains information about the event that occurred can be queried to receive this information

  18. Pressed and released at same location Event Listeners for Mouse and Keyboard Events MouseListener public void mouseEntered(MouseEvent e) publicvoid mouseExited (MouseEvent e) publicvoid mousePressed (MouseEvent e) publicvoid mouseReleased (MouseEvent e) publicvoid mouseClicked (MouseEvent e) MouseMotionListener publicvoid mouseMoved (MouseEvent e) publicvoid mouseDragged (MouseEvent e) KeyListener public void keyPressed(KeyEvent e) publicvoid keyReleased(keyEvent e) publicvoid keyTyped(keyEvent e)

  19. Keyboard 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

  20. Keyboard and Mouse Events MouseEvent methods: getX, getY //determine location of the mouse event getClickCount //differentiates between single and double clicks getModifiers //to determine which button was pressed

  21. Event Handling Examples Clicks Example 1 Clicks Example 2 Clicks Example 3 Colors Example Text Example

  22. Testing an Applet Construct an HTML file that will locate your applet in a region of an html page. Run appletviewer with the name of the html file where you call your applet.

More Related