objectdraw
Class Line

java.lang.Object
  extended by objectdraw.ObjectDrawObject
      extended by objectdraw.ObjectDrawShape
          extended by objectdraw.Line
All Implemented Interfaces:
objectdraw.Dependent, Drawable1DInterface, DrawableInterface, DrawableStrokeInterface
Direct Known Subclasses:
AngLine

public class Line
extends objectdraw.ObjectDrawShape
implements Drawable1DInterface, DrawableStrokeInterface

A Line represents a line segment connecting two points on the screen. The end points may be altered individually to move the line. It is also possible to set the color, thickness, and stroke style of the line.

Author:
Russell Zahniser (russell@zahniser.net)

Field Summary
 
Fields inherited from interface objectdraw.DrawableStrokeInterface
CLICK_SIZE, DEFAULT_STROKE
 
Constructor Summary
Line(double x1, double y1, double x2, double y2, Color color, DrawingCanvas canvas)
          Create a Line from (x1, y1) to (x2, y2), in the given color.
Line(double x1, double y1, double x2, double y2, DrawingCanvas canvas)
          Create a Line from (x1, y1) to (x2, y2), colored black.
Line(Location start, Location end, Color color, DrawingCanvas canvas)
          Create a Line from start to end, in the given color.
Line(Location start, Location end, DrawingCanvas canvas)
          Create a Line from start to end, colored black.
 
Method Summary
 void addToCanvas(DrawingCanvas c)
          Place this object on the specified canvas.
 boolean contains(Location point)
          Return true if the given location is inside this object, false otherwise.
 DrawingCanvas getCanvas()
          Return the canvas that this object is on.
 Color getColor()
          Return the color of this object
 Location getEnd()
          Get the location of the end of the line.
 double getLineWidth()
          Return the width of this object's lines.
 Location getStart()
          Get the location of the start of the line.
 BasicStroke getStroke()
          Return the stroke used for this object's lines.
 void hide()
          Make this object invisible.
 boolean isHidden()
          Return true if this object has been rendered invisible with a call to its hide() method, false if it is still being drawn.
 void move(double dx, double dy)
          Shift (translate) this object left or right by an amount dx, and up or down by an amount dy.
 void moveTo(double x, double y)
          Move the reference point of this object to the given location.
 void moveTo(Location point)
          Move the reference point of this object to the given location.
 void removeFromCanvas()
          Remove this object from its canvas.
 void sendBackward()
          Move this object one step backward in the draw order, causing it to be drawn underneath the objects that are in front of it.
 void sendForward()
          Move this object one step forward in the draw order, causing it to be drawn on top of the objects that are behind it.
 void sendToBack()
          Move this object to the very back of the draw order, causing it to be drawn underneath all the other objects.
 void sendToFront()
          Move this object to the very front of the draw order, causing it to be drawn on top of all the other objects.
 void setColor(Color c)
          Change the color of this object.
 void setEnd(double x, double y)
          Move the end of the line to a new location
 void setEnd(Location point)
          Move the end of the line to a new location
 void setEndPoints(double x1, double y1, double x2, double y2)
          Move both endpoints of the line.
 void setEndPoints(Location start, Location end)
          Move both endpoints of the line.
 void setLineWidth(double width)
          Set the width of this object's lines
 void setStart(double x, double y)
          Move the start of the line to a new location
 void setStart(Location point)
          Move the start of the line to a new location
 void setStroke(BasicStroke stroke)
          Set the stroke used for this object's lines
 void show()
          Make an object visible again after it had been hidden.
 String toString()
          Return a string describing how this object might be constructed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Line

public Line(double x1,
            double y1,
            double x2,
            double y2,
            Color color,
            DrawingCanvas canvas)
Create a Line from (x1, y1) to (x2, y2), in the given color.

Parameters:
x1 - x coordinate of the start point
y1 - y coordinate of the start point
x2 - x coordinate of the end point
y2 - y coordinate of the end point
color - Color of the line
canvas - DrawingCanvas to place the Line

Line

public Line(double x1,
            double y1,
            double x2,
            double y2,
            DrawingCanvas canvas)
Create a Line from (x1, y1) to (x2, y2), colored black.

Parameters:
x1 - x coordinate of the start point
y1 - y coordinate of the start point
x2 - x coordinate of the end point
y2 - y coordinate of the end point
canvas - DrawingCanvas to place the Line

