Tranvick

Untitled

Jul 2nd, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class sample3 {
  5.     public static void main(String args[]) {
  6.         new MyFrame("Paint");
  7.     }
  8. }
  9.  
  10. class MyFrame extends Frame {
  11.     int lastX = -1, lastY = -1;
  12.     public MyFrame(String name) {
  13.         addWindowListener(new WindowAdapter() {
  14.             public void windowClosing(WindowEvent we) {
  15.                 System.exit(0);
  16.             }
  17.         });
  18.         addMouseListener(new MouseAdapter() {
  19.             public void mouseClicked(MouseEvent me) {
  20.                 int x1 = me.getX();
  21.                 int y1 = me.getY();
  22.                 getGraphics().drawLine(x1, y1, x1, y1);
  23.                 lastX = lastY = -1;
  24.             }
  25.            
  26.             public void mouseReleased(MouseEvent me) {
  27.                 lastX = lastY = -1;
  28.             }
  29.         });
  30.         addMouseMotionListener(new MouseMotionAdapter() {
  31.             public void mouseDragged(MouseEvent me) {
  32.                 int x1 = me.getX();
  33.                 int y1 = me.getY();
  34.                 int x2 = lastX;
  35.                 int y2 = lastY;
  36.                 if (x2 == -1) {
  37.                     x2 = x1;
  38.                     y2 = y1;
  39.                 }
  40.                 getGraphics().drawLine(x1, y1, x2, y2);
  41.                 lastX = x1;
  42.                 lastY = y1;
  43.             }
  44.         });
  45.         setSize(new Dimension(300,200));
  46.         setTitle(name);
  47.         setVisible(true);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment