// DrawingRectanglesApplication.java (c) Kari Laitinen // http://www.naturalprogramming.com/ /* 2014-11-28 File created. 2014-11-28 Last modification. With this program the user can draw rectangles to the screen. This program demonstrates how to - react to mouse events - use an ArrayList-based array -- method add() adds an object to the end of an ArrayList -- method get() can be used to index an ArrayList -- method size() tells how many objects are contained in an ArrayList 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.* ; import java.util.* ; // ArrayList, Array, etc. class DrawingRectanglesPanel extends JPanel implements MouseListener, MouseMotionListener { // The Point objects that specify the rectangles will be stored // in ArrayList-based arrays. These arrays are dynamic so that // they can grow when objects are added. ArrayList starting_points = new ArrayList() ; ArrayList ending_points = new ArrayList() ; Point new_starting_point = null ; Point new_ending_point = null ; boolean new_rectangle_is_being_drawn = false ; public DrawingRectanglesPanel() { setBackground( new Color( 0xF5F5DC ) ) ; // Beige color addMouseListener( this ) ; addMouseMotionListener( this ) ; } // The following dummy methods 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 ) { new_starting_point = event.getPoint() ; new_rectangle_is_being_drawn = true ; } public void mouseDragged( MouseEvent event ) { if ( new_rectangle_is_being_drawn == true ) { new_ending_point = event.getPoint() ; repaint(); } } public void mouseReleased( MouseEvent event ) { if ( new_rectangle_is_being_drawn == true ) { // We'll add the new points to the end of the ArrayList-based arrays. starting_points.add( new_starting_point ) ; ending_points.add( event.getPoint() ) ; new_ending_point = null ; new_starting_point = null ; new_rectangle_is_being_drawn = false ; repaint() ; } } // The following method adjusts coordinates so that the rectangle // is shown "in a correct way" in relation to the mouse movement. void draw_filled_rectangle( Graphics graphics, Point starting_point, Point ending_point, Color filling_color ) { 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.setColor( filling_color ) ; graphics.fillRect( drawing_point_x, drawing_point_y, rectangle_width, rectangle_height ) ; graphics.setColor( Color.BLACK ) ; // Color for black edge. graphics.drawRect( drawing_point_x, drawing_point_y, rectangle_width, rectangle_height ) ; } public void paintComponent( Graphics graphics ) { super.paintComponent( graphics ) ; // All rectangles will be redrawn always when this method is executed. // Colors for rectangles will be taken from the following array of // Color objects. Color[] rectangle_colors = { Color.CYAN, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.MAGENTA, Color.PINK } ; int color_index = 0 ; for ( int rectangle_index = 0 ; rectangle_index < starting_points.size() ; rectangle_index ++ ) { draw_filled_rectangle( graphics, starting_points.get( rectangle_index ), ending_points.get( rectangle_index ), rectangle_colors[ color_index ] ) ; color_index ++ ; // Index for the next color to use. // If all colors have been used we'll start re-using colors from the // beginning of the array. if ( color_index >= rectangle_colors.length ) { color_index = 0 ; } } if ( new_ending_point != null ) { draw_filled_rectangle( graphics, new_starting_point, new_ending_point, new Color( 0xFFFAFA ) ) ; // 'Snow' color, almost white } } } class DrawingRectanglesFrame extends JFrame { public static final int FRAME_WIDTH = 600 ; public static final int FRAME_HEIGHT = 480 ; public DrawingRectanglesFrame() { setTitle( "DrawingRectanglesApplication.java" ) ; setSize( 880, 620 ) ; getContentPane().add( new DrawingRectanglesPanel() ) ; setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; } } public class DrawingRectanglesApplication { public static void main( String[] not_in_use ) { new DrawingRectanglesFrame().setVisible( true ) ; } }