Java Reference
In-Depth Information
public void modify(Point start, Point last) {
line.x2 = last.x - position.x;
line.y2 = last.y - position.y;
bounds = new java.awt.Rectangle(
Math.min(start.x ,last.x), Math.min(start.y, last.y),
Math.abs(start.x - last.x)+1, Math.abs(start.y - last.y)+1);
}
// Display the line
public void draw(Graphics2D g2D) {
g2D.setPaint(color);
// Set the line color
g2D.translate(position);
// Move origin
g2D.draw(line);
// Draw the line
g2D.translate(-position.x, -position.y);
// Move context origin back
}
private Line2D.Double line;
private final static long serialVersionUID = 1001L;
}
// Abstract methods & fields in Element class...
}
Directory "Sketcher 4 drawing sketch line and rectangle elements"
The
Line
constructor has three parameters: the two end points of the line as type
Point
and the color. You
use type
Point
because all the points you work with originate from mouse events as type
Point
. Calling the
base class constructor records the position and color of the line in the members of the
Line
class inherited
from
Element
. You create the line shape as a
Line2D.Double
object in the
Line
class constructor using the
Line2D.Double
constructor that accepts the coordinates of the two points that define the line. You then store
the reference in the
line
member of the class. The coordinates of the first point on the line is the
origin
point so you must adjust the coordinates of the second point to be relative to
origin
at (0,0). You can easily
do this by subtracting the
x
and
y
coordinates of
position
from the corresponding coordinates of
end
.
The constructor creates the
bounds
rectangle from the two points that define the line. The position of
the rectangle is the top-left corner. This is the point corresponding to the minimum
x
and minimum
y
of the
two defining points. The rectangle width and height are the differences between the
x
and the
y
coordinates,
respectively. However, when an area specified by a rectangle is painted, the pixels that lie on the right and
bottom edges of the rectangle are not included. To ensure that the entire line lies within the rectangle you
add 1 to the width and height.
The
modify()
method is called when an end point of the line changes when the mouse is being dragged
to define the line. The end point of a
Line2D.Double
object is stored in its public members,
x2
and
y2
. The
modify()
method updates these by subtracting the corresponding members of
last
to adjust for the first
point being at
origin
. The method then updates the
bounds
member to accommodate the new end point.
The
draw()
method sets the line color in the device context by passing it to the
setPaint()
method and
then moves the origin of the device context to
position
by calling its
translate()
method. It then draws
a line relative to the new origin by calling
draw()
for the
g2D
object. You call
translate()
once more to
reset the origin back to its original state at
position
. It is essential to do this because the
mouseDragged()
method executes many times and calls
draw()
using the same device context. If you don't reset the origin
for the device context, it is moved further and further each time you draw another instance of an element.