Drawing
edu.rit.draw

Class Drawing

  • All Implemented Interfaces:
    Displayable, Drawable, Viewable, Externalizable, Serializable


    public class Drawingextends Objectimplements 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
    • 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:
        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:
        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:
        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:
        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:
        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:
        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.
      • write

        public static void write(String theFileName)                  throws IOException
        Write 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:
        IOException - Thrown if an I/O error occurred.
      • write

        public static void write(File theFile)                  throws IOException
        Write 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:
        IOException - Thrown if an I/O error occurred.
      • write

        public static void write(Drawing theDrawing,         String theFileName)                  throws 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:
        IOException - Thrown if an I/O error occurred.
      • write

        public static void write(Drawing theDrawing,         File theFile)                  throws IOException
        Write the given drawing to the given file. theDrawing is written in serialized form to theFile.
        Parameters:
        theDrawing - Drawing.
        theFile - File.
        Throws:
        IOException - Thrown if an I/O error occurred.
      • read

        public static Drawing read(String theFileName)                    throws IOException,                           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:
        IOException - Thrown if an I/O error occurred.
        ClassNotFoundException - Thrown if any class needed to deserialize the drawing cannot be found.
      • read

        public static Drawing read(File theFile)                    throws IOException,                           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:
        IOException - Thrown if an I/O error occurred.
        ClassNotFoundException - Thrown if any class needed to deserialize the drawing cannot be found.
      • draw

        public void draw(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.
        Specified by:
        draw in interface Drawable
        Parameters:
        g2d - 2-D graphics context.
      • getBoundingBox

        public Rectangle2D getBoundingBox()
        Returns this displayable object's bounding box. This is the smallest rectangle that encloses all of this displayable object.
        Specified by:
        getBoundingBox in interface Displayable
      • getFrame

        public DisplayableFrame getFrame()
        Get a displayable frame in which to view this viewable object. Initially, the returned frame is displaying this viewable object.
        Specified by:
        getFrame in interface Viewable
        Returns:
        Displayable frame.
      • getTitle

        public String getTitle()
        Get the title for the frame used to view this viewable object. If the title is null, a default title is used.
        Specified by:
        getTitle in interface Viewable
        Returns:
        Title.
      • setTitle

        public void setTitle(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.

SCaVis 2.0 © jWork.ORG