Painting in Applet

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(Color.red);
  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.

myapplet.html

  1. <html>
  2. <body>
  3. <applet code=“MouseDrag.class” width=“300” height=“300”>
  4. </applet>
  5. </body>
  6. </html>
download this example.
Next TopicDigital Clock In Applet
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Painting in Applet We can perform painting operation in applet by the mouseDragged() method of MouseMotionListener. Example of Painting in Applet: import java.awt.*; import java.awt.event.*; import java.applet.*; public class MouseDrag extends Applet implements MouseMotionListener{ public void init(){ addMouseMotionListener(this); setBackground(Color.red); } public void mouseDragged(MouseEvent me){ Graphics g=getGraphics(); g.setColor(Color.white); g.fillOval(me.getX(),me.getY(),5,5); } public void mouseMoved(MouseEvent me){} } In the above example, getX() and getY() method of MouseEvent is used to get the current x-axis and y-axis. […]

Tags:

....
...