java Painting in Applet

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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.   
  6. public void init(){  
  7. addMouseMotionListener(this);  
  8. setBackground(Color.red);  
  9. }  
  10.   
  11. public void mouseDragged(MouseEvent me){  
  12. Graphics g=getGraphics();  
  13. g.setColor(Color.white);  
  14. g.fillOval(me.getX(),me.getY(),5,5);  
  15. }  
  16. public void mouseMoved(MouseEvent me){}  
  17.   
  18. }  

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.

myapplet.html

  1. <html>  
  2. <body>  
  3. <applet code="MouseDrag.class" width="300" height="300">  
  4. </applet>  
  5. </body>  
  6. </html>