Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 434|回复: 0

[Java基础知识]applet打包

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-9-30 23:47:54 | 显示全部楼层 |阅读模式
    写Applet时,图像和其它资源文件要用如下方法载入:

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

      
      
    第一步:编译源文件

    C:java>javac    Soundmap.java

    第二步:打包
    C:java>jar cf applet.jar java_parts.gif chirp.au chen/applet

    第三步:编写HTML文件(源文件请下载)
    主要部分:
    <APPLET code="chen/applet/Soundmap.class"
    arcHive="applet.jar"  width=288 height=191>
       <PARAM name="image" value="java_parts.gif">
       <PARAM name="sound" value="chirp.au">
       <PARAM name="rect0" value="114,90,151,33,#p1">
       <PARAM name="rect1" value="114,123,151,33,#p2">
       <PARAM name="rect2" value="114,156,151,33,#p3">
    </APPLET>
      
      
       
       
         
       

       
       
      

    下面是applet的源程序:(来自java实例)
    /*
      * 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
          * 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 != null && r != 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).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
                   
                      











    源码下载:http://file.javaxxz.com/2014/9/30/234754391.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-11 04:21 , Processed in 0.382838 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表