Sharkysoft home

lava.gui
Class GuiToolbox

java.lang.Object
  |
  +--lava.gui.GuiToolbox

public class GuiToolbox
extends java.lang.Object

Miscellaneous functions providing AWT and Swing support.

Details: GuiToolbox is a collection of functions that perform various AWT- and Swing-related tasks.

Since:
1998.??.??
Version:
1999.04.22

Constructor Summary
GuiToolbox()
           
 
Method Summary
static int blendColors(int background, int foreground)
          Blends two ARGB colors into a single ARGB color.
static javax.swing.JButton buildJButton(ButtonImages bi)
          Constructs JButton from ButtonImages.
static javax.swing.JToggleButton buildJToggleButton(ButtonImages bi)
          Constructs JToggleButton from ButtonImages.
static java.awt.Rectangle centerRectangle(java.awt.Point reference, java.awt.Rectangle moveable)
          Centers a rectangle on a point.
static java.awt.Rectangle centerRectangle(java.awt.Rectangle reference, java.awt.Rectangle moveable)
          Centers one rectangle with another.
static java.awt.Dimension computeDifference(java.awt.Point from, java.awt.Point to)
          Computes translation between points.
static void configureFaces(javax.swing.AbstractButton b, ButtonImages bi)
          Sets Swing button icons.
static java.awt.Image createImage(java.lang.String filename)
          Creates an image from a filename.
static java.awt.Image cropImage(java.awt.Image srcimg, java.awt.Rectangle rect)
           
static float distance(java.awt.Point p1, java.awt.Point p2)
          Computes distance between points.
static void drawLine(java.awt.Graphics g, java.awt.Point p1, java.awt.Point p2)
          Draws line.
static void drawString(java.awt.Graphics g, java.lang.String s, java.awt.Point p)
           
static void drawStringAligned(java.awt.Graphics g, java.lang.String s, java.awt.Point p, int a)
           
static void drawStringAligned(java.awt.Graphics g, java.lang.String s, java.awt.Rectangle r, int a)
           
static java.awt.Point getCenter(java.awt.Rectangle rect)
          Computes the center of a rectangle.
static int getComponentZOrder(java.awt.Container container, java.awt.Component component)
          Returns component Z-order.
static java.io.File getFile(java.awt.FileDialog fd)
          Uses the supplied FileDialog window to obtain a file selection from the user.
static java.util.Enumeration getMenuItems(java.awt.Menu menu)
          Returns an enumeration of the given menu's MenuItems.
static java.util.Enumeration getMenuItems(java.awt.MenuBar menubar)
          Returns an enumeration of all of the given menu bar's MenuItems.
static java.util.Enumeration getMenus(java.awt.MenuBar menubar)
          Returns an enumeration of the given menu bar's menus.
static java.awt.Container getRootContainer(java.awt.Component comp)
          SUMMARY FORTHCOMING
static int getStringAdvance(java.awt.FontMetrics fm, java.lang.String s)
          Returns the advance width of a String when printed using the font with the given FontMetrics.
static int getStringAdvance(java.awt.Graphics g, java.lang.String s)
          Returns the advance width of a String when drawn through the current Graphics object.
static boolean intersects(java.awt.Polygon p, java.awt.Rectangle r)
          No summary provided.
static java.awt.Image loadImage(java.lang.String resname)
          Loads image from classpath.
static java.awt.Dimension max(java.awt.Dimension d1, java.awt.Dimension d2)
           
static java.awt.Dimension min(java.awt.Dimension d1, java.awt.Dimension d2)
           
static boolean orderedSizes(java.awt.Dimension dim1, java.awt.Dimension dim2)
          Determines whether two dimensions are ordered.
static boolean orderedSizes(java.awt.Dimension dim1, java.awt.Dimension dim2, java.awt.Dimension dim3)
          Determines whether three dimensions are ordered.
static java.awt.Point pointForCentering(java.lang.String text, java.awt.Point center, java.awt.Font font, java.awt.Component component)
          Calculates the draw point for centered text.
static java.awt.Point pointForCentering(java.lang.String text, java.awt.Point center, java.awt.Graphics graphics)
          Calculates the draw point for centered text.
