// DrawingLinesAWTApplication.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2004-10-21 File created. // 2007-02-28 Last modification. import java.awt.* ; import java.awt.event.*; public class DrawingLinesAWTApplication extends Frame implements ActionListener, MouseListener, MouseMotionListener, WindowListener { static final int WINDOW_WIDTH = 500 ; static final int WINDOW_HEIGHT = 400 ; 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 ; MenuItem edit_clear_menu_item = new MenuItem( "Clear" ) ; DrawingLinesAWTApplication() { setBackground(Color.white); // Event listeners must be registered. addMouseListener(this); addMouseMotionListener(this); // This class implements a WindowListener interface. addWindowListener( this ) ; setSize( WINDOW_WIDTH, WINDOW_HEIGHT ) ; setTitle( "DrawingLines Windows Application" ) ; edit_clear_menu_item.addActionListener( this ) ; Menu edit_menu = new Menu( "Edit" ) ; edit_menu.add( edit_clear_menu_item ) ; MenuBar menubar_of_this_window = new MenuBar() ; menubar_of_this_window.add( edit_menu ) ; setMenuBar( menubar_of_this_window ) ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof MenuItem ) { MenuItem valittu_menu_item = (MenuItem) event.getSource() ; if ( valittu_menu_item == edit_clear_menu_item ) { number_of_lines = 0 ; // Poistetaan mahdollinen WINDOW FULL -otsikko. setTitle( "DrawingLines Windows Application" ) ; } repaint() ; } } // 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 windowClosing( WindowEvent event ) { setVisible( false ) ; System.exit( 0 ) ; } // Other methods required by the WindowListener interface: public void windowActivated( WindowEvent event ) {} public void windowClosed( WindowEvent event ) {} public void windowDeactivated( WindowEvent event ) {} public void windowDeiconified( WindowEvent event ) {} public void windowIconified( WindowEvent event ) {} public void windowOpened( WindowEvent event ) {} public void paint( Graphics 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 ) ; setTitle( "WINDOW FULL" ) ; } } public static void main( String[] not_in_use ) { DrawingLinesAWTApplication this_application_window = new DrawingLinesAWTApplication() ; this_application_window.setVisible( true ) ; } }