Package doodlepad

Class Pad

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Iterable<Shape>, Accessible, RootPaneContainer, WindowConstants

public class Pad extends JFrame implements Iterable<Shape>
A class that implements a drawing surface and window for graphical shapes.
See Also:
  • Constructor Details

    • Pad

      public Pad(int width, int height)
      Simple Pad constructor taking only window dimensions
      Parameters:
      width - The width of the window
      height - The height of the window
    • Pad

      public Pad(String title, int width, int height)
      Simple Pad constructor taking only title and window dimensions
      Parameters:
      title - The Pad window title
      width - The width of the window
      height - The height of the window
    • Pad

      public Pad(String title, int width, int height, boolean singleton)
      Simple Pad constructor taking only window dimensions
      Parameters:
      title - The Pad window title
      width - The width of the window
      height - The height of the window
      singleton - True if this Pad object is to be set to the internal singleton
    • Pad

      public Pad(String title, int width, int height, Color background)
      Constructor for objects of class Pad
      Parameters:
      title - The Pad window title
      width - The width of the window
      height - The height of the window
      background - The default color of the background
    • Pad

      public Pad(String title, int width, int height, Color background, boolean singleton)
      Constructor for objects of class Pad
      Parameters:
      title - The Pad window title
      width - The width of the window
      height - The height of the window
      background - The default color of the background
      singleton - True if this Pad object is to be set to the internal singleton
  • Method Details

    • create

      public static void create()
      Create a new Pad object, properly invoking on the event dispatch thread.
    • create

      public static void create(int width, int height)
      Create a new Pad object, properly invoking on the event dispatch thread.
      Parameters:
      width - The width of the window
      height - The height of the window
    • create

      public static void create(String title, int width, int height)
      Create a new Pad object, properly invoking on the event dispatch thread.
      Parameters:
      title - The Pad window title
      width - The width of the window
      height - The height of the window
    • getPad

      public static Pad getPad()
      A static method that creates a new Pad singleton object or returns the current Pad singleton object. Ensures that only one singleton is created by synchronizing execution.
      Returns:
      The current Pad singleton object
    • getDefaultLayer

      public static Layer getDefaultLayer()
      Get the default layer for the Pad.
      Returns:
      Default shape Layer.
    • addLayer

      public int addLayer()
      Create and add a new Layer to the Pad`s layers
      Returns:
      idx The new Layer`s index
    • removeLayer

      public void removeLayer(int idx)
      Remove a Layer given the layer index. Do nothing when index of Layer is 0, the default Layer
      Parameters:
      idx - Index of Layer to remove.
    • getLayer

      public Layer getLayer(int idx)
      Get the named Layer from the layers Map
      Parameters:
      idx - The index of the Layer
      Returns:
      Layer at index idx
    • close

      public void close()
      Close the Pad as if by clicking the [X] close button.
    • finalize

      protected void finalize() throws Throwable
      Free all resources when Pad object is garbage collected.
      Overrides:
      finalize in class Object
      Throws:
      Throwable - May throw exceptions
    • getPadWidth

      public int getPadWidth()
      Return the current width of the drawing area
      Returns:
      Width of the drawing area
    • getPadHeight

      public int getPadHeight()
      Return the current height of the drawing area
      Returns:
      Height of the drawing area
    • setBackground

      public void setBackground(double red, double green, double blue)
      Set the background color for the Pad
      Parameters:
      red - The background color red component
      green - The background color green component
      blue - The background color blue component
    • setBackground

      public void setBackground(double gray)
      Set the background color for the Pad to a gray value
      Parameters:
      gray - The background gray scale
    • getBackground

      public Color getBackground()
      Return the current background color used to clear the Pad
      Overrides:
      getBackground in class Window
      Returns:
      Color object used for the Pad background
    • getShapes

      public ArrayList<Shape> getShapes()
      Return an ArrayList of Shape objects currently managed by the Pad instance
      Returns:
      An ArrayList<Shape> of all Shapes being managed
    • iterator

      public Iterator<Shape> iterator()
      Return an iterator for loop over Shapes
      Specified by:
      iterator in interface Iterable<Shape>
      Returns:
      Iterator for Shapes
    • repaint

      public void repaint()
      Override Component repaint method
      Overrides:
      repaint in class Component
    • redraw

      public void redraw()
      Force a redraw of all Shapes
    • addShape

      public void addShape(Shape s)
      Add a shape to the shapes list
      Parameters:
      s - The Shape object to add to the Pad
    • removeShape

      public void removeShape(Shape s)
      Remove a shape from the shapes list
      Parameters:
      s - The Shape to remove
    • clear

      public void clear()
      Remove all shapes from the Pad
    • setEventsEnabled

      public void setEventsEnabled(boolean enabled)
      Enable or disable add Pad events.
      Parameters:
      enabled - True to enable. False to disable
    • setImmediateMode

      public void setImmediateMode(boolean mode)
      Set the immediateMode flag
      Parameters:
      mode - true or false
    • getImmediateMode

      public boolean getImmediateMode()
      Return the current value of the immediateMode flag
      Returns:
      Gets the Pad's immediate mode state. If true, all graphical updates will trigger and redraw. If false, not draw will happen until redraw() is called explicitly.
    • getTimeStamp

      public long getTimeStamp()
      Return the milliseconds since midnight, January 1, 1970 UTC.
      Returns:
      Milliseconds since midnight, January 1, 1970 UTC.
    • setTickRate

      public final void setTickRate(double tps)
      Set the rate at which to fire onTick events
      Parameters:
      tps - Ticks per second
    • getTickRate

      public final double getTickRate()
      Return the current rate at which the timer ticks
      Returns:
      A double indicating ticks per second.
    • addTickListener

      public void addTickListener(PadTickListener o)
      Add object to the list of items that are notified on Pad's timer tick action.
      Parameters:
      o - An object that implements the ActionListener interface.
    • removeTickListener

      public void removeTickListener(PadTickListener o)
      Remove object from Pad's timer tick action listener list.
      Parameters:
      o - The ActionListener object to be removed.
    • startTimer

      public void startTimer()
      Start the timer firing onTick events.
    • stopTimer

      public void stopTimer()
      Stop timer
    • toggleTimer

      public void toggleTimer()
      Toggle the state of the timer. Started/Stopped.
    • isRunning

      public boolean isRunning()
      Determine if the timer is running
      Returns:
      True or false indicating if the timer is running.
    • addMouseListener

      public void addMouseListener(PadMouseListener o)
      Add object to the list of items that are notified on Pad's mouse events.
      Parameters:
      o - The PadMouseListener object to be added.
    • removeMouseListener

      public void removeMouseListener(PadMouseListener o)
      Remove object from Pad's mouse listener list.
      Parameters:
      o - The PadMouseListener object to be removed.
    • addKeyListener

      public void addKeyListener(PadKeyListener o)
      Add object to the list of items that are notified on Pad's key events.
      Parameters:
      o - The PadKeyListener object to be added.
    • removeKeyListener

      public void removeKeyListener(PadKeyListener o)
      Remove object from Pad's key listener list.
      Parameters:
      o - The PadKeyListener object to be removed.
    • setMouseClickedHandler

      public void setMouseClickedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseClicked event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMouseDoubleClickedHandler

      public void setMouseDoubleClickedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseDoubleClicked event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMouseDraggedHandler

      public void setMouseDraggedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseDragged event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMouseEnteredHandler

      public void setMouseEnteredHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseEntered event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMouseExitedHandler

      public void setMouseExitedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseExited event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMouseMovedHandler

      public void setMouseMovedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseMoved event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMousePressedHandler

      public void setMousePressedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMousePressed event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setMouseReleasedHandler

      public void setMouseReleasedHandler(Pad.PadMouseEventHandler handler)
      Assign an onMouseReleased event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setKeyPressedHandler

      public void setKeyPressedHandler(Pad.PadKeyEventHandler handler)
      Assign an onKeyPressed event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setKeyReleasedHandler

      public void setKeyReleasedHandler(Pad.PadKeyEventHandler handler)
      Assign an onKeyReleased event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setKeyTypedHandler

      public void setKeyTypedHandler(Pad.PadKeyTypedEventHandler handler)
      Assign an onKeyTyped event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • setTickHandler

      public void setTickHandler(Pad.PadTimerEventHandler handler)
      Assign an onTick event handler using a method reference.
      Parameters:
      handler - Method reference to an event handler
    • toFront

      public void toFront(Shape s)
      Move shape to the front of diagram. Assumes Shape is on default layer 0.
      Parameters:
      s - The Shape to bring to the front of the display list.
    • toBack

      public void toBack(Shape s)
      Move shape to the back of diagram
      Parameters:
      s - The Shape to move to the back of the display list.
    • rotate

      public void rotate(double angle)
      Add a rotation angle to Pad transform.
      Parameters:
      angle - Adds the rotation angle to the current transform (degrees)
    • rotate

      public void rotate(double angle, double cx, double cy)
      Add a rotation angle to Pad transform.
      Parameters:
      angle - Adds the rotation angle to the current transform (degrees)
      cx - x-coordinate of point about which rotation occurs
      cy - y-coordinate of point about which rotation occurs
    • translate

      public void translate(double deltaX, double deltaY)
      Add a translate to Pad transform.
      Parameters:
      deltaX - Translate shape in the x-direction by deltaX
      deltaY - Translate shape in the y-direction by deltaY
    • scale

      public void scale(double factor)
      Add a scale factor to Pad transform.
      Parameters:
      factor - Scale the shape by a scale factor
    • scale

      public void scale(double factor, double cx, double cy)
      Add a scale factor to Pad transform.
      Parameters:
      factor - Scale the shape by a scale factor
      cx - x-coordinate of point about which scaling occurs
      cy - y-coordinate of point about which scaling occurs
    • scale

      public void scale(double xFactor, double yFactor)
      Add a scale factor to Pad transform.
      Parameters:
      xFactor - Scale the shape in the x-direction by a xFactor
      yFactor - Scale the shape in the y-direction by a yFactor
    • scale

      public void scale(double xFactor, double yFactor, double cx, double cy)
      Add a scale factor to Pad transform.
      Parameters:
      xFactor - Scale the shape in the x-direction by a xFactor
      yFactor - Scale the shape in the y-direction by a yFactor
      cx - x-coordinate of point about which scaling occurs
      cy - y-coordinate of point about which scaling occurs
    • reset

      public void reset()
      Resets the Pad to have no transformation.
    • getTransform

      public AffineTransform getTransform()
      Clone the Pad's default Layer AffineTransform.
      Returns:
      The cloned AffineTransform.
    • setTransform

      public void setTransform(AffineTransform transform)
      Set a new AffineTransform for the Pad's default Layer.
      Parameters:
      transform - The new transform.
    • toWindowCoords

      public double[] toWindowCoords(double x, double y)
      Convert transformed Pad coordinates to window coordinates returned as a new Point2D object. This method is useful for converting transformed coordinates received from a Pad mouse event method back to original window coordinates.
      Parameters:
      x - Transformed x-coordinate
      y - Transformed y-coordinate
      Returns:
      A Point2D object containing the transformed coordinates
    • toWindowCoords

      public Point toWindowCoords(Point pt)
      Convert transformed Pad coordinates in a Point2D object to window coordinates returned as a new Point2D object. This method is useful for converting transformed coordinates received from a Pad mouse event method back to original window coordinates.
      Parameters:
      pt - A Point object
      Returns:
      A new Point object containing the transformed coordinates
    • startListening

      public void startListening(int port)
      Start listening for new network connection requests.
      Parameters:
      port - The port on which to listen.
    • stopListening

      public void stopListening()
      Stop listening for new network connection requests.
    • openConnection

      public int openConnection(String host, int port)
      Open a connection with a listening socket server.
      Parameters:
      host - The host name to connect with.
      port - The port number on the host through which to connect.
      Returns:
      id of the open socket
    • connectionExists

      public boolean connectionExists(int id)
      Returns true of a connection exists with the id
      Parameters:
      id - A unique integer id identifying the connection.
      Returns:
      A boolean indicating whether or not the connection exists.
    • getConnectionIds

      public Set<Integer> getConnectionIds()
      Return the set of current connection Ids.
      Returns:
      A Set<Integer> holding all current connection Ids.
    • send

      public void send(int id, String msg)
      Send a message to the client given a connection id.
      Parameters:
      id - The id of the connection.
      msg - The message to send.
    • broadcast

      public void broadcast(String msg)
      Broadcast a message to all open network connections.
      Parameters:
      msg - The message to broadcast.
    • closeConnection

      public void closeConnection(int id)
      Close the identified network connection.
      Parameters:
      id - ID of the connection to close.
    • closeAllConnections

      public void closeAllConnections()
      Close all open network connections.
    • getNumConnections

      public int getNumConnections()
      Return the number of open network connections.
      Returns:
      Number of open connections.
    • getIPv4Addresses

      public List<String> getIPv4Addresses() throws SocketException
      Collect and return all IPv4 addresses for this computer
      Returns:
      List<String> of IPv4 addresses
      Throws:
      SocketException - May throw SocketExceptions
    • getIPv6Addresses

      public List<String> getIPv6Addresses() throws SocketException
      Collect and return all IPv6 addresses for this computer
      Returns:
      List<String> of IPv6 addresses
      Throws:
      SocketException - May throw SocketExceptions
    • invokeOnDispatch

      public void invokeOnDispatch(Pad.DispatchMethodNoParameters meth)
      Invoke a method on the GUI event dispatch thread
      Parameters:
      meth - A method implementing the DispatchMethodNoParameters interface, which is a method taking no parameters.
    • onServerStarted

      public void onServerStarted()
      Invoked when the listening server starts
    • onServerStopped

      public void onServerStopped()
      Invoked when the listening server stops and all connections are closed
    • onServerInfo

      public void onServerInfo(String msg)
      Invoked when the listening server has some information to share
      Parameters:
      msg - A message containing information to share
    • onServerError

      public void onServerError(String msg)
      Invoked when the listening server has an error
      Parameters:
      msg - A message containing the error message
    • onClientOpened

      public void onClientOpened(int id)
      Invoked when a new client connection opens
      Parameters:
      id - Unique id for the client connection
    • onClientClosed

      public void onClientClosed(int id)
      Invoked when a client connection is closed
      Parameters:
      id - Unique of the client id that is closed
    • onClientReceived

      public void onClientReceived(int id, String msg)
      Invoked when a connected client socket receives a message
      Parameters:
      id - Unique client id
      msg - Message String received
    • onClientInfo

      public void onClientInfo(int id, String msg)
      Invoked when a connected client socket has some information to share
      Parameters:
      id - Unique client id
      msg - Message from the client
    • onClientError

      public void onClientError(int id, String msg)
      Invoked when a connected client socket has has error
      Parameters:
      id - Unique client id
      msg - Error message from the client
    • onKeyTyped

      public void onKeyTyped(char keyChar)
      A method that can be overridden to handle key typed events
      Parameters:
      keyChar - The char of the key typed
    • onKeyPressed

      public void onKeyPressed(String keyText, String keyModifiers)
      A method that can be overridden to handle key pressed events
      Parameters:
      keyText - Text of the key pressed
      keyModifiers - Special keys pressed
    • onKeyReleased

      public void onKeyReleased(String keyText, String keyModifiers)
      A method that can be overridden to handle key released events
      Parameters:
      keyText - Text of the key pressed
      keyModifiers - Special keys pressed
    • onMousePressed

      public void onMousePressed(double x, double y, int button)
      A method that can be overridden to handle mouse pressed events
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseReleased

      public void onMouseReleased(double x, double y, int button)
      A method that can be overridden to handle mouse released events
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseClicked

      public void onMouseClicked(double x, double y, int button)
      A method that can be overridden to handle mouse clicked events. A mouse clicked event is composed of a mouse pressed event followed by a mouse released event at the same mouse location.
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseDoubleClicked

      public void onMouseDoubleClicked(double x, double y, int button)
      A method that can be overridden to handle mouse double-clicked events.
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseEntered

      public void onMouseEntered(double x, double y, int button)
      A method that can be overridden to handle mouse entered events
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseExited

      public void onMouseExited(double x, double y, int button)
      A method that can be overridden to handle mouse exited events
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseMoved

      public void onMouseMoved(double x, double y, int button)
      A method that can be overridden to handle mouse moved events
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onMouseDragged

      public void onMouseDragged(double x, double y, int button)
      A method that can be overridden to handle mouse dragged events. A mouse dragged event is the same as a mouse moved event while the mouse is pressed.
      Parameters:
      x - x-location
      y - y-location
      button - The button pressed
    • onTick

      public void onTick(long when)
      A method that can be overridden to handle the Pad timer`s tick event. The timer tick rate is set with the setTickRate() method. The timer is started by invoking the startTimer() method. The timer is stopped by invoking the stopTimer() method.
      Parameters:
      when - The difference in milliseconds between the timestamp of when this event occurred and midnight, January 1, 1970 UTC.