9.6. A Useful Class

As promised earlier, this section contains a class that's full of handy static methods. The class is called Utilities and contains the following methods:

public static boolean waitForImage(Image image)

This method waits for the given image to load fully. If everything goes well, it returns true. If there is an error loading the image, it returns false.

public static Image blockingLoad(String path)

public static Image blockingLoad(URL url)

These methods load an image from the given file path or URL. These methods will not return until the image data is fully loaded. If there is an error while loading, null is returned.

public static BufferedImage makeBufferedImage(Image image)

This method creates a BufferedImage from the supplied Image.

public static Frame getNonClearingFrame(String name, Component c)

This method returns a Frame subclass whose update() method does not clear the Frame's drawing surface. The name parameter becomes the Frame's title, and the Frame is sized to fit the preferred size of the given component. Then the component is added to the Frame's contents. The Frame is disposed if the user tries to close it.

public static void sizeContainerToComponent(Container container, Component component)

This method resizes the given container so that it just encloses the preferred size of the given component.

public static void centerFrame(Frame f)

Use this method to place the given Frame in the center of the screen.

Here is the Utilities class itself. ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.