Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Java AWT Code Examples

Drawing using the Mouse

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
Drawing using the Mouse public class MousePaint extends Frame implements MouseMotionListener { private int x1, y1, x2, y2; public MousePaintII() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { dispose(); System.exit(0); } }); addMouseMotionListener(this); setBounds(50,50,400,250); setVisible(true); } public static void main(String[] argv) { new MousePaintII(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.setColor(Color.black); g.drawLine(x1, y1, x2, y2); } public void mouseDragged(MouseEvent me) { me.consume(); int x = me.getX(); int y = me.getY(); if ( x1 == 0 ) { x1 = x; } if ( y1 == 0 ) { y1 = y; } x2 = x; y2 = y; repaint(); x1 = x2; y1 = y2; } public void mouseMoved(MouseEvent me) { } }