Class TLcdAWTUtil
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addComponent
(Container aParent, Component aChild) Replacement foraParent.add(aChild)
that also works if the parent is already visible on screen.static void
addComponent
(Container aParent, Component aChild, Object aLayoutConstraints) Replacement foraParent.add(aChild, aLayoutConstraints)
that also works if the parent is already visible on screen.static void
centerWindow
(Window aWindow) Centers a window in its parent window.static void
centerWindowOnScreen
(Window aWindow) Center a window on the screen.static void
changeStrokeToPlainLine
(Graphics aGraphics, float aWidth) Sets up a solidBasicStroke
with the specified line width.static void
changeStrokeToPlainLine
(Graphics aGraphics, int aWidth) Sets up a solidBasicStroke
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
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
findParentContainer
(Component aComponent) Find the parent container of aComponent
.static Frame
findParentFrame
(Component aComponent) Find the parent frame of aComponent
.static Frame
findParentFrame
(MenuItem aMenuItem) Find the parent frame of aMenuItem
.static Frame
findParentFrame
(Object aObject) Find the parent frame of an object.static Window
findParentWindow
(Component aComponent) Find the parent window of aComponent
.static Window
findParentWindow
(Object aObject) Find the parent window of an object.static void
invokeAndWait
(Runnable aRunnable) Invokes the given runnable on the event dispatch thread, similar toEventQueue.invokeAndWait
orSwingUtilities.invokeAndWait
.static <T> T
invokeAndWait
(Callable<T> aCallable) Invokes the given callable on the event dispatch thread, similar toEventQueue.invokeAndWait
orSwingUtilities.invokeAndWait
, but it also allows to return a computed result.static void
invokeAndWaitWithExc
(Runnable aRunnable) Invokes the given runnable on the event dispatch thread, similar toEventQueue.invokeAndWait
orSwingUtilities.invokeAndWait
.static void
invokeLater
(Runnable aRunnable) Equivalent toEventQueue.invokeLater(Runnable)
.static void
invokeNowOrLater
(Runnable aRunnable) Invokes the given runnable if the calling thread is the dispatch thread and queues it for later execution otherwise.static boolean
Equivalent toEventQueue.isDispatchThread()
.static void
removeComponent
(Container aParent, Component aChild) Replacement foraParent.remove(aChild)
that also works if the parent is already visible on screen.static void
resetGraphics
(Graphics aGraphics) static void
Rotates theGraphics
aGraphics by translating to the specified location, rotating by the specified radians theta, and translating back by the same amount as the original translation.
-
Field Details
-
LEFT
public static final int LEFTCode for left string alignment.- See Also:
-
CENTER
public static final int CENTERCode for center string alignment.- See Also:
-
RIGHT
public static final int RIGHTCode for right string alignment.- See Also:
-
-
Constructor Details
-
TLcdAWTUtil
public TLcdAWTUtil()
-
-
Method Details
-
findParentFrame
Find the parent frame of an object. When the object is an instance ofEventObject
, this method returns the parent frame of the source of theEventObject
.- 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
Find the parent window of an object. When the object is an instance ofEventObject
, this method returns the parent window of the source of theEventObject
.- 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
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
Center a window on the screen.- Parameters:
aWindow
- the Window to center on the screen.
-
findParentFrame
Find the parent frame of aMenuItem
.- Parameters:
aMenuItem
- the MenuItem to find the parent frame of.- Returns:
- he parent frame of the MenuItem.
-
findParentFrame
Find the parent frame of aComponent
.- Parameters:
aComponent
- the component to find the parent frame of.- Returns:
- the parent frame of the component.
-
findParentWindow
Find the parent window of aComponent
.- Parameters:
aComponent
- the Component to find the parent Window of.- Returns:
- the parent window of a
Component
.
-
findParentContainer
Find the parent container of aComponent
.- Parameters:
aComponent
- the component to find the parent container of.- Returns:
- the parent container of a component.
-
drawString
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
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
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
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
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
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
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
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
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
-
rotate
Rotates theGraphics
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
Sets up a solidBasicStroke
with the specified line width. In other words, lines drawn after this call will be solid (i.e. without dashes) andaWidth
pixels wide.- Parameters:
aGraphics
- the Graphics to change the stroke on.aWidth
- the width of the line after changing strokes.
-
changeStrokeToPlainLine
Sets up a solidBasicStroke
with the specified line width. In other words, lines drawn after this call will be solid (i.e. without dashes) andaWidth
pixels wide.- Parameters:
aGraphics
- the Graphics to change the stroke on.aWidth
- the width of the line after changing strokes.
-
invokeLater
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
Invokes the given runnable on the event dispatch thread, similar to
EventQueue.invokeAndWait
orSwingUtilities.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 ajava.lang.Error
. If not running on the event dispatch thread, it behaves just asEventQueue.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 aRuntimeException
and re-thrown,InterruptedException
s 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
Invokes the given callable on the event dispatch thread, similar to
EventQueue.invokeAndWait
orSwingUtilities.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 ajava.lang.Error
. If not running on the event dispatch thread, it behaves just asEventQueue.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 aRuntimeException
and re-thrown,InterruptedException
s 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
orSwingUtilities.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 ajava.lang.Error
. If not running on the event dispatch thread, it behaves just asEventQueue.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
- SeeEventQueue.invokeAndWait
.InvocationTargetException
- SeeEventQueue.invokeAndWait
.
-
invokeNowOrLater
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
Replacement for
aParent.add(aChild)
that also works if the parent is already visible on screen. Refer toaddComponent(Container, Component, Object)
for more information.- Parameters:
aParent
- The parent component.aChild
- The child to add to the parent.
-
addComponent
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()
andrepaint()
methods. When components are added or removed prior to showing the parent - the most common case - therevalidate()
andrepaint()
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 callsrevalidate()
.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 callrevalidate
andrepaint
explicitly after having calledContainer.add
.- Parameters:
aParent
- The parent component.aChild
- The child to add to the parent.aLayoutConstraints
- The layout constraints for the child. For exampleBorderLayout.NORTH
when usingBorderLayout
.- See Also:
-
removeComponent
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
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
-