import java.awt.*; import java.io.*; import java.util.Properties; import javax.swing.*; import javax.swing.plaf.metal.*; import javax.swing.plaf.*; public class HighContrastLAF extends MetalLookAndFeel { static Properties properties = new Properties() ; // load the properties from the resource file static { try { properties.load(HighContrastLAF.class.getResourceAsStream("HighContrastLAF.properties")) ; } catch (IOException ioe) {System.err.println("Could not load properties.") ;} MetalLookAndFeel.setCurrentTheme(new HighContrastTheme()) ; } public String getName() { return properties.getProperty("name");} public String getDescription() { return properties.getProperty("description");} public String getID() {return getClass().getName() ;} protected void initComponentDefaults(UIDefaults table) { super.initComponentDefaults( table ); double mag = 1.0 ; try { mag = Double.parseDouble(properties.getProperty("magFactor")) ; } catch (Exception exc) {} Object[] defaults = new Object[] { "ComboBox.selectionForeground", getHighlightedTextColor(), "Panel.font", getControlTextFont(), "CheckBox.icon", new MagnifiedIcon(MetalIconFactory.getCheckBoxIcon(), mag), "RadioButton.icon", new MagnifiedIcon(MetalIconFactory.getRadioButtonIcon(), mag), "Menu.checkIcon", new MagnifiedIcon(MetalIconFactory.getMenuItemCheckIcon(), mag), "Menu.arrowIcon", new MagnifiedIcon(MetalIconFactory.getMenuArrowIcon(), mag), "CheckBoxMenuItem.checkIcon", new MagnifiedIcon(MetalIconFactory.getCheckBoxMenuItemIcon(), mag), "CheckBoxMenuItem.arrowIcon", new MagnifiedIcon(MetalIconFactory.getMenuItemArrowIcon(), mag), "RadioButtonMenuItem.checkIcon", new MagnifiedIcon(MetalIconFactory.getRadioButtonMenuItemIcon(), mag), "RadioButtonMenuItem.arrowIcon", new MagnifiedIcon(MetalIconFactory.getMenuItemArrowIcon(), mag), "Tree.openIcon", new MagnifiedIcon(MetalIconFactory.getTreeFolderIcon(), mag), "Tree.closedIcon", new MagnifiedIcon(MetalIconFactory.getTreeFolderIcon(), mag), "Tree.leafIcon", new MagnifiedIcon(MetalIconFactory.getTreeLeafIcon(), mag), "Tree.expandedIcon", new MagnifiedIcon(MetalIconFactory.getTreeControlIcon(MetalIconFactory.DARK), mag), "Tree.collapsedIcon", new MagnifiedIcon(MetalIconFactory.getTreeControlIcon(MetalIconFactory.LIGHT), mag), "FileChooser.detailsViewIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserDetailViewIcon(), mag), "FileChooser.homeFolderIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserHomeFolderIcon(), mag), "FileChooser.listViewIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserListViewIcon(), mag), "FileChooser.newFolderIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserNewFolderIcon(), mag), "FileChooser.upFolderIcon", new MagnifiedIcon(MetalIconFactory.getFileChooserUpFolderIcon(), mag), } ; table.putDefaults(defaults); } /** A color theme that loads the main colors and fonts from an external properties file */ protected static class HighContrastTheme extends DefaultMetalTheme { private FontUIResource font ; private static ColorUIResource getColor(String key) { return new ColorUIResource(Integer.parseInt(properties.getProperty(key), 16)); } public HighContrastTheme() { String fontName = properties.getProperty("fontName", "Dialog"); int fontSize = 12; try { fontSize = Integer.parseInt(properties.getProperty("fontSize")); } catch (Exception exc) {} font = new FontUIResource(fontName, Font.PLAIN, fontSize); } protected ColorUIResource getWhite() { return getColor("backgroundColor"); } protected ColorUIResource getBlack() { return getColor("foregroundColor"); } protected ColorUIResource getPrimary1() { return getColor("primaryColor1"); } protected ColorUIResource getPrimary2() { return getColor("primaryColor2"); } protected ColorUIResource getPrimary3() { return getColor("primaryColor3"); } protected ColorUIResource getSecondary1() { return getColor("secondaryColor1"); } protected ColorUIResource getSecondary2() { return getColor("secondaryColor2"); } protected ColorUIResource getSecondary3() { return getColor("secondaryColor3"); } protected ColorUIResource getSelectionForeground() { return getColor("selectionForeground"); } protected ColorUIResource getSelectionBackground() { return getColor("selectionBackground"); } public ColorUIResource getMenuSelectedBackground() { return getSelectionBackground() ;} public ColorUIResource getMenuSelectedForeground() { return getSelectionForeground() ;} public ColorUIResource getTextHighlightColor() { return getSelectionBackground() ;} public ColorUIResource getHighlightedTextColor() { return getSelectionForeground() ;} public FontUIResource getControlTextFont(){ return font; } public FontUIResource getMenuTextFont() { return font; } public FontUIResource getSubTextFont() { return font; } public FontUIResource getSystemTextFont() { return font; } public FontUIResource getUserTextFont() { return font; } public FontUIResource getWindowTitleFont(){ return font; } } /** A class to create a magnified version of an existing icon */ protected class MagnifiedIcon implements Icon { private Icon icon ; private double factor ; public MagnifiedIcon(Icon icon, double factor) { this.icon = icon ; this.factor = factor ; } public int getIconWidth() { return (int)(icon.getIconWidth()*factor) ;} public int getIconHeight() { return (int)(icon.getIconHeight()*factor) ;} public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2d = (Graphics2D)g.create() ; g2d.translate(x,y); g2d.scale(factor, factor); icon.paintIcon(c,g2d,0,0); g2d.dispose(); } } }