public class TLcdAWTUtil extends Object
Modifier and Type | Field and Description |
---|---|
static int |
CENTER
Code for center string alignment.
|
static int |
LEFT
Code for left string alignment.
|
static int |
RIGHT
Code for right string alignment.
|
Constructor and Description |
---|
TLcdAWTUtil() |
Modifier and Type | Method and 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 |
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 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,
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 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 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 a
Component . |
static Frame |
findParentFrame(Component aComponent)
Find the parent frame of a
Component . |
static Frame |
findParentFrame(MenuItem aMenuItem)
Find the parent frame of a
MenuItem . |
static Frame |
findParentFrame(Object aObject)
Find the parent frame of an object.
|
static Window |
findParentWindow(Component aComponent)
Find the parent window of a
Component . |
static Window |
findParentWindow(Object aObject)
Find the parent window of an object.
|
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 |
invokeAndWait(Runnable aRunnable)
Invokes the given runnable on the event dispatch thread, similar to
EventQueue.invokeAndWait or SwingUtilities.invokeAndWait . |
static void |
invokeAndWaitWithExc(Runnable aRunnable)
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 |
invokeNowOrLater(Runnable aRunnable)
Invokes the given runnable if the calling thread is the dispatch thread and queues it for
later execution otherwise.
|
static boolean |
isDispatchThread()
Equivalent to
EventQueue.isDispatchThread() . |
static void |
removeComponent(Container aParent,
Component aChild)
Replacement for
aParent.remove(aChild) that also works if the parent is already visible on screen. |
static void |
resetGraphics(Graphics aGraphics) |
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. |
public static final int LEFT
public static final int CENTER
public static final int RIGHT
public static Frame findParentFrame(Object aObject)
EventObject
, this method returns the parent frame of the
source of the EventObject
.aObject
- the object to find the parent frame of.public static Window findParentWindow(Object aObject)
EventObject
, this method returns the parent window of the
source of the EventObject
.aObject
- the object to find the parent window of.public static void centerWindow(Window aWindow)
aWindow
- the Window to center.public static void centerWindowOnScreen(Window aWindow)
aWindow
- the Window to center on the screen.public static Frame findParentFrame(MenuItem aMenuItem)
MenuItem
.aMenuItem
- the MenuItem to find the parent frame of.public static Frame findParentFrame(Component aComponent)
Component
.aComponent
- the component to find the parent frame of.public static Window findParentWindow(Component aComponent)
Component
.aComponent
- the Component to find the parent Window of.Component
.public static Container findParentContainer(Component aComponent)
Component
.aComponent
- the component to find the parent container of.public static int drawString(String aString, int aAlignment, Graphics aGraphics, Rectangle aClip)
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.public static int drawString(String aString, Graphics aGraphics, Rectangle aClip)
aString
- the String to draw.aGraphics
- the Graphics to draw the String on.aClip
- the rectangle to fit the String in.public static int drawString(String aString, Graphics aGraphics, int x, int y, int w, int h)
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.public static void drawReliefFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
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.public static void drawReliefPoints(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
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.public static void drawPoints(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
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.public static void drawUnReliefFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
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.public static void drawFrame(Graphics aGraphics, int aX, int aY, int aWidth, int aHeight)
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.public static int drawString(String aString, int aAlignment, Graphics aGraphics, int x, int y, int w, int h)
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.public static Point convertPoint(Component source, Point aPoint, Component destination)
source
- the source component.destination
- the destination component.aPoint
- point denoting the position.public static void resetGraphics(Graphics aGraphics)
public static void rotate(Graphics aGraphics, double theta, double x, double y)
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.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.public static void changeStrokeToPlainLine(Graphics aGraphics, int aWidth)
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.aGraphics
- the Graphics to change the stroke on.aWidth
- the width of the line after changing strokes.public static void changeStrokeToPlainLine(Graphics aGraphics, float aWidth)
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.aGraphics
- the Graphics to change the stroke on.aWidth
- the width of the line after changing strokes.public static void invokeLater(Runnable aRunnable)
Equivalent to EventQueue.invokeLater(Runnable)
.
aRunnable
- the Runnable to run.public static boolean isDispatchThread()
Equivalent to EventQueue.isDispatchThread()
.
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,
InterruptedException
s are handled by re-interrupting the calling thread.
aRunnable
- The code to perform.RuntimeException
- If an exception occurred in the given runnable.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,
InterruptedException
s are handled by re-interrupting the calling thread.
aCallable
- The code to perform.RuntimeException
- If an exception occurred in the given runnable.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.
aRunnable
- The code to perform.InterruptedException
- See EventQueue.invokeAndWait
.InvocationTargetException
- See EventQueue.invokeAndWait
.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 );
}
aRunnable
- The code to perform.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.
aParent
- The parent component.aChild
- The child to add to the parent.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 & 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
& repaint
explicitly after having
called Container.add
.
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
.Container.add(Component)
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.
aParent
- The parent component.aChild
- The child to remove.public static boolean dispatchAWTEvent(AWTEvent aEvent, Object aRecipient)
MouseListener
MouseMotionListener
MouseWheelListener
KeyListener
ComponentListener
true
if the event was recognized and could be dispatched to the appropriate interface method,
false
otherwise