Creating UI Graphics

Finally, don't forget about UI graphics! Mostly, you'll probably just need things such as image buttons, to give your game a particular look and feel. When designing UI graphics keep in mind the meanings that different colors imply. For example, think of the stoplight: Red means stop, green means go, yellow means caution. Try to pick colors that signify the meaning you're trying to convey. Also, use simple yet descriptive UI graphics that quickly allow the user to find out what to click on. In the example in , "Interactivity and User Interfaces," you used icons for buttons.

Customizing Swing

If you're using Swing for any part of your UI, such as text boxes for username input, you might want to change the default Swing look to suit your own needs. The most flexible way to do this is to create your own look and feel, but this can be a difficult process. Another solution is to simply change the colors of the default Java look and feel to match those of your UI. The good news is it's easy to change the color of a Swing component. All you have to do is store a value in the UI defaults table, like this:

ColorUIResource myColor = new ColorUIResource(0xf9f9f9);
UIManager.put("Button.background", myColor);


A ColorUIResource object is a subclass of the Color object that implements the UIResource interface, which Swing uses internally. Here, we set the background color of all buttons to our own color. The bad news is that there are hundreds of different color properties in Swing, including Button.shadow, Button.highlight, and several others. If you're curious what all those properties are, you can use the code in Listing 17.1 to display them all, along with their default values.

Listing 17.1 SwingDefaults.java
import java.awt.Color;
import java.util.*;
import javax.swing.UIManager;
import javax.swing.UIDefaults;
/**
 Prints a list of all the Swing defaults for the default
 look and feel.
*/
public class SwingDefaults {
 public static void main(String[] args) throws Exception {
 List allValues = new ArrayList();
 // store all the values in a list
 UIDefaults defaults = UIManager.getDefaults();
 Enumeration e = defaults.keys();
 while (e.hasMoreElements()) {
 Object key = e.nextElement();
 Object value = defaults.get(key);
 if (value instanceof Color) {
 allValues.add(key + " = " + value);
 }
 }
 // sort and print the values
 Collections.sort(allValues);
 for (int i=0; i<allValues.size(); i++) {
 System.out.println(allValues.get(i));
 }
 }
}


Note that this code displays the color properties; you can also modify things such as borders.



   
Comments