28.12.2012 Views

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A Graphics Application 375<br />

Listing 4-18 An icon that shows a color-filled square (continued)<br />

public void paintlcon(Component comp, Graphics g,<br />

int x, int y) {<br />

Color c = g.getColor();<br />

}<br />

// Draw the border<br />

if (borderSize > 0) {<br />

g.setColor(shadow);<br />

for (int i = 0; i < borderSize; i++) {<br />

g.drawRect (x + i, y + i, width - 2 * i - 1,<br />

height - 2 * i - 1);<br />

}<br />

}<br />

// Fill the remainder of the icon<br />

g.setColor(fillColor);<br />

g.fillRect(x + borderSize, y + borderSize,<br />

fillWidth, fillHeight);<br />

g . setColor (c) ;<br />

// Icon state<br />

protected int width; // Color fill width<br />

protected int height; // Color fill height<br />

protected Color fillColor;// Color to fill with<br />

protected Color shadow; // Shadow color<br />

protected int borderSize;// Border size in pixels<br />

protected int fillHeight;// Height of area to fill<br />

protected int fillWidth; // Width of area to fill<br />

// Constants<br />

public static final int BORDER_SIZE = 2;<br />

public static final int DEFAULT_SIZE = 32;<br />

J The most general constructor allows you to tailor the width and height of<br />

the colored area, the width of the border area, which can be removed<br />

entirely by making it zero pixels wide, and the color to be used to fill the center<br />

of the icon. Two other constructors allow you to construct a square icon of<br />

specified size or a 32-pixel square icon, with a fill color of your choice. The<br />

border is colored black, unless the setShadow method is used to change the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!