// DrawingRectanglesApplet.java (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2004-12-13 File created. // 2007-03-29 Last modification. // This program is very much similar to DrawingLinesApplet.java. // The difference between these two programs is that this one draws // rectangles while the other one draws lines. // This program is able to draw a rectangle "in a correct way" // regardless of the direction to which the mouse moves. // Various drawing programs usually produce rectangles in this way. import java.awt.* ; import java.awt.event.*; import javax.swing.* ; public class DrawingRectanglesApplet extends JApplet implements MouseListener, MouseMotionListener { final int MAXIMUM_NUMBER_OF_RECTANGLES = 10 ; Point starting_points[] = new Point[ MAXIMUM_NUMBER_OF_RECTANGLES ] ; Point ending_points[] = new Point[ MAXIMUM_NUMBER_OF_RECTANGLES ] ; Point new_starting_point ; Point new_ending_point ; int current_number_of_rectangles = 0 ; public void init() { getContentPane().setBackground( Color.white ) ; // Event listeners must be registered. addMouseListener( this ) ; addMouseMotionListener( this ) ; } // The following dummy functions are needed // to satisfy 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 ( current_number_of_rectangles < MAXIMUM_NUMBER_OF_RECTANGLES ) { new_starting_point = event.getPoint() ; } } public void mouseReleased( MouseEvent event ) { if ( current_number_of_rectangles < MAXIMUM_NUMBER_OF_RECTANGLES ) { starting_points[ current_number_of_rectangles ] = new_starting_point; ending_points[ current_number_of_rectangles ] = event.getPoint() ; current_number_of_rectangles ++ ; new_ending_point = null ; new_starting_point = null ; repaint() ; } } public void mouseDragged( MouseEvent event ) { if ( current_number_of_rectangles < MAXIMUM_NUMBER_OF_RECTANGLES ) { new_ending_point = event.getPoint() ; repaint(); } } // The following method adjusts coordinates so that the rectangle // is shown "in a correct way" in relation to the mouse movement. void draw_rectangle( Graphics graphics, Point starting_point, Point ending_point ) { int drawing_point_x = starting_point.x ; int drawing_point_y = starting_point.y ; int rectangle_width = ending_point.x - starting_point.x ; int rectangle_height = ending_point.y - starting_point.y ; if ( rectangle_width < 0 ) { rectangle_width = - rectangle_width ; drawing_point_x -= rectangle_width ; } if ( rectangle_height < 0 ) { rectangle_height = - rectangle_height ; drawing_point_y -= rectangle_height ; } graphics.drawRect( drawing_point_x, drawing_point_y, rectangle_width, rectangle_height ) ; } public void paint( Graphics graphics ) { super.paint( graphics ) ; for ( int rectangle_index = 0 ; rectangle_index < current_number_of_rectangles ; rectangle_index ++ ) { draw_rectangle( graphics, starting_points[ rectangle_index ], ending_points[ rectangle_index ] ) ; } graphics.setColor( Color.red ) ; if ( new_ending_point != null ) { draw_rectangle( graphics, new_starting_point, new_ending_point ) ; } if ( current_number_of_rectangles == MAXIMUM_NUMBER_OF_RECTANGLES ) { graphics.drawString( "Cannot draw more rectangles", 0, 300 ) ; } } }