标签:ima 图片 graphics extends event extend agg col ted
题目是:用Aplet创建一个小程序,使得当你的鼠标经过图片时,放歌~
其中,补充知识:1、MouseMotionListener这个和MouseListener这两个监听器要了解以下
2、Aplet这个也要了解一下
下面贴代码:
//【例7-7】 鼠标事件示例。 //程序清单7-7: MouseTest.java import java.awt.*; import java.applet.*; import java.awt.event.*; public class songs extends Applet implements MouseMotionListener { AudioClip audioClip; String text = ""; int x; int y; Image picture; //加载图片 public void init() { picture = getImage(getCodeBase(), "image.jpg"); x = 0; y = 0; addMouseMotionListener(this); } //重写 //使得显示的文本随鼠标移动而移动 public void paint(Graphics g) { g.drawImage(picture, 0, 0, this); g.drawString(text, x, y); } //核心(? public void mouseMoved(MouseEvent e) { x = e.getX(); y = e.getY(); audioClip = getAudioClip(getCodeBase(), "Sample.wav"); text = "开始放歌辽~~~"; audioClip.loop(); repaint(); } // public void mouseReleased(MouseEvent e) // { // text = " "; // repaint(); // } //必须有这个,这样子才能运行,都要重写 public void mouseDragged(MouseEvent arg0) { // TODO Auto-generated method stub } // public void mouseEntered(MouseEvent e) { // } // // public void mouseExited(MouseEvent e) { // } // // public void mouseClicked(MouseEvent e) { // } }
标签:ima 图片 graphics extends event extend agg col ted
原文地址:https://www.cnblogs.com/sakuraXiYue/p/10011166.html