Applet package

  Abstract: applet package 

  </ Td> </ tr> <tr> <td width="517" height="35" valign="top" class="ArticleTeitle"> Applet writing, images and other resources included in the paper to use the following methods: 

  ClassLoader loader = this.getClass (). GetClassLoader (); 
  / / Image = this.getImage (this.getDocumentBase (), this.getParameter ( "image")); 
  Image = this.getImage (loader.getResource (getParameter ( "image "))); 

  Step 1: compiling source files 

  C: \ java> javac Soundmap.java 

  Step 2: Packing 
  C: \ java> jar cf applet.jar java_parts.gif chirp.au chen / applet 

  The third step: the preparation of html file (please download the source files) 
  Main parts: 








  Below are the applet source: (from java examples) 
  / * 
  * Copyright (c) 2000 David Flanagan All rights reserved. 
  * This code is from the book Java Examples in a Nutshell, 2nd Edition. 
  * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. 
  * You may study, use, and modify it for any non-commercial purpose. 
  * You may distribute it non-commercially as long as you retain this notice. 
  * For a commercial use license, or to purchase the book (recommended), 
  * Visit http://www.davidflanagan.com/javaexamples2. 
  * / 
  Package chen.applet; 
  Import java.applet .*; 
  Import java.awt .*; 
  Import java.awt.event .*; 
  Import java.net .*; 
  Import java.util .*; 

  / ** 
  * A Java applet that simulates a client-side imagemap. 
  * Plays a sound whenever the user clicks on one of the hyperlinks. 
  * / 
  Public class Soundmap extends Applet implements MouseListener ( 
  Protected Image image; / / The image to display. 
  Protected Vector rects; / / A list of rectangles in it. 
  Protected AudioClip sound; / / A sound to play on user clicks in a rectangle 
  Protected ImagemapRectangle highlight; / / Which rectangle is highlighted 

  / * Initialize the applet * / 
  Public void init () ( 
  / / Look up the name of the image, relative to a base URL, and load it. 
  / / Note the use of three Applet methods in this one line. 
  ClassLoader loader = this.getClass (). GetClassLoader (); 

  / / Image = this.getImage (this.getDocumentBase (), this.getParameter ( "image")); 
  Image = this.getImage (loader.getResource (getParameter ( "image "))); 
  / / Lookup and parse a list of rectangular areas and their URLs. 
  / / The convenience routine getRectangleParameter () is defined below. 
  Rects = new Vector (); 
  ImagemapRectangle r; 
  For (int i = 0; (r = getRectangleParameter ( "rect" + i))! = Null; i + +) 
  Rects.addElement (r); 

  / / Look up a sound to play when the user clicks one of those areas. 
  Sound = this.getAudioClip (loader.getResource (getParameter ( "sound "))); 

  / / Specify an "event listener" object to respond to mouse button 
  / / Presses and releases. Note that this is the Java 1.1 event model. 
  This.addMouseListener (this); 
  ) 

  / ** 
  * Called when the applet is being unloaded from the system. 
  * We use it here to "flush" the image we no longer need. This may 
  * Result in memory and other resources being freed more quickly. 
  ** / 
  Public void destroy () (image.flush ();) 

  / ** 
  * To display the applet, we simply draw the image, and highlight the 
  * The current rectangle if any. 
  ** / 
  Public void paint (Graphics g) ( 
  G.drawImage (image, 0, 0, this); 
  If (highlight! = Null) ( 
  G.setColor (Color.red); 
  G.drawRect (highlight.x, highlight.y, 
  Highlight.width, highlight.height); 
  G.drawRect (highlight.x +1, highlight.y +1, 
  Highlight.width-2, highlight.height-2); 
  ) 
  ) 

  / ** 
  * We override this method so that it doesn't clear the background 
  * Before calling paint () No clear is necessary, since paint () overwrites 
  * Everything with an image. Causes less flickering this way. 
  ** / 
  Public void update (Graphics g) (paint (g);) 

  / ** 
  * Parse a comma-separated list of rectangle coordinates and a URL. 
  * Used to read the imagemap rectangle definitions from applet parameters 
  ** / 
  Protected ImagemapRectangle getRectangleParameter (String name) ( 
  Int x, y, w, h; 
  URL url; 
  String value = this.getParameter (name); 
  If (value == null) return null; 

  Try ( 
  StringTokenizer st = new StringTokenizer (value, ","); 
  X = Integer.parseInt (st.nextToken ()); 
  Y = Integer.parseInt (st.nextToken ()); 
  W = Integer.parseInt (st.nextToken ()); 
  H = Integer.parseInt (st.nextToken ()); 
  Url = new URL (this.getDocumentBase (), st.nextToken ()); 
  ) 
  Catch (NoSuchElementException e) (return null;) 
  Catch (NumberFormatException e) (return null;) 
  Catch (MalformedURLException e) (return null;) 

  Return new ImagemapRectangle (x, y, w, h, url); 
  ) 

  / ** Called when a mouse button is pressed * / 
  Public void mousePressed (MouseEvent e) ( 
  / / On button down, check if we're inside one of the rectangles. 
  / / If so, highlight the rectangle, display a message, and play a sound. 
  / / The utility routine findrect () is defined below. 
  ImagemapRectangle r = findrect (e); 
  / / If a rectangle is found, and is not already highlighted 
  If (r! R = null & &! = Highlight) ( 
  Highlight = r; / / Remember which rectangle it is 
  ShowStatus ( "To:" + r.url) / / display its URL in status line 
  Sound.play (); / / play the sound 
  Repaint () / / request a redraw to highlight it 
  ) 
  ) 

  / ** Called when a mouse button is released. * / 
  Public void mouseReleased (MouseEvent e) ( 
  / / If the user releases the mouse button over a highlighted 
  / / Rectangle, tell the browser to display its URL. Also, 
  / / Erase the highlight and clear status 
  If (highlight! = Null) ( 
  ImagemapRectangle r = findrect (e); 
  If (r = highlight) getAppletContext (). ShowDocument (r.url); 
  ShowStatus (""); / / clear the message. 
  Highlight = null; / / forget the highlight 
  Repaint () / / request a redraw 
  ) 
  ) 

  / ** Unused methods of the MouseListener interface * / 
  Public void mouseEntered (MouseEvent e) () 
  Public void mouseExited (MouseEvent e) () 
  Public void mouseClicked (MouseEvent e) () 

  / ** Find the rectangle we're inside * / 
  Protected ImagemapRectangle findrect (MouseEvent e) ( 
  Int i, x = e.getX (), y = e.getY (); 
  For (i = 0; i <rects.size (); i + +) ( 
  ImagemapRectangle r = (ImagemapRectangle) rects.elementAt (i); 
  If (r.contains (x, y)) return r; 
  ) 
  Return null; 
  ) 

  / ** 
  * A helper class. Just like java.awt.Rectangle, but with a URL field. 
  * Note the use of a nested toplevel class for neatness. 
  ** / 
  Static class ImagemapRectangle extends Rectangle ( 
  URL url; 
  Public ImagemapRectangle (int x, int y, int w, int h, URL url) ( 
  Super (x, y, w, h); 
  This.url = url; 
  ) 
  ) 
  ) Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document.getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> <td width="167" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , ,

Releated Java Articles

Comments

Leave a Reply