void (Graphics g, JComponent c) { if (!(c instanceof OnOffButton button)) return; int toggleArc = JBUIScale.scale(3); int buttonArc = JBUIScale.scale(5); int vGap = JBUIScale.scale(3); int hGap = JBUIScale.scale(3); Dimension size = button.getSize(); int w = size.width - 2 * vGap; int h = size.height - 2 * hGap; if (h % 2 == 1) { h--; } Graphics2D g2 = (Graphics2D)g.create(); try { g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int xOff = (button.getWidth() - w) / 2; int yOff = (button.getHeight() - h) / 2; g2.translate(xOff, yOff); boolean selected = button.isSelected(); g2.setColor(selected ? ON_BACKGROUND : OFF_BACKGROUND); g2.fillRoundRect(0, 0, w, h, buttonArc, buttonArc); int knobWidth = w - SwingUtilities.computeStringWidth(g2.getFontMetrics(), button.getOffText()) - JBUIScale.scale(2); knobWidth = Math.min(knobWidth, h); int textAscent = g2.getFontMetrics().getAscent(); Rectangle viewRect = new Rectangle(); Rectangle textRect = new Rectangle(); Rectangle iconRect = new Rectangle(); g2.setColor(BUTTON_COLOR); if (selected) { g2.fillRoundRect(w - knobWidth, 0, knobWidth, h, toggleArc, toggleArc); viewRect.setBounds(0, 0, w - knobWidth, h); SwingUtilities.layoutCompoundLabel(g2.getFontMetrics(), button.getOnText(), null, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, viewRect, iconRect, textRect, 0); g2.setColor(ON_FOREGROUND); g2.drawString(button.getOnText(), textRect.x, textRect.y + textAscent); } else { g2.fillRoundRect(0, 0, knobWidth, h, toggleArc, toggleArc); viewRect.setBounds(knobWidth, 0, w - knobWidth, h); SwingUtilities.layoutCompoundLabel(g2.getFontMetrics(), button.getOffText(), null, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, viewRect, iconRect, textRect, 0); g2.setColor(OFF_FOREGROUND); g2.drawString(button.getOffText(), textRect.x, textRect.y + textAscent); } g2.setColor(BORDER_COLOR); g2.drawRoundRect(0, 0, w, h, buttonArc, buttonArc); } finally { g2.dispose(); } }