static void repaint(java.awt.Component c, java.awt.Rectangle b)
          Invalidates rectangular region.
static void runInAwtEventThread(java.lang.Runnable r)
          Executes Runnable in AWT event thread.
static java.awt.Rectangle scaleRectangle(java.awt.Dimension oldsize, java.awt.Dimension newsize, java.awt.Rectangle rect)
           
static java.awt.Dimension waitForDimensions(java.awt.Image image)
          Waits for an image's dimensions.
static boolean waitForImage(java.awt.Image i)
          Waits for an image to load.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GuiToolbox

public GuiToolbox()
Method Detail

blendColors

public static final int blendColors(int background,
                                    int foreground)
Blends two ARGB colors into a single ARGB color.
Parameters:
background - the background color
foreground - the foreground color
Returns:
the combined color

getStringAdvance

public static final int getStringAdvance(java.awt.Graphics g,
                                         java.lang.String s)
Returns the advance width of a String when drawn through the current Graphics object.
Parameters:
g - the graphics context
s - the string
Returns:
the advanced width

getStringAdvance

public static final int getStringAdvance(java.awt.FontMetrics fm,
                                         java.lang.String s)
Returns the advance width of a String when printed using the font with the given FontMetrics.
Parameters:
fm - the graphics context
s - the string
Returns:
the advanced width

waitForImage

public static final boolean waitForImage(java.awt.Image i)
Waits for an image to load.

Details: waitForImage completely loads the specified image (i). This class is useful in situations where images load progressively but progressive display is not desired. true is returned if the image loads successfully, false otherwise.

Parameters:
i - the image to wait on
Returns:
true iff the image loads successfully
Since:
1998.10.06

pointForCentering

public static java.awt.Point pointForCentering(java.lang.String text,
                                               java.awt.Point center,
                                               java.awt.Font font,
                                               java.awt.Component component)
Calculates the draw point for centered text.

Details: This method determines the total width and height of text, drawn using the given Font and Component. It then computes the text draw coordinate (for use in Graphics.drawString, etc.) that will result in text centered around the given center point, center.

Parameters:
text - the text
center - the desired center point
font - the font
component - the component to which the text will be drawn
Returns:
the text draw coordinate for centering
Since:
1998.10.10

pointForCentering

public static java.awt.Point pointForCentering(java.lang.String text,
                                               java.awt.Point center,
                                               java.awt.Graphics graphics)
Calculates the draw point for centered text.

Details: This method determines the total width and height of text, drawn using the given Graphics object. It then computes the text draw coordinate (for use in graphics.drawString, etc.) that will produced centered text around the given center point, center.

Parameters:
text - the text
center - the desired center point
graphics - the graphics context
Returns:
the text draw coordinate for centering
Since:
1998.10.10

waitForDimensions

public static java.awt.Dimension waitForDimensions(java.awt.Image image)
Waits for an image's dimensions.

Details: Many types of images are loaded asynchronously. This applies not only to the image's pixels, but also to other information about the image, such as its width and height. This function waits for the given image's width and height to come in, and then returns with a Dimension of those values. It will not return until the image's dimensions have been determined, or until the asynchronous image loader has given up, due to an error. If an error occurs, this method returns null. If image is null, null is returned.

Parameters:
image - the image
Returns:
the image's size
Since:
1998.10.10

getMenus

public static java.util.Enumeration getMenus(java.awt.MenuBar menubar)
Returns an enumeration of the given menu bar's menus.
Parameters:
menubar - the menu bar
Returns:
the list of menus
Since:
1988.10.11

getMenuItems

public static java.util.Enumeration getMenuItems(java.awt.Menu menu)
Returns an enumeration of the given menu's MenuItems.
Parameters:
menu - the menu
Returns:
the list of MeniItems
Since:
1988.10.11

getMenuItems

public static java.util.Enumeration getMenuItems(java.awt.MenuBar menubar)
Returns an enumeration of all of the given menu bar's MenuItems.

Details: getMenuItems recursively searches menubar's menus to build an Enumeration of all MenuItems. The Enumeration is then returned.

Parameters:
menubar - the menu bar
Returns:
an Enumeration of menubar's menuitems
Since:
1988.10.11

