//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所示)