1 package snow.lookandfeel; 2 3 11 12 13 import javax.swing.*; 14 import javax.swing.border.*; 15 import javax.swing.event.*; 16 import java.awt.*; 17 import java.io.*; 18 import java.util.*; 19 import java.awt.event.*; 20 import java.beans.*; 21 import javax.swing.plaf.metal.*; 22 23 24 25 public final class CustomOceanImagePanel extends JPanel 26 { 27 28 public static final int ApplyUpperLeftCornerHighLight = 0; 30 public static final int ApplyVerticalHighLight = 1; 31 32 public static final String ActiveTitleBackground = "InternalFrame.activeTitleBackground"; 34 public static final String PanelBackground = "Panel.background"; 35 36 37 public static final int LightGradientStrength = 0; 39 public static final int MediumGradientStrength = 1; 40 public static final int StrongGradientStrength = 2; 41 42 private static final int AlfaColorStrength = 224; 43 45 46 private Color lightColor = new Color(190,190,250); 47 private Color mediumColor = new Color(120,120,180); 48 private Color darkColor = new Color(80,80,120); 49 float xGradient; 50 51 private Color basisColor; 53 private int gradientType = ApplyUpperLeftCornerHighLight; 55 private int gradientStrength = MediumGradientStrength; 56 private String colorKey = ActiveTitleBackground; 57 58 float gradientLength; 59 60 61 private int finalColorOffset; 62 private int colorOffset; 63 64 65 private boolean useParentBackGround = false; 67 private ImageIcon backgroundImage = null; 68 71 private ImageIcon customBackgroundImage = null; 72 76 77 private boolean introThreadHasBeenStarted = false; 78 79 80 private boolean allIsReadyForSpecialUpdates = false; 81 85 86 103 public CustomOceanImagePanel( final int theGradientType, 104 final int theGradientStrength, 105 final String theColorKey ) 106 { 107 this( new BorderLayout(),true,theGradientType,theGradientStrength,theColorKey ); 108 } 109 110 111 public CustomOceanImagePanel( ) 112 { 113 this( new BorderLayout(), true, ApplyUpperLeftCornerHighLight, LightGradientStrength, PanelBackground ); 114 } 115 116 117 134 public CustomOceanImagePanel( LayoutManager layout, 135 int theGradientType, 136 int theGradientStrength, 137 String theColorKey ) 138 { 139 this(layout, true, theGradientType, theGradientStrength, theColorKey); 140 } 141 142 143 144 145 162 public CustomOceanImagePanel( LayoutManager layout, 163 boolean isDoubleBuffered, 164 int theGradientType, 165 int theGradientStrength, 166 String theColorKey ) 167 { 168 super(layout,isDoubleBuffered); 169 this.gradientType = theGradientType; 170 this.gradientStrength = theGradientStrength; 171 this.updateSpecialUI(); if( colorKey != null ) 173 { 174 this.colorKey = theColorKey; 175 } float unitSize1 = UIManager.getFont("TextField.font").getSize2D(); 178 this.xGradient = unitSize1; 179 180 this.colorOffset = this.getFinalColorOffset()/16; 181 185 this.finalColorOffset = 70; if( this.gradientStrength == LightGradientStrength ) 187 { 188 this.finalColorOffset = 40; 189 } 190 if( this.gradientStrength == StrongGradientStrength ) 191 { 192 this.finalColorOffset = 90; 193 } 194 195 196 197 EventQueue.invokeLater(new Runnable () 198 { 199 public void run() 200 { 201 allIsReadyForSpecialUpdates = true; 202 } 203 }); 204 205 } 207 208 209 210 211 217 public CustomOceanImagePanel( LayoutManager layout, 218 int theGradientType, 219 int theGradientStrength, 220 String theColorKey, 221 ImageIcon customBackgroundImage ) 222 { 223 this( layout,theGradientType,theGradientStrength,theColorKey); 224 this.customBackgroundImage = customBackgroundImage; 225 } 226 227 228 229 233 public void updateUI() 234 { 235 super.updateUI(); 236 if( this.allIsReadyForSpecialUpdates ) 237 { 238 updateSpecialUI(); 240 float unitSize2 = UIManager.getFont("TextField.font").getSize2D(); 242 xGradient = unitSize2; 243 } 244 } 245 246 247 248 249 253 public void setBackground( Color bgColor ) 254 { 255 super.setBackground(bgColor); 256 this.useParentBackGround = true; this.basisColor = super.getBackground(); 258 this.calculateColors(); 259 } 260 261 262 263 267 private void calculateColors() 268 { 269 int rBase = this.basisColor.getRed(); 270 int gBase = this.basisColor.getGreen(); 271 int bBase = this.basisColor.getBlue(); 272 int rStart = rBase + colorOffset; 274 int gStart = gBase + colorOffset; 275 int bStart = bBase + colorOffset; 276 if( (rStart <= 255) && (gStart <= 255) && (bStart <= 255) ) 277 { 278 this.lightColor = new Color( rStart,gStart,bStart ); 279 } else 280 { 281 if( rStart > 255 ) rStart = 255; 282 if( gStart > 255 ) gStart = 255; 283 if( bStart > 255 ) bStart = 255; 284 this.lightColor = new Color( rStart,gStart,bStart ); 285 } 286 287 this.mediumColor = this.basisColor; 288 289 rStart = rBase - colorOffset; 290 gStart = gBase - colorOffset; 291 bStart = bBase - colorOffset; 292 if( (rStart >= 0) && (gStart >= 0) && (bStart >= 0) ) 293 { 294 this.darkColor = new Color( rStart,gStart,bStart ); 295 } else 296 { 297 if( rStart < 0 ) rStart = 0; 298 if( gStart < 0 ) gStart = 0; 299 if( bStart < 0 ) bStart = 0; 300 this.darkColor = new Color( rStart,gStart,bStart ); 301 } 302 303 if( this.customBackgroundImage != null ) 308 { 309 this.mediumColor = new Color( mediumColor.getRed(), 311 mediumColor.getGreen(), 312 mediumColor.getBlue(), 313 AlfaColorStrength ); 314 this.backgroundImage = this.customBackgroundImage; 316 } 317 else 318 { 319 CustomOceanTheme cot = ThemesManager.getInstance().getSelectedTheme(); 320 if( cot != null ) 321 { 322 this.backgroundImage = cot.getBackgroundImage(); 323 if( this.backgroundImage != null ) 324 { 325 this.mediumColor = new Color( mediumColor.getRed(), 326 mediumColor.getGreen(), 327 mediumColor.getBlue(), 328 AlfaColorStrength ); 329 } 330 331 } 332 } 333 } 335 336 337 338 339 340 344 public void updateSpecialUI() 345 { 346 this.basisColor = UIManager.getColor(this.colorKey); 348 this.calculateColors(); 350 } 351 352 353 356 public void paint( Graphics g ) 357 { 358 Graphics2D graphics2D = (Graphics2D)g; 359 final Paint savePaint = graphics2D.getPaint(); 360 361 if( this.backgroundImage != null ) 363 { 364 int xMax = this.getWidth(); 365 int yMax = this.getHeight(); 366 int imageWidth = this.backgroundImage.getIconWidth(); 367 int imageHeight = this.backgroundImage.getIconHeight(); 368 369 if( (xMax > 0) && 371 (yMax > 0) && 372 (imageWidth > 0) && 373 (imageHeight > 0) ) 374 { 375 int x = 0; 376 int y = 0; 377 while( x < xMax ) 378 { 379 while( y < yMax ) 380 { 381 graphics2D.drawImage( backgroundImage.getImage(),x,y,imageWidth,imageHeight, 384 backgroundImage.getImageObserver() ); 385 y += imageHeight; 387 } y = 0; 389 x += imageWidth; 390 } } } 394 if( this.gradientType == ApplyUpperLeftCornerHighLight ) 395 { 396 GradientPaint upperLeftGradientPaint = 397 new GradientPaint( 0f,0f, 398 lightColor, 399 xGradient,xGradient*5.0f, 400 mediumColor ); 401 402 graphics2D.setPaint( upperLeftGradientPaint ); 403 graphics2D.fill( graphics2D.getClip() ); 404 } 405 else if( this.gradientType == ApplyVerticalHighLight ) 406 { 407 408 this.gradientLength = xGradient; 409 if( gradientLength > this.getHeight()/2.5f ) 410 { 411 gradientLength = this.getHeight()/2.5f; 412 } 413 GradientPaint upperVerticalGradientPaint = 414 new GradientPaint( 0f,0f, 415 this.lightColor, 416 0f, gradientLength, 417 this.mediumColor ); 418 419 GradientPaint lowerVerticalGradientPaint = 420 new GradientPaint( 0f,getHeight(), 421 this.darkColor, 422 0f,getHeight() - gradientLength, 423 this.mediumColor ); 424 425 Shape saveClip = graphics2D.getClip(); 426 427 Rectangle rLower = new Rectangle( 0,getHeight()/2,getWidth(),1+getHeight()/2 ); 428 graphics2D.setPaint( lowerVerticalGradientPaint ); 429 graphics2D.fill( rLower ); 430 431 Rectangle rUpper = new Rectangle( 0,0,getWidth(),1+getHeight()/2 ); 432 graphics2D.setPaint( upperVerticalGradientPaint ); 433 graphics2D.fill( rUpper ); 434 435 graphics2D.setClip(saveClip); 436 } 437 graphics2D.setPaint( savePaint ); 438 super.paintChildren(graphics2D); 439 440 if( !introThreadHasBeenStarted ) 441 { 442 introThreadHasBeenStarted = true; 443 this.startIntroThread(); 444 } 445 } 447 448 449 450 454 public void update( Graphics g ) 455 { 456 paint(g); 458 } 459 460 461 public float getGradientLength() 462 { 463 return this.gradientLength; 464 } 465 466 467 468 469 470 474 public void setColorOffset( int newValue ) 475 { 476 this.colorOffset = newValue; 477 this.calculateColors(); 478 if( this.isShowing() && this.isVisible() ) 479 { 480 this.updateUI(); 481 } else 482 { 483 this.revalidate(); 484 } 485 } 486 487 488 private void startIntroThread() 489 { 490 final StartupThread startupThread = new StartupThread(this); 492 SwingUtilities.invokeLater( new Runnable () 495 { 496 public void run() 497 { 498 startupThread.start(); 499 } 500 }); 501 } 502 503 504 505 public int getFinalColorOffset() 506 { 507 return this.finalColorOffset; 508 } 509 510 511 512 513 514 518 private class StartupThread extends Thread 519 { 520 private CustomOceanImagePanel bgPanel; 521 522 public StartupThread( CustomOceanImagePanel bgPanel ) 523 { 524 this.bgPanel = bgPanel; 525 this.setDaemon(true); 526 } 527 528 public void run() 529 { 530 try 532 { 533 Thread.sleep(200); 534 } 535 catch( Exception sdfkjh ) 536 { 537 } 538 int loops = 16; 539 int offsetStep = bgPanel.getFinalColorOffset()/loops; 540 for( int i=1; i <= loops; i++ ) 541 { 542 try{ Thread.sleep(90); } catch(Exception wurscht ){} 543 final int colorOffset = i*offsetStep; 544 SwingUtilities.invokeLater( new Runnable () 545 { 546 public void run() 547 { 548 bgPanel.setColorOffset(colorOffset); 549 } 550 }); 551 try{ Thread.yield(); } catch(Exception wurscht ){} 552 } 553 } 554 555 } 557 558 559 560 561 } | Popular Tags |