getFile

public static java.io.File getFile(java.awt.FileDialog fd)
Uses the supplied FileDialog window to obtain a file selection from the user.

Details: getFile uses the supplied FileDialog window to obtain a file selection from the user, and does not return until the selection is made or cancelled. The value returned is a File object representing the user's choice, or null if the user did not make a selection. Either way, this method disposes of the FileDialog object before returning.

Parameters:
fd - the FileDialog
Returns:
a File object corresponding to the selected file
Since:
1988.10.11

orderedSizes

public static boolean orderedSizes(java.awt.Dimension dim1,
                                   java.awt.Dimension dim2,
                                   java.awt.Dimension dim3)
Determines whether three dimensions are ordered.

Details: orderedSizes determines whether the given sizes are ordered. To be ordered, both the widths and heights must be ordered. orderedSizes returns true if dim1, dim2, and dim3 are ordered, false otherwise.

Parameters:
min1 - the smallest dimension
min2 - the middle dimension
min3 - the largest dimension
Returns:
true iff the dimensions are ordered
Since:
1998.04.19

orderedSizes

public static boolean orderedSizes(java.awt.Dimension dim1,
                                   java.awt.Dimension dim2)
Determines whether two dimensions are ordered.

Details: orderedSizes returns the result of the expression

dim1.width<=dim2.width&&dim1.height<=dim2.height

In other words, orderedSizes returns true if the dimensions are ordered, false otherwise.

Parameters:
min1 - the small dimension
min2 - the large dimension
Returns:
true iff the dimensions are ordered
Since:
1998.04.19

centerRectangle

public static java.awt.Rectangle centerRectangle(java.awt.Rectangle reference,
                                                 java.awt.Rectangle moveable)
Centers one rectangle with another.

Details: centerRectangle returns a new Rectangle that is concentric with reference and the same size as moveable.

Parameters:
reference - the rectangle defining the center
moveable - the rectangle defining the dimensions
Returns:
the centered rectangle
Since:
1999.04.20

centerRectangle

public static java.awt.Rectangle centerRectangle(java.awt.Point reference,
                                                 java.awt.Rectangle moveable)
Centers a rectangle on a point.

Details: centerRectangle returns a new Rectangle that is centered on reference and the same size as moveable.

Parameters:
reference - the center point
moveable - the rectangle defining the dimensions
Returns:
the centered rectangle
Since:
1999.04.20

getCenter

public static java.awt.Point getCenter(java.awt.Rectangle rect)
Computes the center of a rectangle.

Details: getCenter computes and returns the center of a Rectangle.

Parameters:
rect - the rectangle
Returns:
the center point
Since:
1999.04.20

createImage

public static java.awt.Image createImage(java.lang.String filename)
Creates an image from a filename.

Details: createImage uses the default Toolkit to create an image based on the given filename.

Parameters:
filename - the filename
Returns:
the new Image
Since:
1999.04.24

min

public static java.awt.Dimension min(java.awt.Dimension d1,
                                     java.awt.Dimension d2)

max

public static java.awt.Dimension max(java.awt.Dimension d1,
                                     java.awt.Dimension d2)

scaleRectangle

public static java.awt.Rectangle scaleRectangle(java.awt.Dimension oldsize,
                                                java.awt.Dimension newsize,
                                                java.awt.Rectangle rect)

drawString

public static void drawString(java.awt.Graphics g,
                              java.lang.String s,
                              java.awt.Point p)

drawStringAligned

public static void drawStringAligned(java.awt.Graphics g,
                                     java.lang.String s,
                                     java.awt.Point p,
                                     int a)

drawStringAligned

public static void drawStringAligned(java.awt.Graphics g,
                                     java.lang.String s,
                                     java.awt.Rectangle r,
                                     int a)

cropImage

public static java.awt.Image cropImage(java.awt.Image srcimg,
                                       java.awt.Rectangle rect)

configureFaces

public static void configureFaces(javax.swing.AbstractButton b,
                                  ButtonImages bi)
Sets Swing button icons.

Details: configureFaces assigns the various button state images stored in bi to the corresponding Swing button. Only non-null images in bi will be assigned to the Swing button. configureFaces also removes the border from the button.

