1 Importjava.awt.*;2 3 Importjavax.swing.*;4 classGstudyextendsjframe{5 6 Private intX1,y1,x2,y2;7 PrivateNewpanel panel=NewNewpanel ();8 PrivateJButton btn=NewJButton ("Clear");9 PublicGstudy ()Ten { OneSettitle ("Interactive Drawing"); ASetBounds (10,10,750,700); -Btn.addactionlistener (Newclearlist ()); - Panel.add (Btn,borderlayout.south); the Add (panel); - } - Public Static voidMain (string[] args) - { +Gstudy frm=Newgstudy (); -Frm.setvisible (true); + frm.setdefaultcloseoperation (jframe.exit_on_close); A } at Private classClearlistImplementsActionListener - { - @Override - Public voidactionperformed (ActionEvent e) { - - panel.paintcomponent (Panel.getgraphics ()); in } - } to Private classMymouselistextendsMouseadapter + { - Public voidmousepressed (MouseEvent e) the { *x1=e.getx (); $y1=e.gety ();Panax Notoginseng } - } the Private classMymousemotionlistextendsMousemotionadapter + { A Public voidmousedragged (MouseEvent e) the { +X2=e.getx (); -Y2=e.gety (); $Graphics g=panel.getgraphics (); $ g.drawline (x1, y1, x2, y2); -x1=x2; -y1=Y2; the } - }Wuyi Private classNewpanelextendsJPanel the { - Wu PublicNewpanel () - { About This. Addmousemotionlistener (Newmymousemotionlist ()); $ This. Addmouselistener (Newmymouselist ()); - } - @Override - protected voidpaintcomponent (Graphics g) { A //TODO auto-generated Method Stub + Super. paintcomponent (g); the } - $}
Java Program for mouse drawing