Class rectTool

java.lang.Object
  extended by rectTool
All Implemented Interfaces:
ourTool

public class rectTool
extends java.lang.Object
implements ourTool

A rectTool implements the operations performed by a rectangle. It holds the private fields int startX and startY, boolean dragged and int fillType. The variable dragged is set to true only when the method dragAction is called and is set to false otherwise. The fillType is assigned as follows: 1=set the color of the border. 2=set the color of the border and the fill. otherwise only set the color of the fill. It should work with all operating systems and hardware. There are no variances and no security constraints.


Field Summary
(package private)  java.awt.image.BufferedImage backupImage
          Holds the current image and the saved backup image.
(package private)  java.awt.image.BufferedImage curImage
          Holds the current image and the saved backup image.
private  boolean dragged
          initialized to false and set to false in clickAction because the mouse has not yet moved.
private  int fillType
          initializes fillType to 1.
 java.awt.Graphics2D g2D
          holds a Graphics2D used in the following methods.
private  int prevX
          Integer representing the previous x coordinate.
private  int prevY
          Integer representing the previous y coordinate.
 int startX
          initialized in clickAction by calling the function getX() and getY() on the MouseEvent mevt.
 int startY
          initialized in clickAction by calling the function getX() and getY() on the MouseEvent mevt.
 
Constructor Summary
rectTool()
           
 
Method Summary
 void clickAction(java.awt.event.MouseEvent mevt, main_canvas theCanvas)
          Allows the user to click on the canvas, to draw a rectangle.
 void dragAction(java.awt.event.MouseEvent mevt, main_canvas theCanvas)
          Allows the user to drag the mouse on the canvas and stretch the rectangle.
 void drawRectangle(int x, int y, int width, int height, java.awt.event.MouseEvent mevt, java.awt.Graphics2D g2D, main_canvas theCanvas)
          Draws the rectangle and sets the color of the border and the fill.
 boolean getDragged()
          Returns the current state of the flag dragged.
 int getFillType()
          Returns the fill type of the rectangle as an int.
 java.awt.Color getG2dColor()
          Returns the Color corresponding to the Graphics2D g2D.
 void mouseReleaseAction(java.awt.event.MouseEvent mevt, main_canvas theCanvas)
          Allows the user to release the mouse and stop drawing the rectangle, which was created by dragging the mouse.
 void setFillType(int theFillType)
          Sets the fillType to the int passed in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

startX

public int startX
initialized in clickAction by calling the function getX() and getY() on the MouseEvent mevt. (Initialized here since a rectangle is drawn by first clicking the mouse)


startY

public int startY
initialized in clickAction by calling the function getX() and getY() on the MouseEvent mevt. (Initialized here since a rectangle is drawn by first clicking the mouse)


dragged

private boolean dragged
initialized to false and set to false in clickAction because the mouse has not yet moved. Once the mouse has been clicked on and dragged, it is then set to true.


fillType

private int fillType
initializes fillType to 1. Reset by calling the method setFillType and passing a different integer value.


curImage

java.awt.image.BufferedImage curImage
Holds the current image and the saved backup image.


backupImage

java.awt.image.BufferedImage backupImage
Holds the current image and the saved backup image.


g2D

public java.awt.Graphics2D g2D
holds a Graphics2D used in the following methods.


prevX

private int prevX
Integer representing the previous x coordinate.


prevY

private int prevY
Integer representing the previous y coordinate.

Constructor Detail

rectTool

public rectTool()
Method Detail

clickAction

public void clickAction(java.awt.event.MouseEvent mevt,
                        main_canvas theCanvas)
Allows the user to click on the canvas, to draw a rectangle. Sets the dragged to be false and the backup Image and current Image to be the same because nothing has been drawn yet.

Specified by:
clickAction in interface ourTool
Parameters:
mevt - holds a MouseEvent to call the getX() and getY() to set the startX and startY, which are the coordinates that the mouse first clicks on
theCanvas - holds a main_canvas that calls the getBufferedImage to be set to the backup image and current image.

dragAction

public void dragAction(java.awt.event.MouseEvent mevt,
                       main_canvas theCanvas)
Allows the user to drag the mouse on the canvas and stretch the rectangle. Always sets the flag dragged to true. The variables x and y are initially set to the values of the startX and startY. However, if the endX is moved to the left (which would mean it is now less than the startX), then the variable x is set to the endX. The same is done for the variable y. The width is calculated to be the absolute value of the startX - endX. The height is calculated to be the absolute value of the startY - endY.

Specified by:
dragAction in interface ourTool
Parameters:
mevt - holds a MouseEvent to call the getX() and getY() for the endX() and endY() variables.
theCanvas - holds a main_canvas to drag the rectangle on.

mouseReleaseAction

public void mouseReleaseAction(java.awt.event.MouseEvent mevt,
                               main_canvas theCanvas)
Allows the user to release the mouse and stop drawing the rectangle, which was created by dragging the mouse. Therefore, this is performed only if dragged = true. The x and y variables are initially set to startX and startY, however, if the endX is now on the left of the startX (or is < than the startX, then x is set to endX. Also, if the endY is now less than the startY, the y is set to the endY. The width is calculated as the absolute value of the startX-endX. The height is calculated as the absolute value of the startY-endY. Dragged is set to false.

Specified by:
mouseReleaseAction in interface ourTool
Parameters:
mevt - holds a MouseEvent to call the getX and getY to set the endX and endY
theCanvas - holds a main_canvas

setFillType

public void setFillType(int theFillType)
Sets the fillType to the int passed in. Default is 1.

Parameters:
theFillType - determines which way to fill the rectangle. 1 = border left_color 2 = fill with right_color 3 = solid left_color
See Also:
getFillType()

getFillType

public int getFillType()
Returns the fill type of the rectangle as an int.

Returns:
int corresponding to the fill type
See Also:
setFillType(int)

drawRectangle

public void drawRectangle(int x,
                          int y,
                          int width,
                          int height,
                          java.awt.event.MouseEvent mevt,
                          java.awt.Graphics2D g2D,
                          main_canvas theCanvas)
Draws the rectangle and sets the color of the border and the fill. If fillType==1, and the LeftMouseButton is used, it sets the field g2D to the left color. Otherwise it is set to the right color. If fillType==2, and the LeftMouseButton is used, it sets the field g2D to the right color. Otherwise it is set to the left color. It fills the g2D with the color that it was set to. To set the border, if the leftMouseButton is used, then the color of the border is set to the left color. If the rightMouseButton is used, then the color is set to the right color. If the fill type is not 1 or 2, then it proceeds to the final else. If the LeftMouseButton is used, the fill color is set to the left color. Otherwise, it is set to the right color. Notice that the border is not set in this case.

Parameters:
x - used to initialize the starting x coordinate of the rectangle.
y - used to initialize the starting y coordinate of the rectangle
width - used to set the width of the rectangle
height - used to set the height of the rectangle
mevt - holds a MouseEvent
g2D - holds a Graphics2D
theCanvas - holds a main_canvas

getDragged

public boolean getDragged()
Returns the current state of the flag dragged. If dragAction was called, it returns true. Otherwise, it returns false.

Returns:
a boolean corresponding to the flag dragged

getG2dColor

public java.awt.Color getG2dColor()
Returns the Color corresponding to the Graphics2D g2D.

Returns:
Color corresponding to that of the Graphics2D