//this program must draw line in a panel using mouse events .. import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; public class MouseTest { public static void main(String [] args) { MouseFrame frame = new MouseFrame() ; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; frame.show() ; } } class MouseFrame extends JFrame { MouseFrame() { setTitle("Mouse" ) ; setSize(DEFAULT_WIDTH , DEFAULT_lENGTH) ; MousePanel panel = new MousePanel() ; Container contentPane = getContentPane() ; contentPane.add(panel) ; } public static final int DEFAULT_WIDTH = 350 ; public static final int DEFAULT_lENGTH = 350 ; } class MousePanel extends JPanel { Line2D line ; public MousePanel() { addMouseListener(new MouseHandler()) ; } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g1 =(Graphics2D) g ; if(line!=null) { g1.draw(line) ; } } private class MouseHandler extends MouseAdapter { public void MousePressed( MouseEvent event) { line = new Line2D.Double(0 , 0 , event.getX() , event.getY()) ; repaint() ; } } }