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

Using class MouseMotionAdapter

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
Using class MouseMotionAdapter import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Painter extends JFrame { private int xValue = -10, yValue = -10; public Painter() { super( "A simple paint program" ); getContentPane().add( new Label( "Drag the mouse to draw" ), BorderLayout.SOUTH ); addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged( MouseEvent e ) { xValue = e.getX(); yValue = e.getY(); repaint(); } } ); setSize( 300, 150 ); show(); } public void paint( Graphics g ) { g.fillOval( xValue, yValue, 4, 4 ); } public static void main( String args[] ) { Painter app = new Painter(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }