Program:
import java.awt.*;
import java.awt.event.*;
class ex9 extends Frame implements ActionListener
{
Canvas c;
Button b;
int x1,x2,y1,y2;
ex9()
{
super("Drawing");
setLayout(null);
setSize(500,500);
setVisible(true);
c=new Canvas();
c.setBackground(Color.white);
b=new Button("Clear");
c.setBounds(50,50,300,300);
add(c);
b.setBounds(200,400,50,20);
add(b);
b.addActionListener(this);
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent ae)
{
x1=ae.getX();
y1=ae.getY();
c.getGraphics().drawLine(x1, y1, x1, y1);
}
}
);
c.addMouseMotionListener(new MouseAdapter()
{
public void mouseDragged(MouseEvent ae)
{
x2=ae.getX();
y2=ae.getY();
c.getGraphics().drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
c.repaint();
}
public static void main(String s[])
{
ex9 ob=new ex9();
}
}
Output:
nn@linuxmint ~/Desktop/java 7 $ javac ex9.java
nn@linuxmint ~/Desktop/java 7 $ java ex9
nn@linuxmint ~/Desktop/java 7 $
import java.awt.*;
import java.awt.event.*;
class ex9 extends Frame implements ActionListener
{
Canvas c;
Button b;
int x1,x2,y1,y2;
ex9()
{
super("Drawing");
setLayout(null);
setSize(500,500);
setVisible(true);
c=new Canvas();
c.setBackground(Color.white);
b=new Button("Clear");
c.setBounds(50,50,300,300);
add(c);
b.setBounds(200,400,50,20);
add(b);
b.addActionListener(this);
c.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent ae)
{
x1=ae.getX();
y1=ae.getY();
c.getGraphics().drawLine(x1, y1, x1, y1);
}
}
);
c.addMouseMotionListener(new MouseAdapter()
{
public void mouseDragged(MouseEvent ae)
{
x2=ae.getX();
y2=ae.getY();
c.getGraphics().drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
c.repaint();
}
public static void main(String s[])
{
ex9 ob=new ex9();
}
}
nn@linuxmint ~/Desktop/java 7 $ javac ex9.java
nn@linuxmint ~/Desktop/java 7 $ java ex9
nn@linuxmint ~/Desktop/java 7 $
0 comments:
Post a Comment