Java Reference
In-Depth Information
The icons for the customAction object need to be created in the SketcherFrame constructor, ready for
use in the Action object:
public SketcherFrame(String title, Sketcher theApp) {
setTitle(title); // Set the window title
this.theApp = theApp; // Save app. object reference
setJMenuBar(menuBar); // Add the menu bar to the
window
setDefaultCloseOperation(EXIT_ON_CLOSE); // Default is exit the
application
setCustomIconColor(CUSTOM16,customColor); // Setup small custom color icon
setCustomIconColor(CUSTOM24,customColor); // Setup large custom color icon
// Rest of the constructor code as before...
}
Directory "Sketcher 11 with custom colors"
A new method in the SketcherFrame class sets up the icons, so add the implementation for that next:
private void setCustomIconColor(ImageIcon icon, Color color) {
BufferedImage image = (BufferedImage)(icon.getImage());
int width = image.getWidth();
// Image width
int indent = width == 16 ? 3 : 2;
// Indent for filled rectangle
int rectSize = width - 2*indent;
// Filled rectangle size
Graphics2D g2D = image.createGraphics();
g2D.setPaint(color);
g2D.fillRect(indent,indent,rectSize,rectSize);
// Fill centered rectangle
if(width == 24) {
TextLayout textLayout = new TextLayout("C", g2D.getFont(),
g2D.getFontRenderContext());
Rectangle2D.Float rect = (Rectangle2D.Float)textLayout.getBounds();
g2D.setPaint(
new Color(255-color.getRed(),255-color.getGreen(), 255-color.getBlue()));
g2D.drawString("C", (width-rect.width)/2, (width+rect.height)/2);
}
g2D.dispose();
}
Directory "Sketcher 11 with custom colors"
Add the following import statements to SketcherFrame.java for the new types introduced in this meth-
od:
Search WWH ::




Custom Search