Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- public class sample3 {
- public static void main(String args[]) {
- new MyFrame("Paint");
- }
- }
- class MyFrame extends Frame {
- int lastX = -1, lastY = -1;
- public MyFrame(String name) {
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent me) {
- int x1 = me.getX();
- int y1 = me.getY();
- getGraphics().drawLine(x1, y1, x1, y1);
- lastX = lastY = -1;
- }
- public void mouseReleased(MouseEvent me) {
- lastX = lastY = -1;
- }
- });
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseDragged(MouseEvent me) {
- int x1 = me.getX();
- int y1 = me.getY();
- int x2 = lastX;
- int y2 = lastY;
- if (x2 == -1) {
- x2 = x1;
- y2 = y1;
- }
- getGraphics().drawLine(x1, y1, x2, y2);
- lastX = x1;
- lastY = y1;
- }
- });
- setSize(new Dimension(300,200));
- setTitle(name);
- setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment