1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import com.sun.java.swing.Painter; 10 import sun.swing.plaf.synth.SynthIcon; 11 12 import javax.swing.plaf.synth.SynthContext ; 13 import javax.swing.*; 14 import java.awt.*; 15 import java.awt.geom.AffineTransform ; 16 import java.awt.image.BufferedImage ; 17 18 22 class NimbusIcon extends SynthIcon { 23 private int width; 24 private int height; 25 private String prefix; 26 private String key; 27 28 NimbusIcon(String prefix, String key, int w, int h) { 29 this.width = w; 30 this.height = h; 31 this.prefix = prefix; 32 this.key = key; 33 } 34 35 @Override 36 public void paintIcon(SynthContext context, Graphics g, int x, int y, 37 int w, int h) { 38 Painter painter = null; 39 if (context != null) { 40 painter = (Painter)context.getStyle().get(context, key); 41 } 42 if (painter == null){ 43 painter = (Painter) UIManager.get(prefix + "[Enabled]." + key); 44 } 45 46 if (painter != null && context != null) { 47 JComponent c = context.getComponent(); 48 boolean rotate = false; 49 boolean flip = false; 50 if (c instanceof JToolBar) { 51 rotate = ((JToolBar)c).getOrientation() == JToolBar.VERTICAL; 52 flip = !((JToolBar)c).getComponentOrientation().isLeftToRight(); 53 } 54 if (g instanceof Graphics2D){ 55 Graphics2D gfx = (Graphics2D)g; 56 gfx.translate(x, y); 57 if (rotate) { 58 gfx.rotate(Math.toRadians(90)); 59 gfx.translate(0, -w); 60 painter.paint(gfx, context.getComponent(), h, w); 61 gfx.translate(0, w); 62 gfx.rotate(Math.toRadians(-90)); 63 } else if (flip){ 64 gfx.scale(-1, 1); 65 gfx.translate(-w,0); 66 painter.paint(gfx, context.getComponent(), w, h); 67 gfx.translate(w,0); 68 gfx.scale(-1, 1); 69 } else { 70 painter.paint(gfx, context.getComponent(), w, h); 71 } 72 gfx.translate(-x, -y); 73 } else { 74 BufferedImage img = new BufferedImage (w,h, 77 BufferedImage.TYPE_INT_ARGB); 78 Graphics2D gfx = img.createGraphics(); 79 if (rotate) { 80 gfx.rotate(Math.toRadians(90)); 81 gfx.translate(0, -w); 82 painter.paint(gfx, context.getComponent(), h, w); 83 } else if (flip){ 84 gfx.scale(-1, 1); 85 gfx.translate(-w,0); 86 painter.paint(gfx, context.getComponent(), w, h); 87 } else { 88 painter.paint(gfx, context.getComponent(), w, h); 89 } 90 gfx.dispose(); 91 g.drawImage(img,x,y,null); 92 img = null; 93 } 94 } 95 } 96 97 103 @Override 104 public void paintIcon(Component c, Graphics g, int x, int y) { 105 Painter painter = (Painter)UIManager.get(prefix + "[Enabled]." + key); 106 if (painter != null){ 107 JComponent jc = (c instanceof JComponent) ? (JComponent)c : null; 108 Graphics2D gfx = (Graphics2D)g; 109 gfx.translate(x, y); 110 painter.paint(gfx, jc , width, height); 111 gfx.translate(-x, -y); 112 } 113 } 114 115 @Override 116 public int getIconWidth(SynthContext context) { 117 if (context == null) { 118 return width; 119 } 120 JComponent c = context.getComponent(); 121 if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) { 122 Insets i = c.getInsets(); 123 return c.getWidth() - i.left - i.right; 124 } else { 125 return scale(context, width); 126 } 127 } 128 129 @Override 130 public int getIconHeight(SynthContext context) { 131 if (context == null) { 132 return height; 133 } 134 JComponent c = context.getComponent(); 135 if (c instanceof JToolBar){ 136 if (((JToolBar)c).getOrientation() == JToolBar.HORIZONTAL) { 137 Insets i = c.getInsets(); 138 return c.getHeight() - i.top - i.bottom; 139 } else { 140 return scale(context, width); 141 } 142 } else { 143 return scale(context, height); 144 } 145 } 146 147 156 private int scale(SynthContext context, int size) { 157 if (context == null || context.getComponent() == null){ 158 return size; 159 } 160 String scaleKey = (String ) context.getComponent().getClientProperty( 162 "JComponent.sizeVariant"); 163 if (scaleKey != null) { 164 if (NimbusStyle.LARGE_KEY.equals(scaleKey)) { 165 size *= NimbusStyle.LARGE_SCALE; 166 } else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) { 167 size *= NimbusStyle.SMALL_SCALE; 168 } else if (NimbusStyle.MINI_KEY.equals(scaleKey)) { 169 size *= NimbusStyle.MINI_SCALE + 0.07; 172 } 173 } 174 return size; 175 } 176 } 177 | Popular Tags |