Class TLcdAWTUtil

java.lang.Object
com.luciad.gui.TLcdAWTUtil

public class TLcdAWTUtil extends Object
A collection of convenience methods for AWT/Swing.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Code for center string alignment.
    static final int
    Code for left string alignment.
    static final int
    Code for right string alignment.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addComponent(Container aParent, Component aChild)
    Replacement for aParent.add(aChild) that also works if the parent is already visible on screen.
    static void
    addComponent(Container aParent, Component aChild, Object aLayoutConstraints)
    Replacement for aParent.add(aChild, aLayoutConstraints) that also works if the parent is already visible on screen.
    static void
    Centers a window in its parent window.
    static void
    Center a window on the screen.
    static void
    changeStrokeToPlainLine(Graphics aGraphics, float aWidth)
    Sets up a solid BasicStroke with the specified line width.
    static void
    changeStrokeToPlainLine(Graphics aGraphics, int aWidth)
    Sets up a solid BasicStroke with the specified line width.
    static Point
    convertPoint(Component source, Point aPoint, Component destination)
    Convert a aPoint in source coordinate system to destination coordinate system.
    static boolean
    dispatchAWTEvent(AWTEvent aEvent, Object aRecipient)
    Dispatches AWT mouse, key and component events to the given recipient.
    static void
    drawFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
    Draws a relief frame on position aX,aY with width aWidth and height aHeight.
    static void
    drawPoints(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
    Draw a rectangle of points on position x,y with width w and height h.
    static void
    drawReliefFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
    Draws a relief frame on position aX,aY with width aWidth and height aHeight.
    static void
    drawReliefPoints(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
    Draw a rectangle of points on position x,y with width w and height h.
    static int
    drawString(String aString, int aAlignment, Graphics aGraphics, int x, int y, int w, int h)
    Draws a string aligned in a rectangle on the position x,y with width w and height h.
    static int
    drawString(String aString, int aAlignment, Graphics aGraphics, Rectangle aClip)
    Draws a string aligned from the left in a rectangle aClip.
    static int
    drawString(String aString, Graphics aGraphics, int x, int y, int w, int h)
    Draws a string aligned to the left in a rectangle on the position x,y with width w and height h.
    static int
    drawString(String aString, Graphics aGraphics, Rectangle aClip)
    Draws a string aligned from the left in a rectangle aClip.
    static void
    drawUnReliefFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
    Draws a relief frame on position aX,aY with width aWidth and height aHeight.
    static Container
    Find the parent container of a Component.
    static Frame
    Find the parent frame of a Component.
    static Frame
    Find the parent frame of a MenuItem.
    static Frame
    Find the parent frame of an object.
    static Window
    Find the parent window of a Component.
    static Window
    Find the parent window of an object.
    static void
    Invokes the given runnable on the event dispatch thread, similar to EventQueue.invokeAndWait or SwingUtilities.invokeAndWait.
    static <T> T
    invokeAndWait(Callable<T> aCallable)
    Invokes the given callable on the event dispatch thread, similar to EventQueue.invokeAndWait or SwingUtilities.invokeAndWait, but it also allows to return a computed result.
    static void
    Invokes the given runnable on the event dispatch thread, similar to EventQueue.invokeAndWait or SwingUtilities.invokeAndWait.
    static void
    invokeLater(Runnable aRunnable)
    Equivalent to EventQueue.invokeLater(Runnable).
    static void
    Invokes the given runnable if the calling thread is the dispatch thread and queues it for later execution otherwise.
    static boolean
    Equivalent to EventQueue.isDispatchThread().
    static void
    Replacement for aParent.remove(aChild) that also works if the parent is already visible on screen.
    static void
     
    static void
    rotate(Graphics aGraphics, double theta, double x, double y)
    Rotates the Graphics aGraphics by translating to the specified location, rotating by the specified radians theta, and translating back by the same amount as the original translation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TLcdAWTUtil

      public TLcdAWTUtil()
  • Method Details

    • findParentFrame

      public static Frame findParentFrame(Object aObject)
      Find the parent frame of an object. When the object is an instance of EventObject, this method returns the parent frame of the source of the EventObject.
      Parameters:
      aObject - the object to find the parent frame of.
      Returns:
      the frame this object is displayed in, or in the case of an event, the source of the event is displayed in.
    • findParentWindow

      public static Window findParentWindow(Object aObject)
      Find the parent window of an object. When the object is an instance of EventObject, this method returns the parent window of the source of the EventObject.
      Parameters:
      aObject - the object to find the parent window of.
      Returns:
      the window this object is displayed in, or in the case of an event, the source of the event is displayed in.
    • centerWindow

      public static void centerWindow(Window aWindow)
      Centers a window in its parent window. If the parent window is null, the window is centered on the screen.
      Parameters:
      aWindow - the Window to center.
    • centerWindowOnScreen

      public static void centerWindowOnScreen(Window aWindow)
      Center a window on the screen.
      Parameters:
      aWindow - the Window to center on the screen.
    • findParentFrame

      public static Frame findParentFrame(MenuItem aMenuItem)
      Find the parent frame of a MenuItem.
      Parameters:
      aMenuItem - the MenuItem to find the parent frame of.
      Returns:
      he parent frame of the MenuItem.
    • findParentFrame

      public static Frame findParentFrame(Component aComponent)
      Find the parent frame of a Component.
      Parameters:
      aComponent - the component to find the parent frame of.
      Returns:
      the parent frame of the component.
    • findParentWindow

      public static Window findParentWindow(Component aComponent)
      Find the parent window of a Component.
      Parameters:
      aComponent - the Component to find the parent Window of.
      Returns:
      the parent window of a Component.
    • findParentContainer

      public static Container findParentContainer(Component aComponent)
      Find the parent container of a Component.
      Parameters:
      aComponent - the component to find the parent container of.
      Returns:
      the parent container of a component.
    • drawString

      public static int drawString(String aString, int aAlignment, Graphics aGraphics, Rectangle aClip)
      Draws a string aligned from the left in a rectangle aClip. If the string does not fit into the rectangle, the string is abbreviated.
      Parameters:
      aString - the String to draw.
      aAlignment - the alignment to use to draw the String (LEFT, CENTER or RIGHT ).
      aGraphics - the Graphics to draw the String on.
      aClip - the rectangle to fit the String in.
    • drawString

      public static int drawString(String aString, Graphics aGraphics, Rectangle aClip)
      Draws a string aligned from the left in a rectangle aClip. If the string does not fit into the rectangle, the string is abbreviated.
      Parameters:
      aString - the String to draw.
      aGraphics - the Graphics to draw the String on.
      aClip - the rectangle to fit the String in.
    • drawString

      public static int drawString(String aString, Graphics aGraphics, int x, int y, int w, int h)
      Draws a string aligned to the left in a rectangle on the position x,y with width w and height h. If the string does not fit into the rectangle, the string is abbreviated.
      Parameters:
      aString - the String to draw.
      aGraphics - the Graphics to draw the String on.
      x - the x position of the baseline of the leftmost character in this graphics context's coordinate system.
      y - the y position of the baseline of the leftmost character in this graphics context's coordinate system.
      w - the width of the rectangle to fit the text in.
      h - the height of the rectangle to fit the text in.
    • drawReliefFrame

      public static void drawReliefFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
      Draws a relief frame on position aX,aY with width aWidth and height aHeight. The frame looks like it pops up from the background.
      Parameters:
      aGraphics - the Graphics to draw a frame on.
      aX - the aX coordinate of the top left point of the frame.
      aY - the aY coordinate of the top left point of the frame.
      aWidth - the width of the frame.
      aHeight - the height of the frame.
    • drawReliefPoints

      public static void drawReliefPoints(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
      Draw a rectangle of points on position x,y with width w and height h.
      Parameters:
      aGraphics - the Graphics to draw a frame on.
      aX - the x coordinate of the top left point of the frame.
      aY - the y coordinate of the top left point of the frame.
      aWidth - the width of the frame.
      aHeight - the height of the frame.
    • drawPoints

      public static void drawPoints(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
      Draw a rectangle of points on position x,y with width w and height h.
      Parameters:
      aGraphics - the Graphics to draw a frame on.
      aX - the x coordinate of the top left point of the frame.
      aY - the y coordinate of the top left point of the frame.
      aWidth - the width of the frame.
      aHeight - the height of the frame.
    • drawUnReliefFrame

      public static void drawUnReliefFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
      Draws a relief frame on position aX,aY with width aWidth and height aHeight. The frame looks like it is pressed into the background.
      Parameters:
      aGraphics - the Graphics to draw a frame on.
      aX - the x coordinate of the top left point of the frame.
      aY - the y coordinate of the top left point of the frame.
      aWidth - the width of the frame.
      aHeight - the height of the frame.
    • drawFrame

      public static void drawFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
      Draws a relief frame on position aX,aY with width aWidth and height aHeight.
      Parameters:
      aGraphics - the Graphics to draw a frame on.
      aX - the x coordinate of the top left point of the frame.
      aY - the y coordinate of the top left point of the frame.
      aWidth - the width of the frame.
      aHeight - the height of the frame.
    • drawString

      public static int drawString(String aString, int aAlignment, Graphics aGraphics, int x, int y, int w, int h)
      Draws a string aligned in a rectangle on the position x,y with width w and height h. If the string does not fit into the rectangle, the string is abbreviated.
      Parameters:
      aString - the String to draw.
      aAlignment - the alignment to use to draw the String (LEFT, CENTER or RIGHT ).
      aGraphics - the Graphics to draw the String on.
      x - the x position of the baseline of the leftmost character in this graphics context's coordinate system.
      y - the y position of the baseline of the leftmost character in this graphics context's coordinate system.
      w - the width of the rectangle to fit the text in.
      h - the height of the rectangle to fit the text in.
    • convertPoint

      public static Point convertPoint(Component source, Point aPoint, Component destination)
      Convert a aPoint in source coordinate system to destination coordinate system. If sourceis null, aPoint is assumed to be in destination's root component coordinate system. If destinationis null, aPoint will be converted to source's root component coordinate system. If both source and destination are null, return aPoint without any conversion. If both source and destination are non null and does not share a common ancestor, an error will be thrown.
      Parameters:
      source - the source component.
      aPoint - point denoting the position.
      destination - the destination component.
    • resetGraphics

      public static void resetGraphics(Graphics aGraphics)
    • rotate

      public static void rotate(Graphics aGraphics, double theta, double x, double y)
      Rotates the Graphics aGraphics by translating to the specified location, rotating by the specified radians theta, and translating back by the same amount as the original translation. This is equivalent to the following sequence of calls: translate(x, y); rotate(theta); translate(-x, -y); Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
      Parameters:
      aGraphics - the Graphics to rotate.
      theta - the angle to rotate.
      x - the x coordinate of the AWT point to rotate around.
      y - the y coordinate of the AWT point to rotate around.
    • changeStrokeToPlainLine

      public static void changeStrokeToPlainLine(Graphics aGraphics, int aWidth)
      Sets up a solid BasicStroke with the specified line width. In other words, lines drawn after this call will be solid (i.e. without dashes) and aWidth pixels wide.
      Parameters:
      aGraphics - the Graphics to change the stroke on.
      aWidth - the width of the line after changing strokes.
    • changeStrokeToPlainLine

      public static void changeStrokeToPlainLine(Graphics aGraphics, float aWidth)
      Sets up a solid BasicStroke with the specified line width. In other words, lines drawn after this call will be solid (i.e. without dashes) and aWidth pixels wide.
      Parameters:
      aGraphics - the Graphics to change the stroke on.
      aWidth - the width of the line after changing strokes.
    • invokeLater

      public static void invokeLater(Runnable aRunnable)

      Equivalent to EventQueue.invokeLater(Runnable).

      Parameters:
      aRunnable - the Runnable to run.
    • isDispatchThread

      public static boolean isDispatchThread()

      Equivalent to EventQueue.isDispatchThread().

      Returns:
      true if the current thread is an AWT event dispatching thread.
    • invokeAndWait

      public static void invokeAndWait(Runnable aRunnable)

      Invokes the given runnable on the event dispatch thread, similar to EventQueue.invokeAndWait or SwingUtilities.invokeAndWait.

      The difference is that this method tests if it is running on the event dispatch thread (=AWT thread). If so, it just calls aRunnable.run() instead of throwing a java.lang.Error. If not running on the event dispatch thread, it behaves just as EventQueue.invokeAndWait: it queues the runnable on the dispatch thread and blocks the calling thread until the runnable has finished.

      It does not throw checked exceptions, instead exceptions that happen inside the Runnable are wrapped in a RuntimeException and re-thrown, InterruptedExceptions are handled by re-interrupting the calling thread.

      Parameters:
      aRunnable - The code to perform.
      Throws:
      RuntimeException - If an exception occurred in the given runnable.
    • invokeAndWait

      public static <T> T invokeAndWait(Callable<T> aCallable)

      Invokes the given callable on the event dispatch thread, similar to EventQueue.invokeAndWait or SwingUtilities.invokeAndWait, but it also allows to return a computed result.

      The other difference is that this method tests if it is running on the event dispatch thread (=AWT thread). If so, it just calls aCallable.call() instead of throwing a java.lang.Error. If not running on the event dispatch thread, it behaves just as EventQueue.invokeAndWait: it queues the runnable on the dispatch thread and blocks the calling thread until the runnable has finished.

      It does not throw checked exceptions, instead exceptions that happen inside the Callable are wrapped in a RuntimeException and re-thrown, InterruptedExceptions are handled by re-interrupting the calling thread.

      Parameters:
      aCallable - The code to perform.
      Throws:
      RuntimeException - If an exception occurred in the given runnable.
    • invokeAndWaitWithExc

      public static void invokeAndWaitWithExc(Runnable aRunnable) throws InterruptedException, InvocationTargetException

      Invokes the given runnable on the event dispatch thread, similar to EventQueue.invokeAndWait or SwingUtilities.invokeAndWait.

      The difference is that this method tests if it is running on the event dispatch thread (=AWT thread). If so, it just calls aRunnable.run() instead of throwing a java.lang.Error. If not running on the event dispatch thread, it behaves just as EventQueue.invokeAndWait: it queues the runnable on the dispatch thread and blocks the calling thread until the runnable has finished.

      Parameters:
      aRunnable - The code to perform.
      Throws:
      InterruptedException - See EventQueue.invokeAndWait.
      InvocationTargetException - See EventQueue.invokeAndWait.
    • invokeNowOrLater

      public static void invokeNowOrLater(Runnable aRunnable)

      Invokes the given runnable if the calling thread is the dispatch thread and queues it for later execution otherwise.

      Its behavior is similar to this code fragment:

      
       if ( EventQueue.isDispatchThread() ) {
         aRunnable.run();
       }
       else {
         EventQueue.invokeLater( aRunnable );
       }
       

      Parameters:
      aRunnable - The code to perform.
    • addComponent

      public static void addComponent(Container aParent, Component aChild)

      Replacement for aParent.add(aChild) that also works if the parent is already visible on screen. Refer to addComponent(Container, Component, Object) for more information.

      Parameters:
      aParent - The parent component.
      aChild - The child to add to the parent.
    • addComponent

      public static void addComponent(Container aParent, Component aChild, Object aLayoutConstraints)

      Replacement for aParent.add(aChild, aLayoutConstraints) that also works if the parent is already visible on screen.

      Whenever child components are added to or removed from a Swing component that is already showing on screen, care must be taken to call the revalidate() and repaint() methods. When components are added or removed prior to showing the parent - the most common case - the revalidate() and repaint() methods don't have to be called. This is a utility method to do all that in one go; it is safe to be used for both already showing and still hidden parents.

      When setVisible() is used on the child component to make it appear or not (instead of adding/removing), no special care is needed either as Swing automatically calls revalidate().

      These rules are general Swing considerations. Not appropriately calling the revalidate() method usually manifests as rendering artifacts that disappear as soon as an explicit re-layout and repaint are triggered, for example by resizing the parent.

      Note that Container.add offers more overloaded alternatives, if any of those is needed it is recommended to not use this utility method and call revalidate and repaint explicitly after having called Container.add.

      Parameters:
      aParent - The parent component.
      aChild - The child to add to the parent.
      aLayoutConstraints - The layout constraints for the child. For example BorderLayout.NORTH when using BorderLayout.
      See Also:
    • removeComponent

      public static void removeComponent(Container aParent, Component aChild)

      Replacement for aParent.remove(aChild) that also works if the parent is already visible on screen.

      See addComponent(Container, Component, Object) for additional information.

      Parameters:
      aParent - The parent component.
      aChild - The child to remove.
    • dispatchAWTEvent

      public static boolean dispatchAWTEvent(AWTEvent aEvent, Object aRecipient)
      Dispatches AWT mouse, key and component events to the given recipient. The following interfaces are supported:
      • MouseListener
      • MouseMotionListener
      • MouseWheelListener
      • KeyListener
      • ComponentListener
      Returns:
      true if the event was recognized and could be dispatched to the appropriate interface method, false otherwise
      Since:
      2019.1