First, the purpose and requirements of the experiment
Students are required to develop the ability to design web browser-based applets by experimenting with learning and understanding Java applets in classroom learning content, and by fostering students to combine Java applet-related knowledge points, including Java and low-level event processing models.
Experimental content:
Design and write an applet applets and corresponding HTML pages that can be manipulated by the mouse, observe the execution of the applet, and test the effect of the user interaction of the program mouse.
Second, the experimental code
Package Think;import Java.applet.applet;import Java.applet.audioclip;import java.awt.graphics;import java.awt.Image ; Import Java.awt.event.mouseevent;import Java.awt.event.mouselistener;public class Java_exp_02applet extends Applet Implements Mouselistener{audioclip sound;private int mousex =25;private int mousey =25; @Overridepublic void Init () {Super . Init (); sound = getAudioClip (GetCodeBase (), "think/think.au"); Addmouselistener (this);} @Overridepublic void Start () {//TODO auto-generated method Stubsuper.start (); Sound.loop ();} @Overridepublic void Stop () {//TODO auto-generated method Stubsuper.stop (); Sound.stop ();} @Overridepublic void Paint (Graphics g) {//TODO auto-generated method Stubsuper.paint (g); g.DrawString ("Mouse position", MouseX, mousey);} public void mousepressed (MouseEvent evt) {mousex= evt.getx (); mousey = Evt.gety (); repaint ();} @Overridepublic void mouseclicked (MouseEvent e) {//TODO auto-generated method stub} @Overridepublic void Mousereleased ( MouseEvent e) {//TODO Auto-geneRated method stub} @Overridepublic void Mouseentered (MouseEvent e) {//TODO auto-generated method stub} @Overridepublic voi D mouseexited (MouseEvent e) {//TODO auto-generated method stub}}<strong></strong>
third, the interface
Iv. Summary of the experiment
The whole idea:
* After clicking on the mouse, output a phrase "mouse position" in the location of the click. Because of the operation of listening to the mouse, so the interface of the way implements MouseListener, so must implement mouse several methods, in the mousepressed () The position of the coordinates is changed in the method, the repaint() function is called, and the position of the mouse is updated .
The two private variables of the mouse position, initialized in init().
Problems encountered and how to resolve them:
* after running in Eclipse, the browser does not open, and later found that the HTML applet closed tag was incorrectly written.
* After running in Chrome , you need to install the plugin automatically, otherwise it can't be supported.
* Initially want to insert the picture, because I put Java files and pictures in a directory, so, directly set the path to the relative path "thinkimage.png", The discovery does not show up. Re-read the Javac source and add a layer of directory "think/thinkimage.png", success.
* Extra want to put a song, later only to know, AudioClip only support au format music.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Njupt applet design for Java language applets