zoukankan      html  css  js  c++  java
  • applet例子


    //test_applet_package.java
    package test;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.lang.String;

    public class test_applet_package extends Applet implements MouseListener{
        private int width, height;
        private Image off1, img1, img2, img3;
        private Graphics offG;
        private MediaTracker imageTracker;
        private boolean isMouseEnter = false, isPress = false;
        private Color ButtonColor = Color.yellow, lightC, darkC;
        private URL url;
        private AudioClip soundA, soundB;
        private String param;
        public void init() {
            param = new String();
            param = getParameter("soundA");
            if(param == null)
                param = "midiA.mid";
            soundA = getAudioClip(getDocumentBase(), param);
            param = getParameter("soundB");
            if(param == null)
                param = "midiB.mid";
            soundB = getAudioClip(getDocumentBase(), param);
            width = getSize().width;
            height = getSize().height;
            param = getParameter("URL");
            try{
                url = new URL(param);
            } catch (MalformedURLException e){
                
            }
            off1 = createImage(width, height);
            offG = off1.getGraphics();
            imageTracker = new MediaTracker(this);
            param = getParameter("Images1");
            img1 = getImage(getCodeBase(), param);
            imageTracker.addImage(img1, 0);
            param = getParameter("Images2");
            img2 = getImage(getCodeBase(), param);
            imageTracker.addImage(img2, 0);
            param = getParameter("Images3");
            img3 = getImage(getCodeBase(), param);
            imageTracker.addImage(img3, 0);
            try{
                imageTracker.waitForID(0);
            } catch (InterruptedException e){
                
            }
            addMouseListener(this);
        }

        public void start(){
    //        img1 = getImage(getCodeBase(), "button1.gif");
            offG.drawImage(img1, 0, 0, width, height, this);
    //        offG.drawString("hello", width, height);
            repaint();
        }
        public void mouseClicked(MouseEvent e){
            offG.drawString("hello", 20, 20);
            repaint();
        }
        public void mousePressed(MouseEvent e){
    //        img3 = getImage(getCodeBase(), "button3.gif");
            offG.drawImage(img3, 0, 0, width, height, this);
            repaint();
            soundA.play();
        }
        public void mouseReleased(MouseEvent e){
    //        img2 = getImage(getCodeBase(), "button2.gif");
            offG.drawImage(img2, 0, 0, width, height, this);
            repaint();
            soundB.play();
            getAppletContext().showDocument(url);
        }
        public void mouseEntered(MouseEvent e){
    //        img2 = getImage(getCodeBase(), "button2.gif");
            offG.drawImage(img2, 0, 0, width, height, this);
            repaint();
        }
        public void mouseExited(MouseEvent e) {
    //        img1 = getImage(getCodeBase(), "button1.gif");
            offG.drawImage(img1, 0, 0, width, height, this);
            repaint();
        }
        public void paint(Graphics g){
            g.drawImage(off1, 0, 0, width, height, this);
        }
    }


    //test_applet_package.html
    <html>
    <head><title>mouse_event</title></head>
    <body>
    <applet codebase ="."  code = "test.test_applet_package.class" width = 200 height= 200>
    --<param name = "soundA" value="midiA.mid">
    <param name = "soundB" value="midiB.mid">
    <param name = "Images1" value="button1.gif">
    <param name = "Images2" value="button2.gif">
    <param name = "Images3" value="button3.gif">
    <param name = "URL" value="http://www.w3school.com.cn/tags/att_form_method.asp">
    </applet>
    </body>
    </html>

    //mouseevent.html
    <html>
    <head><title>mouse_event</title></head>
    <body>
    <applet code = "mouseevent.class" width = 200 height= 200>
    --<param name = "soundA" value="midiA.mid">
    <param name = "soundB" value="midiB.mid">
    <param name = "Images1" value="button1.gif">
    <param name = "Images2" value="button2.gif">
    <param name = "Images3" value="button3.gif">
    <param name = "URL" value="http://www.w3school.com.cn/tags/att_form_method.asp">
    </applet>
    </body>
    </html>


    applet在嵌入html时,如果applet class在package内,则需要在html文件中设置(如test_applet_package.html所示)

  • 相关阅读:
    django继承User表导致的问题
    Sublime Text 3 鼠标右键快捷选项 简单且高效
    php所有版本
    windows 安装redis
    php玩弄redis基本操作
    PHP 实现文件下载
    PHP 获取客户端的真实IP
    根据标签内个数,显示隐藏《加载更多》
    ubuntu 安装nginx+php+mysql+phpadmin环境
    面试总结
  • 原文地址:https://www.cnblogs.com/charlexu/p/2854334.html
Copyright © 2011-2022 走看看