Package de.sciss.eisenkraut.gui

Source Code of de.sciss.eisenkraut.gui.RoundedBorder

/*
*  RoundedBorder.java
*  Eisenkraut
*
*  Copyright (c) 2004-2014 Hanns Holger Rutz. All rights reserved.
*
*  This software is published under the GNU General Public License v3+
*
*
*  For further information, please contact Hanns Holger Rutz at
*  contact@sciss.de
*/

package de.sciss.eisenkraut.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.awt.geom.RoundRectangle2D;

import javax.swing.border.AbstractBorder;

/**
*  A border looking like aqua's search-field border
*
@author    Hanns Holger Rutz
@version  0.70, 07-Dec-07
*/
public class RoundedBorder
extends AbstractBorder
{
  private final int    radius    = 11;
  private final int    diameter  = radius << 1;
  private final Insets  insets    = new Insets( 3, radius, 1, radius );

  private static final Color colrDark    = new Color( 0x00, 0x00, 0x00, 0x88 );
  private static final Color colrLight  = new Color( 0xFF, 0xFF, 0xFF, 0xD8 );
//  private static final Color colrLight  = new Color( 0xFF, 0x00, 0x00, 0xFF );
  private static final Color colrDark2  = new Color( 0x00, 0x00, 0x00, 0x38 );
  private static final Color colrClearD  = new Color( 0x00, 0x00, 0x00, 0x00 );
  private static final Color colrClearL  = new Color( 0xFF, 0xFF, 0xFF, 0x00 );
  private static final Stroke  strkOutline  = new BasicStroke( 1.0f );
  private static final Stroke  strkInline  = new BasicStroke( 2.0f );
 
  private Color  colrBg    = Color.white;
  private Paint  pntInline, pntOutlineT, pntOutlineB;

  private Shape  shpBg, shpInline, shpOutline;

  private int    recentWidth    = -1;
  private int    recentHeight  = -1;

  public RoundedBorder()
  {
    super();
  }
 
  public RoundedBorder( Color c )
  {
    this();
    setColor( c );
  }
 
  public void setColor( Color c )
  {
    colrBg  = c;
  }
 
  public Insets getBorderInsets( Component c )
  {
    return new Insets( insets.top, insets.left, insets.bottom, insets.right );
  }
 
  public Insets getBorderInsets( Component c, Insets i )
  {
    i.top    = insets.top;
    i.left    = insets.left;
    i.bottom  = insets.bottom;
    i.right    = insets.right;
    return i;
  }
 
  public void paintBorder( Component c, Graphics g, int x, int y, int width, int height )
  {
    final Graphics2D    g2      = (Graphics2D) g;
    final AffineTransform  atOrig    = g2.getTransform();
 
    g2.translate( x, y );
 
    if( (width != recentWidth) || (height != recentHeight) ) {
      if( height != recentHeight ) {
        final int hh  = height >> 1;
        pntOutlineT    = new GradientPaint( 0, 0, colrDark, 0, hh, colrClearD );
        pntOutlineB    = new GradientPaint( 0, hh, colrClearL, 0, height - 2, colrLight );
        pntInline    = new GradientPaint( 0, 0, colrDark2, 0, hh, colrClearD );
      }
     
      final RectangularShape  r  = new RoundRectangle2D.Float( 0.5f, 0, width - 1, height, diameter, diameter );
      final RectangularShape  r2  = new RoundRectangle2D.Float( 0.5f, 0, width - 2, height - 1, diameter, diameter );
      final Area        a  = new Area( r );
      a.subtract( new Area( new Rectangle2D.Float( insets.left, insets.top,
        width - insets.left - insets.right, height - insets.top - insets.bottom )));

      shpOutline    = strkOutline.createStrokedShape( r2 );
      shpInline    = strkInline.createStrokedShape( r2 );
      shpBg      = a;

      recentWidth    = width;
      recentHeight  = height;
    }
             
    g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    g2.setPaint( colrBg );
    g2.fill( shpBg );
    g2.setPaint( pntOutlineT );
    g2.fill( shpOutline );
    g2.setPaint( pntOutlineB );
    g2.fill( shpOutline );
    g2.translate( 0, 1 );
    g2.setPaint( pntInline );
    g2.fill( shpInline );

    g2.setTransform( atOrig );
  }
}
TOP

Related Classes of de.sciss.eisenkraut.gui.RoundedBorder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.