Line

public Line(Location start,
            Location end,
            Color color,
            DrawingCanvas canvas)
Create a Line from start to end, in the given color.

Parameters:
start - Location of the start point
end - Location of the end point
color - Color of the line
canvas - DrawingCanvas to place the Line

Line

public Line(Location start,
            Location end,
            DrawingCanvas canvas)
Create a Line from start to end, colored black.

Parameters:
start - Location of the start point
end - Location of the end point
canvas - DrawingCanvas to place the Line
Method Detail

addToCanvas

public void addToCanvas(DrawingCanvas c)
Place this object on the specified canvas. The object will be removed from its current canvas, if it is on a canvas that is not the one it is being moved to. The object will be at the front of the canvas, as if it had just been drawn. If the given canvas is null the object is simply removed from its current canvas.

Specified by:
addToCanvas in interface DrawableInterface
Overrides:
addToCanvas in class objectdraw.ObjectDrawShape
Parameters:
c - DrawingCanvas to place this object on

contains

public boolean contains(Location point)
Return true if the given location is inside this object, false otherwise. Because Lines and FramedArcs have no "inside", a point is considered to be contained in one of them as long as it is within a few pixels of the line.

Specified by:
contains in interface DrawableInterface
Parameters:
point - Location to test
Returns:
whether the given location is inside this shape

getCanvas

public DrawingCanvas getCanvas()
Return the canvas that this object is on. If it has been removed from its canvas, this method will return null.

Specified by:
getCanvas in interface DrawableInterface
Overrides:
getCanvas in class objectdraw.ObjectDrawShape
Returns:
the DrawingCanvas that this object is on.

getColor

public Color getColor()
Return the color of this object

Specified by:
getColor in interface DrawableInterface
Overrides:
getColor in class objectdraw.ObjectDrawShape
Returns:
the Color of this object

getEnd

public Location getEnd()
Get the location of the end of the line. Translating this Location object will have the same effect as changing the end point. So, the following three lines of code are equivalent:

line.setEnd(line.getEnd().getX() + 10, line.getEnd().getY() + 20);
line.setEnd(line.getEnd().offset(10, 20));
line.getEnd().translate(10, 20);


If you want to create a Location offset by some amount from the end Location of this line, without actually moving this line, use the offset() method in Location, which creates a new Location.

Specified by:
getEnd in interface Drawable1DInterface
Returns:
the Location of the end of the line.

getLineWidth

public double getLineWidth()
Return the width of this object's lines.

Specified by:
getLineWidth in interface DrawableStrokeInterface
Returns:
the width of this object's lines

getStart

public Location getStart()
Get the location of the start of the line. Translating this Location object will have the same effect as changing the start point. So, the following three lines of code are equivalent:

line.setStart(line.getStart().getX() + 10, line.getStart().getY() + 20);
line.setStart(line.getStart().offset(10, 20));
line.getStart().translate(10, 20);


If you want to create a Location offset by some amount from the start Location of this line, without actually moving this line, use the offset() method in Location, which creates a new Location.

Specified by:
getStart in interface Drawable1DInterface
Returns:
the Location of the start of the line.

getStroke

public BasicStroke getStroke()
Return the stroke used for this object's lines.

Specified by:
getStroke in interface DrawableStrokeInterface
Returns:
the stroke used for this object's lines

hide

public void hide()
Make this object invisible. It stays in position on its canvas; it is simply not drawn. This method has no effect if the object was already hidden.

Specified by:
hide in interface DrawableInterface
Overrides:
hide in class objectdraw.ObjectDrawShape

isHidden

public boolean isHidden()
Return true if this object has been rendered invisible with a call to its hide() method, false if it is still being drawn.

Specified by:
isHidden in interface DrawableInterface
Overrides:
isHidden in class objectdraw.ObjectDrawShape
Returns:
whether this object is hidden

move

public void move(double dx,
                 double dy)
Shift (translate) this object left or right by an amount dx, and up or down by an amount dy. A negative dx indicates a shift to the left; a negative dy indicates a shift up

Specified by:
move in interface DrawableInterface
Overrides:
move in class objectdraw.ObjectDrawShape
Parameters:
dx - amount of x translation
dy - amount of y translation

moveTo

public void moveTo(double x,
                   double y)