Parameters:
b - the Swing button
bi - the image set
Since:
1999.04.29

buildJButton

public static javax.swing.JButton buildJButton(ButtonImages bi)
Constructs JButton from ButtonImages.

Details: buildJButton constructs a JButton from the given ButtonImages object. In particular, this method creates a JButton, initializes the button states by calling configureFaces, and then sets the button's size to be equal to the size of the out-cold face.

Parameters:
bi - the button faces
Returns:
the new JButton
Since:
1999.04.29

buildJToggleButton

public static javax.swing.JToggleButton buildJToggleButton(ButtonImages bi)
Constructs JToggleButton from ButtonImages.

Details: buildJToggleButton does the same thing as buildJButton, but returns a JToggleButton instead.

Since:
1999.04.29

loadImage

public static java.awt.Image loadImage(java.lang.String resname)
Loads image from classpath.

Details: loadImage loads an image from the classpath. resname is the slash-separated resource identifier. Forward slashes (/) should always be used regardless of the host platform.

Programming your application to load images from the classpath makes it possible for your application to find its image resources even when the application is run in an alternate initial directory. This technique also makes it possible for JAR'ed applications to load images from their own JARs.

loadImage returns null if the named resource cannot be found.

Parameters:
resname - the image filename
Returns:
the Image
Since:
1999.05.30

distance

public static float distance(java.awt.Point p1,
                             java.awt.Point p2)
Computes distance between points.

Details: distance computes the distance between two points using Pythagorus' theorem.

Parameters:
p1 - the first point
p2 - the second point
Returns:
the distance
Since:
1998.12.03

repaint

public static void repaint(java.awt.Component c,
                           java.awt.Rectangle b)
Invalidates rectangular region.

Details: repaint invalidates the given Rectangle on the given Component.

Parameters:
c - the Component
b - the Rectangle
Since:
1998.12.03

drawLine

public static void drawLine(java.awt.Graphics g,
                            java.awt.Point p1,
                            java.awt.Point p2)
Draws line.

Details: drawLine draws a line between the two given points using the given graphics context.

Parameters:
p1 - first point
p2 - second point
Since:
1998.12.03

intersects

public static boolean intersects(java.awt.Polygon p,
                                 java.awt.Rectangle r)
No summary provided.

Details: No details provided.

Parameters:
p - p
r - r
Returns:
boolean
Since:
1998.12.03

getRootContainer

public static java.awt.Container getRootContainer(java.awt.Component comp)
SUMMARY FORTHCOMING

Details: getRootContainer returns the root Container of the given Component, or null if the parent cannot be determined.

Parameters:
the - Component
Returns:
the root Container
Since:
1999.07.07

computeDifference

public static java.awt.Dimension computeDifference(java.awt.Point from,
                                                   java.awt.Point to)
Computes translation between points.

Details: computeDifference determines the rectangular difference between two given points. For example, if from = (2, 2) and to = (4, 5), then computeDifference returns (2, 3) in a Dimension object.

Parameters:
from - the first point
from - the second point
Returns:
the regtangular distance
Since:
2000.01.26

getComponentZOrder

public static int getComponentZOrder(java.awt.Container container,
                                     java.awt.Component component)
Returns component Z-order.

Details: This method returns the Z-order of the given component (component ) within the given container (container). If the component is in the container, the value returned will be the value n for which container . getComponent (n) returns component. Otherwise, -1 is returned. Whether low numbers are on top or high numbers are on top depends on the container implementation.

Parameters:
container - the container
component - the component
Returns:
the Z-order
Since:
2000.01.26

runInAwtEventThread

public static final void runInAwtEventThread(java.lang.Runnable r)
Executes Runnable in AWT event thread.

Details: runInAwtEventThread guarantees that the given Runnable (r) will be executed in an AWT event dispatching thread. If the thread in which this method is called is an AWT event dispatching thread, r will be executed immediately, and this method will not return until r.run has completed. If the current thread is not an AWT dispatching thread, however, r will be enqueued in an AWT dispatching thread for asynchronous execution.

No action is taken if r is null.

Parameters:
r - the Runnable
Since:
2000.02.02

Sharkysoft home