## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

182
|
Chapter 5, Windows, Dialogs, and Frames
#35 Add Windows Resize Icons
HACK
Icon is pretty simple, and it has only three methods:
void paintIcon(Component c, Graphics g, int x, int y);
int getIconWidth( );
int getIconHeight( );
The getIconWidth( ) and getIconHeight( ) methods should be pretty easy to
implement—you just need the pixel size of your custom icons. The
paintIcon( ) method is where all the interesting stuff happens.
The Explorer Icon
Figure 5-4 shows a huge blowup of what we’ll call the Explorer icon.
At a glance, you can see the six squares in a triangular pattern with a subtle
white 3D effect on the squares. The easiest thing to handle is the size, so
start with that. This icon is 12 × 12 pixels (one square in Figure 5-4 equals
one pixel).
Figure 5-2. The Windows Explorer resize icon
Figure 5-3. The Windows MS Office resize icon
Add Windows Resize Icons #35
Chapter 5, Windows, Dialogs, and Frames
|
183
HACK
Here are the width and height methods. Start by creating constants for
height and width, and write the getters:
private static final int WIDTH = 12;
private static final int HEIGHT = 12;
public int getIconHeight( ) {
return WIDTH;
}
public int getIconWidth( ) {
return HEIGHT;
}
To implement paintIcon( ), you’ll need to recreate this icon using the
graphic primitives provided by AWT. So, you need to carefully analyze how
the original was created. After close inspection, you’ll find that each square
actually has three separate colors: the top-right color, the bottom-right
color, and the left color. Here are the color constants you need:
private static final Color SQUARE_COLOR_LEFT = new Color(184, 180, 163);
private static final Color SQUARE_COLOR_TOP_RIGHT = new Color(184, 180, 161);
private static final Color SQUARE_COLOR_BOTTOM_RIGHT = new Color(184, 181, 161);
To recreate the icon, it will help to have a method to paint the gray squares.
Notice that the method also caches
Graphics previous color and resets the
graphics color at the end of the method, which is generally a good practice:
private void drawSquare(Graphics g, int x, int y){
Color oldColor = g.getColor( );
g.setColor(SQUARE_COLOR_LEFT);
g.drawLine(x,y, x,y+1);
g.setColor(SQUARE_COLOR_TOP_RIGHT);
g.drawLine(x+1,y, x+1,y);
g.setColor(SQUARE_COLOR_BOTTOM_RIGHT);
g.drawLine(x+1,y+1, x+1,y+1);
g.setColor(oldColor);
}
Figure 5-4. The Windows Explorer icon zoomed in
184
|
Chapter 5, Windows, Dialogs, and Frames
#35 Add Windows Resize Icons
HACK
The code looks strange because of all of the drawLine( ) calls
that really should be
drawPoint( ) calls. Because there is no
drawPoint() method, you can accomplish the same thing by
drawing a line to and from the same point.
You could paint each square and then add the 3D effect with more lines, but
this is a good time to take advantage of the non-transparent nature of com-
puters. It’s easier just to paint the white 3D effect as white squares and paint
over the corners when you draw the gray squares.
Continuing as before, create a constant for the 3D effect color (white):
private static final Color THREE_D_EFFECT_COLOR = new Color(255, 255, 255);
The constant is used by a helper method to paint the white squares. It
caches the old paint color, sets the graphics to paint white, draws the
square, and resets the graphics color:
private void draw3dSquare(Graphics g, int x, int y){
Color oldColor = g.getColor( );
g.setColor(THREE_D_EFFECT_COLOR);
g.fillRect(x,y,2,2);
g.setColor(oldColor);
}
Now, think of the icon as a grid with rows and columns representing the
locations for the squares. It’s really a 3 × 3 grid that’s partially filled in like
this:
| | | X |
| | X | X |
| X | X | X |
Keep track of the rows and columns with variables to make painting easier.
Also, keep track of the space between the rows and columns in pixels:
int firstRow = 0;
int firstColumn = 0;
int rowDiff = 4;
int columnDiff = 4;
The row difference works out to be four because the square is two pixels
wide, plus one pixel for the white effect, plus one pixel for spacing. The
same deal applies for the column difference. From there, it’s easy to calcu-
late the other rows and columns based on the starting row and column and
their distances from each other:
int secondRow = firstRow + rowDiff;
int secondColumn = firstColumn + columnDiff;
int thirdRow = secondRow + rowDiff;
int thirdColumn = secondColumn + columnDiff;

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required