Tuesday, October 21, 2014

Draw a Line with pen (Consider mouse pointer is a pen) implement with java program






Save this file draw1.java
……………………………………………………………………………………
import java.awt.*;
import java.applet.*;
public class draw1 extends java.applet.Applet
{
            private int x=0,y=0;
            public boolean mouseDown(Event e,int x1,int y1)
            {                     
                        x=x1;y=y1;
                        return true;       
            }
            public boolean mouseDrag(Event e,int x1,int y1)
            {
                        Graphics g=getGraphics();
                        g.drawLine(x,y,x1,y1);
                        x=x1;y=y1;
                        return true;                   
            }
}
………………………………………………………………………………………
compile it : javac draw1.java
……………………………………………………………………………………….
html file: draw1.html
<applet code ="draw1.class" width=200 height=200></applet>
……………………………………………………………………………………….
Run it: appletviewer draw1.html
……………………………………………………………………………………….
Output:

                   
                          

No comments:

Post a Comment