private static void paintAndCheckIcon(Icon icon, SynthContext synthContext, int width, int height) { BufferedImage buffImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = buffImage.createGraphics(); g.setColor(Color.RED); g.fillRect(0, 0, width, height); SynthGraphicsUtils.paintIcon(icon, synthContext, g, 0, 0, width, height); g.dispose(); Color iconCenterColor = new Color(buffImage.getRGB(width / 2, height / 2)); if (!TEST_COLOR.equals(iconCenterColor)) { throw new RuntimeException("Icon is painted incorrectly!"); } }
/** * Paints the background of the thumb of a slider. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to * @param orientation One of <code>JSlider.HORIZONTAL</code> or * <code>JSlider.VERTICAL</code> */ public void paintSliderThumbBackground(SynthContext context, Graphics g, int x, int y, int w, int h, int orientation) { if (context.getComponent().getClientProperty( "Slider.paintThumbArrowShape") == Boolean.TRUE){ if (orientation == JSlider.HORIZONTAL){ orientation = JSlider.VERTICAL; } else { orientation = JSlider.HORIZONTAL; } paintBackground(context, g, x, y, w, h, orientation); } else { paintBackground(context, g, x, y, w, h, orientation); } }
@Override public int getIconWidth(SynthContext context) { if (context == null) { return width; } JComponent c = context.getComponent(); if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) { //we only do the -1 hack for UIResource borders, assuming //that the border is probably going to be our border if (c.getBorder() instanceof UIResource) { return c.getWidth() - 1; } else { return c.getWidth(); } } else { return scale(context, width); } }
/** * {@inheritDoc} * * Overridden to cause this style to populate itself with data from * UIDefaults, if necessary. If a value named "font" is not found in * UIDefaults, then the "defaultFont" font in UIDefaults will be returned * instead. */ @Override protected Font getFontForState(SynthContext ctx) { Font f = (Font)get(ctx, "font"); if (f == null) f = UIManager.getFont("defaultFont"); // Account for scale // The key "JComponent.sizeVariant" is used to match Apple's LAF String scaleKey = (String)ctx.getComponent().getClientProperty( "JComponent.sizeVariant"); if (scaleKey != null){ if (LARGE_KEY.equals(scaleKey)){ f = f.deriveFont(Math.round(f.getSize2D()*LARGE_SCALE)); } else if (SMALL_KEY.equals(scaleKey)){ f = f.deriveFont(Math.round(f.getSize2D()*SMALL_SCALE)); } else if (MINI_KEY.equals(scaleKey)){ f = f.deriveFont(Math.round(f.getSize2D()*MINI_SCALE)); } } return f; }
/** * Scale a size based on the "JComponent.sizeVariant" client property of the * component that is using this icon * * @param context The synthContext to get the component from * @param size The size to scale * @return The scaled size or original if "JComponent.sizeVariant" is not * set */ private int scale(SynthContext context, int size) { if (context == null || context.getComponent() == null){ return size; } // The key "JComponent.sizeVariant" is used to match Apple's LAF String scaleKey = (String) context.getComponent().getClientProperty( "JComponent.sizeVariant"); if (scaleKey != null) { if (NimbusStyle.LARGE_KEY.equals(scaleKey)) { size *= NimbusStyle.LARGE_SCALE; } else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) { size *= NimbusStyle.SMALL_SCALE; } else if (NimbusStyle.MINI_KEY.equals(scaleKey)) { // mini is not quite as small for icons as full mini is // just too tiny size *= NimbusStyle.MINI_SCALE + 0.07; } } return size; }
private void paintBackground(SynthContext ctx, Graphics g, int x, int y, int w, int h, AffineTransform transform) { // if the background color of the component is 100% transparent // then we should not paint any background graphics. This is a solution // for there being no way of turning off Nimbus background painting as // basic components are all non-opaque by default. Component c = ctx.getComponent(); Color bg = (c != null) ? c.getBackground() : null; if (bg == null || bg.getAlpha() > 0){ Painter<Object> backgroundPainter = style.getBackgroundPainter(ctx); if (backgroundPainter != null) { paint(backgroundPainter, ctx, g, x, y, w, h,transform); } } }
/** * {@inheritDoc} * * <p>Overridden to cause this style to populate itself with data from * UIDefaults, if necessary.</p> * * <p>In addition, NimbusStyle handles ColorTypes slightly differently from * Synth.</p> * <ul> * <li>ColorType.BACKGROUND will equate to the color stored in UIDefaults * named "background".</li> * <li>ColorType.TEXT_BACKGROUND will equate to the color stored in * UIDefaults named "textBackground".</li> * <li>ColorType.FOREGROUND will equate to the color stored in UIDefaults * named "textForeground".</li> * <li>ColorType.TEXT_FOREGROUND will equate to the color stored in * UIDefaults named "textForeground".</li> * </ul> */ @Override protected Color getColorForState(SynthContext ctx, ColorType type) { String key = null; if (type == ColorType.BACKGROUND) { key = "background"; } else if (type == ColorType.FOREGROUND) { //map FOREGROUND as TEXT_FOREGROUND key = "textForeground"; } else if (type == ColorType.TEXT_BACKGROUND) { key = "textBackground"; } else if (type == ColorType.TEXT_FOREGROUND) { key = "textForeground"; } else if (type == ColorType.FOCUS) { key = "focus"; } else if (type != null) { key = type.toString(); } else { return DEFAULT_COLOR; } Color c = (Color) get(ctx, key); //if all else fails, return a default color (which is a ColorUIResource) if (c == null) c = DEFAULT_COLOR; return c; }
/** * Paints the background of an arrow button. Arrow buttons are created by * some components, such as <code>JScrollBar</code>. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintArrowButtonBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { if (context.getComponent().getComponentOrientation().isLeftToRight()){ paintBackground(context, g, x, y, w, h, null); } else { AffineTransform transform = new AffineTransform(); transform.translate(x,y); transform.scale(-1, 1); transform.translate(-w,0); paintBackground(context, g, 0, 0, w, h, transform); } }
/** * Paints the border of a formatted text field. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintFormattedTextFieldBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { if (context.getComponent().getComponentOrientation().isLeftToRight()){ paintBorder(context, g, x, y, w, h, null); } else { AffineTransform transform = new AffineTransform(); transform.translate(x,y); transform.scale(-1, 1); transform.translate(-w,0); paintBorder(context, g, 0, 0, w, h, transform); } }
/** * Paints the background of a formatted text field. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintFormattedTextFieldBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { if (context.getComponent().getComponentOrientation().isLeftToRight()){ paintBackground(context, g, x, y, w, h, null); } else { AffineTransform transform = new AffineTransform(); transform.translate(x,y); transform.scale(-1, 1); transform.translate(-w,0); paintBackground(context, g, 0, 0, w, h, transform); } }
/** * Paints the border of a text field. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintTextFieldBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { if (context.getComponent().getComponentOrientation().isLeftToRight()){ paintBorder(context, g, x, y, w, h, null); } else { AffineTransform transform = new AffineTransform(); transform.translate(x,y); transform.scale(-1, 1); transform.translate(-w,0); paintBorder(context, g, 0, 0, w, h, transform); } }
private void paintBackground(SynthContext ctx, Graphics g, int x, int y, int w, int h, AffineTransform transform) { // if the background color of the component is 100% transparent // then we should not paint any background graphics. This is a solution // for there being no way of turning off Nimbus background painting as // basic components are all non-opaque by default. Component c = ctx.getComponent(); Color bg = (c != null) ? c.getBackground() : null; if (bg == null || bg.getAlpha() > 0){ Painter backgroundPainter = style.getBackgroundPainter(ctx); if (backgroundPainter != null) { paint(backgroundPainter, ctx, g, x, y, w, h,transform); } } }
/** * {@inheritDoc} * * <p>Overridden to cause this style to populate itself with data from * UIDefaults, if necessary.</p> * * <p>Properties in UIDefaults may be specified in a chained manner. For * example: * <pre> * background * Button.opacity * Button.Enabled.foreground * Button.Enabled+Selected.background * </pre> * * <p>In this example, suppose you were in the Enabled+Selected state and * searched for "foreground". In this case, we first check for * Button.Enabled+Selected.foreground, but no such color exists. We then * fall back to the next valid state, in this case, * Button.Enabled.foreground, and have a match. So we return it.</p> * * <p>Again, if we were in the state Enabled and looked for "background", we * wouldn't find it in Button.Enabled, or in Button, but would at the top * level in UIManager. So we return that value.</p> * * <p>One special note: the "key" passed to this method could be of the form * "background" or "Button.background" where "Button" equals the prefix * passed to the NimbusStyle constructor. In either case, it looks for * "background".</p> * * @param ctx * @param key must not be null */ @Override public Object get(SynthContext ctx, Object key) { Values v = getValues(ctx); // strip off the prefix, if there is one. String fullKey = key.toString(); String partialKey = fullKey.substring(fullKey.indexOf(".") + 1); Object obj = null; int xstate = getExtendedState(ctx, v); // check the cache tmpKey.init(partialKey, xstate); obj = v.cache.get(tmpKey); boolean wasInCache = obj != null; if (!wasInCache){ // Search exact matching states and then lesser matching states RuntimeState s = null; int[] lastIndex = new int[] {-1}; while (obj == null && (s = getNextState(v.states, lastIndex, xstate)) != null) { obj = s.defaults.get(partialKey); } // Search Region Defaults if (obj == null && v.defaults != null) { obj = v.defaults.get(partialKey); } // return found object // Search UIManager Defaults if (obj == null) obj = UIManager.get(fullKey); // Search Synth Defaults for InputMaps if (obj == null && partialKey.equals("focusInputMap")) { obj = super.get(ctx, fullKey); } // if all we got was a null, store this fact for later use v.cache.put(new CacheKey(partialKey, xstate), obj == null ? NULL : obj); } // return found object return obj == NULL ? null : obj; }
@Override public int getIconWidth(SynthContext sc) { return width; }
/** * Paints the border of the track of a slider. This implementation invokes the * method of the same name without the orientation. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to * @param orientation One of <code>JSlider.HORIZONTAL</code> or * <code>JSlider.VERTICAL</code> * @since 1.6 */ public void paintSliderTrackBorder(SynthContext context, Graphics g, int x, int y, int w, int h, int orientation) { paintBorder(context, g, x, y, w, h, orientation); }
/** * Paints the background of a list. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintListBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the background of the track of a scrollbar. The track contains * the thumb. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintScrollBarTrackBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the background of the viewport. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintViewportBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the divider, when the user is dragging the divider, of a * split pane. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to * @param orientation One of <code>JSplitPane.HORIZONTAL_SPLIT</code> or * <code>JSplitPane.VERTICAL_SPLIT</code> */ public void paintSplitPaneDragDivider(SynthContext context, Graphics g, int x, int y, int w, int h, int orientation) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the border of the thumb of a scrollbar. The thumb provides * a graphical indication as to how much of the Component is visible in a * <code>JScrollPane</code>. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to * @param orientation Orientation of the JScrollBar, one of * <code>JScrollBar.HORIZONTAL</code> or * <code>JScrollBar.VERTICAL</code> */ public void paintScrollBarThumbBorder(SynthContext context, Graphics g, int x, int y, int w, int h, int orientation) { paintBorder(context, g, x, y, w, h, orientation); }
/** * Paints the border of the header of a table. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintTableHeaderBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the border of an arrow button. Arrow buttons are created by * some components, such as <code>JScrollBar</code>. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintArrowButtonBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the background of a button. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintButtonBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the background of a password field. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintPasswordFieldBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the border of a tabbed pane. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintTabbedPaneBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the border of a check box menu item. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintCheckBoxMenuItemBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the background of a check box. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintCheckBoxBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the border of a list. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintListBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the border of a viewport. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintViewportBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the background of a desktop icon. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintDesktopIconBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the border of a desktop icon. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintDesktopIconBorder(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBorder(context, g, x, y, w, h, null); }
/** * Paints the background of a tool bar. This implementation invokes the * method of the same name without the orientation. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to * @param orientation One of <code>JToolBar.HORIZONTAL</code> or * <code>JToolBar.VERTICAL</code> * @since 1.6 */ public void paintToolBarBackground(SynthContext context, Graphics g, int x, int y, int w, int h, int orientation) { paintBackground(context, g, x, y, w, h, orientation); }
/** * Paints the background of the window containing the tool bar when it * has been detached from its primary frame. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintToolBarDragWindowBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the background of an editor pane. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintEditorPaneBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the background of an internal frame. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintInternalFrameBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }
/** * Paints the background of the tool bar's content area. * * @param context SynthContext identifying the <code>JComponent</code> and * <code>Region</code> to paint to * @param g <code>Graphics</code> to paint to * @param x X coordinate of the area to paint to * @param y Y coordinate of the area to paint to * @param w Width of the area to paint to * @param h Height of the area to paint to */ public void paintToolBarContentBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { paintBackground(context, g, x, y, w, h, null); }