All for Joomla All for Webmasters

Painting in Applet

We can perform painting operation in applet by the mouseDragged() method of MouseMotionListener.

Example of Painting in Applet:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. public class MouseDrag extends Applet implements MouseMotionListener{
  5. public void init(){
  6. addMouseMotionListener(this);
  7. setBackground(;
  8. }
  9. public void mouseDragged(MouseEvent me){
  10. Graphics g=getGraphics();
  11. g.setColor(Color.white);
  12. g.fillOval(me.getX(),me.getY(),5,5);
  13. }
  14. public void mouseMoved(MouseEvent me){}
  15. }
In the above example, getX() and getY() method of MouseEvent is used to get the current x-axis and y-axis. The getGraphics() method of Component class returns the object of Graphics.


  1. <html>
  2. <body>
  3. <applet code=“MouseDrag.class” width=“300” height=“300”>
  4. </applet>
  5. </body>
  6. </html>
submit to reddit

Leave a Reply