// DrawingLinesApplication.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com/ // 2006-01-20 File created. // 2006-02-28 Last modification. // This is a Java Application that operates like DrawingLinesApplet.java import java.awt.* ; import java.awt.event.*; import javax.swing.* ; import javax.swing.event.* ; // Class DrawingLinesPanes is here exactly the same as in // DrawingLinesApplet.java. class DrawingLinesPanel extends JPanel implements MouseListener, MouseMotionListener { final int MAXIMUM_NUMBER_OF_LINES = 10 ; Point[] starting_points = new Point[ MAXIMUM_NUMBER_OF_LINES ] ; Point[] ending_points = new Point[ MAXIMUM_NUMBER_OF_LINES ] ; Point start_of_new_line ; Point end_of_new_line ; int number_of_lines = 0 ; public DrawingLinesPanel() { setBackground( Color.white ) ; // Event listeners must be registered. addMouseListener( this ) ; addMouseMotionListener( this ) ; } // The following dummy methods are needed // to fully implement the listener interfaces. public void mouseMoved( MouseEvent event ) {} public void mouseClicked( MouseEvent event ) {} public void mouseEntered( MouseEvent event ) {} public void mouseExited( MouseEvent event ) {} public void mousePressed( MouseEvent event ) { if ( number_of_lines < MAXIMUM_NUMBER_OF_LINES ) { start_of_new_line = event.getPoint() ; } } public void mouseReleased( MouseEvent event ) { if ( number_of_lines < MAXIMUM_NUMBER_OF_LINES ) { starting_points[ number_of_lines ] = start_of_new_line; ending_points[ number_of_lines ] = event.getPoint() ; number_of_lines ++ ; end_of_new_line = null ; start_of_new_line = null ; repaint() ; } } public void mouseDragged( MouseEvent event ) { if ( number_of_lines < MAXIMUM_NUMBER_OF_LINES ) { end_of_new_line = event.getPoint() ; repaint(); } } public void paintComponent( Graphics graphics ) { // The superclass version of paintComponent() clears the // component area and does other necessary things. super.paintComponent( graphics ) ; for ( int line_index = 0 ; line_index < number_of_lines ; line_index ++ ) { graphics.drawLine( starting_points[ line_index ].x, starting_points[ line_index ].y, ending_points[ line_index ].x, ending_points[ line_index ].y ) ; } graphics.setColor( Color.red ) ; if ( end_of_new_line != null ) { graphics.drawLine( start_of_new_line.x, start_of_new_line.y, end_of_new_line.x, end_of_new_line.y ) ; } if ( number_of_lines == MAXIMUM_NUMBER_OF_LINES ) { graphics.drawString( "Cannot draw more lines", 0, 300 ) ; } } } class DrawingLinesFrame extends JFrame { public static final int FRAME_WIDTH = 500 ; public static final int FRAME_HEIGHT = 400 ; public DrawingLinesFrame() { setTitle( "YOU CAN DRAW LINES INTO THIS WINDOW" ) ; setSize( FRAME_WIDTH, FRAME_HEIGHT ) ; DrawingLinesPanel drawing_lines_panel = new DrawingLinesPanel() ; Container content_pane = getContentPane() ; content_pane.add( drawing_lines_panel ) ; } } public class DrawingLinesApplication { public static void main( String[] not_in_use ) { DrawingLinesFrame drawing_lines_frame = new DrawingLinesFrame() ; drawing_lines_frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; drawing_lines_frame.setVisible( true ) ; } }