Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.*; Public classDrawLineextendsJAppletImplementsmouselistener,mousemotionlistener{intX1,y1,x2,y2;//int screenx,screeny;//Dimension screen;Stroke sk=NewBasicstroke (2.0f); Public voidinit () {//Screen=toolkit.getdefaulttoolkit (). Getscreensize();//screenx= (int) screen.getwidth ();//screeny= (int) screen.getheight ();//SetBounds ((screenx-400)/2, (screeny-700)/2,700); Addmouselistener ( This); Addmousemotionlistener ( This); } Public voidPaint (Graphics g) {graphics2d GG=(graphics2d) G; Gg.setstroke (SK); Gg.drawline (x1, y1, x2, y2); } @Override Public voidmouseclicked (MouseEvent arg0) {//TODO auto-generated Method Stub} @Override Public voidmouseentered (MouseEvent arg0) {//TODO auto-generated Method Stub} @Override Public voidmouseexited (MouseEvent arg0) {//TODO auto-generated Method Stub } Public voidmousepressed (MouseEvent arg0) {//X1=arg0.getx (); //y1=arg0.gety ();} @Override Public voidmousereleased (MouseEvent arg0) {//TODO auto-generated Method Stub} @Override Public voidmousedragged (MouseEvent arg0) {x2=Arg0.getx (); Y2=arg0.gety (); X1=x2; Y1=Y2; Repaint (); } @Override Public voidmousemoved (MouseEvent arg0) {point P=NewPoint (X2,y2); SYSTEM.OUT.PRINTLN (P); //System.out.println ("Y" +this.y2); }}
The most basic application of applets--drawing lines on applets