java Write an event-driven program to draw free-hand drawing shape like single line, triangle and rectangle on applet.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
/*
<applet code=prg5
height=600 width=600>
</applet>
*/
public class prg5 extends Applet implements ActionListener
{
Button b1,b2,b3;
int x1,y1,x2,y2,ch=1;
public void init()
{
setBackground(Color.pink);
b1=new Button("Line");
b2=new Button("Triangle");
b3=new Button("Rectangle");
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
x1=0;x2=0;y1=0;y2=0;
x1=me.getX();
y1=me.getY();
}
public void mouseReleased(MouseEvent me)
{
x2=Math.abs(me.getX());
y2=Math.abs(me.getY());
repaint();
}
});
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
ch=1;
}
if(ae.getSource()==b2)
{
ch=2;
}
if(ae.getSource()==b3)
{
ch=3;
}
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{
switch(ch)
{
case 1:
g.drawLine(x1,y1,x2,y2);
break;
case 2:
g.drawLine(x1,y1,x2,y2);
int t=(x2-x1);
g.drawLine(x1-t,y2,x2,y2);
g.drawLine(x1-t,y2,x1,y1);
break;
case 3:
if(x2>x1)
{
g.drawRect(x1,y1,x2-x1,y2-y1);
}
else
{
g.drawRect(x2,y1,x1-x2,y2-y1);
}
break;
}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;
/*
<applet code=prg5
height=600 width=600>
</applet>
*/
public class prg5 extends Applet implements ActionListener
{
Button b1,b2,b3;
int x1,y1,x2,y2,ch=1;
public void init()
{
setBackground(Color.pink);
b1=new Button("Line");
b2=new Button("Triangle");
b3=new Button("Rectangle");
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
x1=0;x2=0;y1=0;y2=0;
x1=me.getX();
y1=me.getY();
}
public void mouseReleased(MouseEvent me)
{
x2=Math.abs(me.getX());
y2=Math.abs(me.getY());
repaint();
}
});
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
ch=1;
}
if(ae.getSource()==b2)
{
ch=2;
}
if(ae.getSource()==b3)
{
ch=3;
}
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g)
{
switch(ch)
{
case 1:
g.drawLine(x1,y1,x2,y2);
break;
case 2:
g.drawLine(x1,y1,x2,y2);
int t=(x2-x1);
g.drawLine(x1-t,y2,x2,y2);
g.drawLine(x1-t,y2,x1,y1);
break;
case 3:
if(x2>x1)
{
g.drawRect(x1,y1,x2-x1,y2-y1);
}
else
{
g.drawRect(x2,y1,x1-x2,y2-y1);
}
break;
}
}
}
Free Hand Drawing in Applet
Reviewed by Unknown
on
8:59 pm
Rating:
No comments: