1.4 Develop an applet that display the position of the mouse at the upper left corner of the applet when it is dragged or moved. Draw a 10x10 pixel rectangle filed with black at the current mouse position.
MouseSquare.java
import java.applet.Applet;import java.awt.*;import java.awt.event.*;/*<applet code=MouseSquare.class height=400 width=800></applet>*/public class MouseSquare extends Applet implements MouseMotionListener{String str="";int x,y;public void init(){addMouseMotionListener(this);}public void paint(Graphics g){g.fillRect(x,y,10,10);g.drawString("X: "+x+" Y:"+y,20,20);showStatus(str);}public void mouseDragged(MouseEvent e){x=e.getX();y=e.getY();str="X="+x+" Y="+y;repaint();}public void mouseMoved(MouseEvent e){x=e.getX();y=e.getY();str="X="+x+" Y="+y;repaint();}}
Try code to see proper output
Happy Coding :)
0 Comments
Post a Comment