1 package com.jsql.view.swing.tree.custom; 2 3 import javax.swing.*; 4 import javax.swing.plaf.UIResource; 5 import javax.swing.plaf.metal.MetalLookAndFeel; 6 import java.awt.*; 7 import java.io.Serializable; 8 9 public class CheckBoxMenuItemIconCustom implements Icon, UIResource, Serializable { 10 11 @Override 12 public void paintIcon(Component component, Graphics graphics, int x, int y) { 13 14 AbstractButton abstractButton = (AbstractButton) component; 15 ButtonModel model = abstractButton.getModel(); 16 17 boolean isSelected = model.isSelected(); 18 boolean isEnabled = model.isEnabled(); 19 boolean isPressed = model.isPressed(); 20 boolean isArmed = model.isArmed(); 21 22 graphics.translate(x, y); 23 24 if (isEnabled) { 25 26 if (isPressed || isArmed) { 27 28 graphics.setColor(MetalLookAndFeel.getControlInfo()); 29 graphics.drawLine(0, 0, 8, 0); 30 graphics.drawLine(0, 0, 0, 8); 31 graphics.drawLine(8, 2, 8, 8); 32 graphics.drawLine(2, 8, 8, 8); 33 34 graphics.setColor(MetalLookAndFeel.getPrimaryControl()); 35 36 } else { 37 38 graphics.setColor(MetalLookAndFeel.getControlDarkShadow()); 39 graphics.drawLine(0, 0, 8, 0); 40 graphics.drawLine(0, 0, 0, 8); 41 graphics.drawLine(8, 2, 8, 8); 42 graphics.drawLine(2, 8, 8, 8); 43 44 graphics.setColor(MetalLookAndFeel.getControlHighlight()); 45 } 46 47 graphics.drawLine(1, 1, 7, 1); 48 graphics.drawLine(1, 1, 1, 7); 49 graphics.drawLine(9, 1, 9, 9); 50 graphics.drawLine(1, 9, 9, 9); 51 52 } else { 53 54 graphics.setColor(MetalLookAndFeel.getMenuDisabledForeground()); 55 graphics.drawRect(0, 0, 8, 8); 56 } 57 58 if (isSelected) { 59 this.drawSelected(component, graphics, abstractButton, model, isEnabled); 60 } 61 62 graphics.translate(-x, -y); 63 } 64 65 private void drawSelected(Component component, Graphics graphics, AbstractButton abstractButton, ButtonModel model, boolean isEnabled) { 66 67 if (isEnabled) { 68 if (model.isArmed() || (component instanceof JMenu && model.isSelected())) { 69 graphics.setColor(MetalLookAndFeel.getMenuSelectedForeground()); 70 } else { 71 graphics.setColor(abstractButton.getForeground()); 72 } 73 } else { 74 graphics.setColor(MetalLookAndFeel.getMenuDisabledForeground()); 75 } 76 77 graphics.drawLine(2, 2, 2, 6); 78 graphics.drawLine(3, 2, 3, 6); 79 graphics.drawLine(4, 4, 8, 0); 80 graphics.drawLine(4, 5, 9, 0); 81 } 82 83 @Override 84 public int getIconWidth() { 85 return 10; 86 } 87 88 @Override 89 public int getIconHeight() { 90 return 10; 91 } 92 }