Class Drawing
- java.lang.Object
-
- edu.rit.draw.Drawing
-
- All Implemented Interfaces:
- Displayable, Drawable, Viewable, java.io.Externalizable, java.io.Serializable
public class Drawing extends java.lang.Object implements java.io.Externalizable, Viewable
Class Drawing provides a drawing. A drawing consists of a sequence of DrawingItems, which are displayed in order. Thus, a drawing item later in the sequence will obscure a drawing item earlier in the sequence if the items overlap.A drawing contains a ColorFill object that tells how to color the drawing's background.
A drawing contains the size of the rectangular region within which the drawing items are to be displayed. By default, the display region is just large enough to contain all the drawing items, plus a border. Methods are provided to specify a different display region if desired.
An instance of class Drawing may be serialized. Class Drawing provides static convenience methods to write a drawing to a file and read a drawing from a file. A drawing file is simply a binary file that contains a serialized drawing object. The View program can be used to display the drawing file.
There is a default drawing. Initially, the default drawing is a new drawing object. The default drawing may be changed, or set to null to signify that there is no default drawing. A DrawingItem's add() and addFirst() methods add the drawing item to the default drawing (if there is one).
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classDrawing.AlignmentEnumeration Drawing.Alignment specifies how a Drawing's items are to be aligned within the drawing's display region.
-
Field Summary
Fields Modifier and Type Field and Description static SizeAUTOMATIC_SIZESignifies that the drawing's display region's size should be determined automatically based on the drawing items in the drawing.static Drawing.AlignmentNORMAL_ALIGNMENTThe normal display region/drawing item alignment (northwest).static ColorFillNORMAL_BACKGROUNDThe normal background color (white).static doubleNORMAL_BORDERThe normal display region border width (2).static SizeNORMAL_SIZEThe normal display region size (automatic).
-
Constructor Summary
Constructors Constructor and Description Drawing()Construct a new drawing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidadd(DrawingItem theItem)Add the given drawing item to the end of this drawing's sequence of drawing items.voidaddFirst(DrawingItem theItem)Add the given drawing item to the beginning of this drawing's sequence of drawing items.Drawing.Alignmentalignment()Returns this drawing's alignment.Drawingalignment(Drawing.Alignment theAlignment)Set this drawing's alignment.GroupasGroup()Returns a Group consisting of the drawing items in this drawing.ColorFillbackground()Returns this drawing's background color fill.Drawingbackground(ColorFill theBackground)Set this drawing's background color fill.doubleborder()Returns this drawing's display region's border.Drawingborder(double theBorder)Set this drawing's display region's border.voidclear()Clear this drawing's sequence of drawing items.static DrawingdefaultDrawing()Returns the default drawing.static voiddefaultDrawing(Drawing theDrawing)Set the default drawing.voiddraw(java.awt.Graphics2D g2d)Draw this drawable object in the given graphics context.java.awt.PaintgetBackgroundPaint()Returns this displayable object's background paint.java.awt.geom.Rectangle2DgetBoundingBox()Returns this displayable object's bounding box.DisplayableFramegetFrame()Get a displayable frame in which to view this viewable object.java.lang.StringgetTitle()Get the title for the frame used to view this viewable object.static Drawingread(java.io.File theFile)Read a drawing from the given file.static Drawingread(java.lang.String theFileName)Read a drawing from the file with the given name.voidreadExternal(java.io.ObjectInput in)Read this drawing from the given object input stream.voidsetTitle(java.lang.String theTitle)Set the title for the frame used to view this viewable object.Sizesize()Returns this drawing's display region's size.Drawingsize(Size theSize)Set this drawing's display region's size.static voidwrite(Drawing theDrawing, java.io.File theFile)Write the given drawing to the given file.static voidwrite(Drawing theDrawing, java.lang.String theFileName)Write the given drawing to the file with the given name.static voidwrite(java.io.File theFile)Write the default drawing to the given file.static voidwrite(java.lang.String theFileName)Write the default drawing to the file with the given name.voidwriteExternal(java.io.ObjectOutput out)Write this drawing to the given object output stream.
-
-
-
Field Detail
-
AUTOMATIC_SIZE
public static final Size AUTOMATIC_SIZE
Signifies that the drawing's display region's size should be determined automatically based on the drawing items in the drawing. A Size of (0,0) is used for this purpose.
-
NORMAL_SIZE
public static final Size NORMAL_SIZE
The normal display region size (automatic).
-
NORMAL_BORDER
public static final double NORMAL_BORDER
The normal display region border width (2).- See Also:
- Constant Field Values
-
NORMAL_ALIGNMENT
public static final Drawing.Alignment NORMAL_ALIGNMENT
The normal display region/drawing item alignment (northwest).
-
NORMAL_BACKGROUND
public static final ColorFill NORMAL_BACKGROUND
The normal background color (white).
-
-
Constructor Detail
-
Drawing
public Drawing()
Construct a new drawing. The drawing contains no drawing items. The normal size (automatic), normal border (2), normal alignment (northwest), and normal background color (white) are used.
-
-
Method Detail
-
defaultDrawing
public static Drawing defaultDrawing()
Returns the default drawing.- Returns:
- Default drawing, or null if there is no default drawing.
-
defaultDrawing
public static void defaultDrawing(Drawing theDrawing)
Set the default drawing.- Parameters:
theDrawing- Default drawing, or null if there is no default drawing.
-
clear
public void clear()
Clear this drawing's sequence of drawing items.
-
add
public void add(DrawingItem theItem)
Add the given drawing item to the end of this drawing's sequence of drawing items.- Parameters:
theItem- Drawing item.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theItem is null.
-
addFirst
public void addFirst(DrawingItem theItem)
Add the given drawing item to the beginning of this drawing's sequence of drawing items.- Parameters:
theItem- Drawing item.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theItem is null.
-
size
public Size size()
Returns this drawing's display region's size. This includes the border if any. If the return value is equal to Drawing.AUTOMATIC_SIZE (0,0), it signifies that the drawing's display region's size should be determined automatically based on the drawing items in the drawing.- Returns:
- Display region size.
-
size
public Drawing size(Size theSize)
Set this drawing's display region's size. This includes the border if any. If theSize is equal to Drawing.AUTOMATIC_SIZE (0,0), or if either the width or the height of theSize is negative, it signifies that the drawing's display region's size should be determined automatically based on the drawing items in the drawing.- Parameters:
theSize- Display region size.- Returns:
- This drawing object.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theSize is null.
-
border
public double border()
Returns this drawing's display region's border.- Returns:
- Display region border width.
-
border
public Drawing border(double theBorder)
Set this drawing's display region's border.- Parameters:
theBorder- Display region border width.- Returns:
- This drawing object.
- Throws:
java.lang.IllegalArgumentException- (unchecked exception) Thrown if theBorder is less than 0.
-
alignment
public Drawing.Alignment alignment()
Returns this drawing's alignment. The alignment determines where this drawing's drawing items are displayed relative to this drawing's display region.- Returns:
- Alignment.
-
alignment
public Drawing alignment(Drawing.Alignment theAlignment)
Set this drawing's alignment. The alignment determines where this drawing's drawing items are displayed relative to this drawing's display region.- Parameters:
theAlignment- Alignment.- Returns:
- This drawing object.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theAlignment is null.
-
background
public ColorFill background()
Returns this drawing's background color fill.- Returns:
- Background color fill.
-
background
public Drawing background(ColorFill theBackground)
Set this drawing's background color fill.- Parameters:
theBackground- Background color fill.- Returns:
- This drawing object.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theBackground is null.
-
asGroup
public Group asGroup()
Returns a Group consisting of the drawing items in this drawing. In this way, one drawing can be embedded (as a Grouip) inside another drawing.- Returns:
- Group.
-
writeExternal
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOExceptionWrite this drawing to the given object output stream.- Specified by:
writeExternalin interfacejava.io.Externalizable- Parameters:
out- Object output stream.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
readExternal
public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundExceptionRead this drawing from the given object input stream.- Specified by:
readExternalin interfacejava.io.Externalizable- Parameters:
in- Object input stream.- Throws:
java.io.IOException- Thrown if an I/O error occurred.java.lang.ClassNotFoundException- Thrown if a class needed to deserialize this drawing cannot be found.
-
write
public static void write(java.lang.String theFileName) throws java.io.IOExceptionWrite the default drawing to the file with the given name. The default drawing is written in serialized form to the file. If there is no default drawing, the write() method does nothing.- Parameters:
theFileName- File name.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
write
public static void write(java.io.File theFile) throws java.io.IOExceptionWrite the default drawing to the given file. The default drawing is written in serialized form to theFile. If there is no default drawing, the write() method does nothing.- Parameters:
theFile- File.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
write
public static void write(Drawing theDrawing, java.lang.String theFileName) throws java.io.IOException
Write the given drawing to the file with the given name. theDrawing is written in serialized form to the file.- Parameters:
theDrawing- Drawing.theFileName- File name.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
write
public static void write(Drawing theDrawing, java.io.File theFile) throws java.io.IOException
Write the given drawing to the given file. theDrawing is written in serialized form to theFile.- Parameters:
theDrawing- Drawing.theFile- File.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
read
public static Drawing read(java.lang.String theFileName) throws java.io.IOException, java.lang.ClassNotFoundException
Read a drawing from the file with the given name. The file must contain one instance of class Drawing in serialized form; for example, as written by the static Drawing.write() method.- Parameters:
theFileName- File name.- Returns:
- Drawing.
- Throws:
java.io.IOException- Thrown if an I/O error occurred.java.lang.ClassNotFoundException- Thrown if any class needed to deserialize the drawing cannot be found.
-
read
public static Drawing read(java.io.File theFile) throws java.io.IOException, java.lang.ClassNotFoundException
Read a drawing from the given file. The file must contain one instance of class Drawing in serialized form; for example, as written by the static Drawing.write() method.- Parameters:
theFile- File.- Returns:
- Drawing.
- Throws:
java.io.IOException- Thrown if an I/O error occurred.java.lang.ClassNotFoundException- Thrown if any class needed to deserialize the drawing cannot be found.
-
draw
public void draw(java.awt.Graphics2D g2d)
Draw this drawable object in the given graphics context. Upon return from this method, the given graphics context's state (color, font, transform, clip, and so on) is the same as it was upon entry to this method.
-
getBoundingBox
public java.awt.geom.Rectangle2D getBoundingBox()
Returns this displayable object's bounding box. This is the smallest rectangle that encloses all of this displayable object.- Specified by:
getBoundingBoxin interfaceDisplayable
-
getBackgroundPaint
public java.awt.Paint getBackgroundPaint()
Returns this displayable object's background paint.- Specified by:
getBackgroundPaintin interfaceDisplayable
-
getFrame
public DisplayableFrame getFrame()
Get a displayable frame in which to view this viewable object. Initially, the returned frame is displaying this viewable object.
-
getTitle
public java.lang.String getTitle()
Get the title for the frame used to view this viewable object. If the title is null, a default title is used.
-
setTitle
public void setTitle(java.lang.String theTitle)
Set the title for the frame used to view this viewable object. If the title is null, a default title is used.- Parameters:
theTitle- Title.
-
-
DMelt 3.0 © DataMelt by jWork.ORG