Move the reference point of this object to the given location. For a Drawable2D object, the reference point is the top left corner; for a line, it is the start point.

Specified by:
moveTo in interface DrawableInterface
Overrides:
moveTo in class objectdraw.ObjectDrawShape
Parameters:
x - x coordinate to move to
y - y coordinate to move to

moveTo

public void moveTo(Location point)
Move the reference point of this object to the given location. For a Drawable2D object, the reference point is the top left corner; for a line, it is the start point.

Specified by:
moveTo in interface DrawableInterface
Overrides:
moveTo in class objectdraw.ObjectDrawShape
Parameters:
point - Location to move to

removeFromCanvas

public void removeFromCanvas()
Remove this object from its canvas.

Specified by:
removeFromCanvas in interface DrawableInterface
Overrides:
removeFromCanvas in class objectdraw.ObjectDrawShape

sendBackward

public void sendBackward()
Move this object one step backward in the draw order, causing it to be drawn underneath the objects that are in front of it.

Specified by:
sendBackward in interface DrawableInterface
Overrides:
sendBackward in class objectdraw.ObjectDrawShape

sendForward

public void sendForward()
Move this object one step forward in the draw order, causing it to be drawn on top of the objects that are behind it.

Specified by:
sendForward in interface DrawableInterface
Overrides:
sendForward in class objectdraw.ObjectDrawShape

sendToBack

public void sendToBack()
Move this object to the very back of the draw order, causing it to be drawn underneath all the other objects.

Specified by:
sendToBack in interface DrawableInterface
Overrides:
sendToBack in class objectdraw.ObjectDrawShape

sendToFront

public void sendToFront()
Move this object to the very front of the draw order, causing it to be drawn on top of all the other objects.

Specified by:
sendToFront in interface DrawableInterface
Overrides:
sendToFront in class objectdraw.ObjectDrawShape

setColor

public void setColor(Color c)
Change the color of this object.

Specified by:
setColor in interface DrawableInterface
Overrides:
setColor in class objectdraw.ObjectDrawShape
Parameters:
c - new Color for this object

setEnd

public void setEnd(double x,
                   double y)
Move the end of the line to a new location

Specified by:
setEnd in interface Drawable1DInterface
Parameters:
x - x coordinate to which the end of the line should be moved
y - y coordinate to which the end of the line should be moved

setEnd

public void setEnd(Location point)
Move the end of the line to a new location

Specified by:
setEnd in interface Drawable1DInterface
Parameters:
point - Location to which the end of the line should be moved

setEndPoints

public void setEndPoints(double x1,
                         double y1,
                         double x2,
                         double y2)
Move both endpoints of the line.

Specified by:
setEndPoints in interface Drawable1DInterface
Parameters:
x1 - x1 coordinate to which the start of the line should be moved
y1 - y1 coordinate to which the start of the line should be moved
x2 - x coordinate to which the end of the line should be moved
y2 - y coordinate to which the end of the line should be moved

setEndPoints

public void setEndPoints(Location start,
                         Location end)
Move both endpoints of the line.

Specified by:
setEndPoints in interface Drawable1DInterface
Parameters:
start - Location to which the start of the line should be moved
end - Location to which the end of the line should be moved

setLineWidth

public void setLineWidth(double width)
Set the width of this object's lines

Specified by:
setLineWidth in interface DrawableStrokeInterface
Parameters:
width - the new width of this object's lines

setStart

public void setStart(double x,
                     double y)
Move the start of the line to a new location

Specified by:
setStart in interface Drawable1DInterface
Parameters:
x - x coordinate to which the start of the line should be moved
y - y coordinate to which the start of the line should be moved

setStart

public void setStart(Location point)
Move the start of the line to a new location

Specified by:
setStart in interface Drawable1DInterface
Parameters:
point - Location to which the start of the line should be moved

setStroke

public void setStroke(BasicStroke stroke)
Set the stroke used for this object's lines

Specified by:
setStroke in interface DrawableStrokeInterface
Parameters:
stroke - the new stroke for this object's lines

show

public void show()
Make an object visible again after it had been hidden. This method has no effect if the object was already visible.

Specified by:
show in interface DrawableInterface
Overrides:
show in class objectdraw.ObjectDrawShape

toString

public String toString()
Return a string describing how this object might be constructed.

Overrides:
toString in class objectdraw.ObjectDrawObject