1 package snow.utils.gui; 2 3 13 14 import javax.swing.*; 15 import javax.swing.border.*; 16 import java.awt.*; 17 import java.io.*; 18 import java.util.*; 19 import java.awt.event.*; 20 import java.beans.*; 21 22 23 public final class EFCNBackgroundPanel extends JPanel 24 { 25 26 public static final int ApplyUpperLeftCornerHighLight = 0; 28 public static final int ApplyVerticalHighLight = 1; 29 30 public static final String ActiveTitleBackground = "InternalFrame.activeTitleBackground"; 32 public static final String PanelBackground = "Panel.background"; 33 34 35 public static final int LightGradientStrength = 0; 37 public static final int MediumGradientStrength = 1; 38 public static final int StrongGradientStrength = 2; 39 40 41 private Color lightColor = new Color(190,190,250); 42 private Color mediumColor = new Color(120,120,180); 43 private Color darkColor = new Color(80,80,120); 44 float xGradient; 45 46 private Color basisColor; 48 private int gradientType = ApplyUpperLeftCornerHighLight; 50 private int gradientStrength = MediumGradientStrength; 51 private String colorKey = ActiveTitleBackground; 52 53 54 private boolean useParentBackGround = false; 56 57 58 59 60 77 public EFCNBackgroundPanel( int theGradientType, 78 int theGradientStrength, 79 String theColorKey ) 80 { 81 this( new BorderLayout(),true,theGradientType,theGradientStrength,theColorKey ); 82 } 83 84 85 86 87 88 105 public EFCNBackgroundPanel( LayoutManager layout, 106 int theGradientType, 107 int theGradientStrength, 108 String theColorKey ) 109 { 110 this(layout,true,theGradientType,theGradientStrength,theColorKey); 111 } 112 113 114 115 116 133 public EFCNBackgroundPanel( LayoutManager layout, 134 boolean isDoubleBuffered, 135 int theGradientType, 136 int theGradientStrength, 137 String theColorKey ) 138 { 139 super(layout,isDoubleBuffered); 140 this.gradientType = theGradientType; 141 this.gradientStrength = theGradientStrength; 142 this.updateSpecialUI(); if( colorKey != null ) 144 { 145 this.colorKey = theColorKey; 146 } float unitSize = UIManager.getFont("TextField.font").getSize2D(); 149 this.xGradient = unitSize; 150 151 this.addPropertyChangeListener( 153 new PropertyChangeListener() 154 { 155 public void propertyChange(PropertyChangeEvent e) 156 { 157 if( !useParentBackGround ) { 159 String name = e.getPropertyName(); 160 if (name.equals("ancestor")) 161 { 162 SwingUtilities.invokeLater( new Runnable () 163 { 164 public void run() 165 { 166 updateSpecialUI(); 167 float unitSize = UIManager.getFont("TextField.font").getSize2D(); 169 xGradient = unitSize; 170 } 171 }); 172 } 173 } 174 } 175 }); 176 177 } 179 180 181 185 public void setBackground( Color bgColor ) 186 { 187 super.setBackground(bgColor); 188 this.useParentBackGround = true; this.basisColor = super.getBackground(); 190 this.calculateColors(); 191 } 192 193 194 195 199 private void calculateColors() 200 { 201 int colorOffset = 50; if( this.gradientStrength == LightGradientStrength ) 203 { 204 colorOffset = 30; 205 } 206 if( this.gradientStrength == StrongGradientStrength ) 207 { 208 colorOffset = 70; 209 } 210 int rBase = this.basisColor.getRed(); 211 int gBase = this.basisColor.getGreen(); 212 int bBase = this.basisColor.getBlue(); 213 int rStart = rBase + colorOffset; 215 int gStart = gBase + colorOffset; 216 int bStart = bBase + colorOffset; 217 if( (rStart <= 255) && (gStart <= 255) && (bStart <= 255) ) 218 { 219 this.lightColor = new Color( rStart,gStart,bStart ); 220 } else 221 { 222 if( rStart > 255 ) rStart = 255; 223 if( gStart > 255 ) gStart = 255; 224 if( bStart > 255 ) bStart = 255; 225 this.lightColor = new Color( rStart,gStart,bStart ); 226 } 227 228 this.mediumColor = this.basisColor; 229 230 rStart = rBase - colorOffset; 231 gStart = gBase - colorOffset; 232 bStart = bBase - colorOffset; 233 if( (rStart >= 0) && (gStart >= 0) && (bStart >= 0) ) 234 { 235 this.darkColor = new Color( rStart,gStart,bStart ); 236 } else 237 { 238 if( rStart < 0 ) rStart = 0; 239 if( gStart < 0 ) gStart = 0; 240 if( bStart < 0 ) bStart = 0; 241 this.darkColor = new Color( rStart,gStart,bStart ); 242 } 243 244 } 245 246 247 251 public void updateSpecialUI() 252 { 253 Color color = UIManager.getColor(this.colorKey); 255 if( color == null ) 256 { 257 color = new Color(250,170,150); } int r = color.getRed() - 3; 261 int g = color.getGreen() - 3; 262 int b = color.getBlue() + 6; 263 if( r+g+b > 384 ) 265 { 266 r -= 10; 267 g -= 10; 268 b -= 10; 269 } else 270 { 271 r += 10; 272 g += 10; 273 b += 10; 274 } 275 if( r < 0 ) r = 0; if( r > 255 ) r = 255; 277 if( g < 0 ) g = 0; if( g > 255 ) g = 255; 278 if( b < 0 ) b = 0; if( b > 255 ) b = 255; 279 this.basisColor = new Color(r,g,b); 281 this.calculateColors(); 283 } 284 285 286 289 public void paint( Graphics g ) 290 { 291 Graphics2D graphics2D = (Graphics2D)g; 292 final Paint savePaint = graphics2D.getPaint(); 293 if( this.gradientType == ApplyUpperLeftCornerHighLight ) 294 { 295 GradientPaint upperLeftGradientPaint = 296 new GradientPaint( 0f,0f, 297 lightColor, 298 xGradient,xGradient*5.0f, 299 mediumColor ); 300 301 graphics2D.setPaint( upperLeftGradientPaint ); 302 graphics2D.fill( graphics2D.getClip() ); 303 } 304 else if( this.gradientType == ApplyVerticalHighLight ) 305 { 306 307 float gradientLength = xGradient; 308 if( gradientLength > this.getHeight()/2.5f ) 309 { 310 gradientLength = this.getHeight()/2.5f; 311 } 312 GradientPaint upperVerticalGradientPaint = 313 new GradientPaint( 0f,0f, 314 this.lightColor, 315 0f, gradientLength, 316 this.mediumColor ); 317 318 GradientPaint lowerVerticalGradientPaint = 319 new GradientPaint( 0f,getHeight(), 320 this.darkColor, 321 0f,getHeight() - gradientLength, 322 this.mediumColor ); 323 324 Shape saveClip = graphics2D.getClip(); 325 326 graphics2D.setPaint( lowerVerticalGradientPaint ); 327 graphics2D.fill( graphics2D.getClip() ); 328 329 Rectangle r = new Rectangle( 0,0,getWidth(),1+getHeight()/2 ); 330 graphics2D.setPaint( upperVerticalGradientPaint ); 331 graphics2D.fill( r ); 332 333 graphics2D.setClip(saveClip); 334 } 335 graphics2D.setPaint( savePaint ); 336 super.paintChildren(graphics2D); 337 } 339 340 341 342 346 public void update( Graphics g ) 347 { 348 paint(g); 350 } 351 352 353 354 355 } | Popular Tags |