Friday, July 27, 2012

Simple Pencil Sketch Java Applet Example+Source Code



Java Applets are easy to Implement Web Programs that can be run on the browser without any installation or downloads. The only requirement is that Java Virtual Machine(JVM) Should be installed on the END User Machine. As you may know Java Applets are OS Independent it can be run on any system supporting Java.
Simple Pencil Sketch Applet in Java
The following is a simple Java Example Applet that draws Lines when a user clicks and Drags the mouse.
It uses mouseMoved, mousePressed events to Track mouse clicks by using the MouseAdapter Motion Listener Class.The following is the screenshot of the Applet


The following is the complete Example Source code for the Pencil Sketch Java Applet
 import java.applet.*;  
 import java.awt.event.*;  
 import java.awt.Graphics;  
 import java.awt.Color;  
 //<applet code=LineDraw.class width=400 height=300> </applet>  
 public class LineDraw extends Applet implements MouseMotionListener  
 {int x1,y1,x2,y2;  
  boolean flag=false;  
 public void init()  
 { x1=0; y1=0;  
  addMouseMotionListener(this);  
  addMouseListener(new MouseAdapter(){  
  public void mousePressed(MouseEvent ME)  
  { x1=ME.getX(); y1=ME.getY();}  
  }  
  );  
 }  
 public void mouseMoved(MouseEvent ME)  
 {showStatus(ME.getX()+","+ME.getY()); }  
 public void mouseDragged(MouseEvent ME)  
 {  
  Graphics g=this.getGraphics();  
  x2=ME.getX();  
  y2=ME.getY();  
  g.drawLine(x1,y1,x2,y2);  
  x1=x2; y1=y2;  
  }  
 }  

17 comments:

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.