ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.gui
Class JxTitledBorder

java.lang.Object
  extended by com.ehsbe.commons.gui.JxTitledBorder
All Implemented Interfaces:
Border

public class JxTitledBorder
extends Object
implements Border

Draws a title (if set) with a [gradient] background.

NOTE: If you change a property of the border after the border has been set to a component, you should revalidate() the component, if the property change caused a change wrt. to the insets of this border (usually caused by setting another position or font). After that, you should repaint() the component. If this is the only border of the component and insets have not been changed (foreground, background, title), you may restrict the paint to the border insets or call directly the borders paint method.

E.g.

 JPanel p = new JPanel();
 JxTitledBorder b = new JxTitledBorder(" ");
 p.setBorder(b);
 // ...
 b.setTitle("another none-empty string");
 Point2D p1 = new Point2D.Float(0, 0);
 Point2D p2 = new Point2D.Float(1.5f, 0);
 b.setBackground(new GradientPaint(p1, Color.RED, p2, Color.YELLOW));
 b.paintBorder(p, 0, 0, p.getWidth(), p.getHeight());
 

Version:
$Revision: 432 $
Author:
Jens Elkner

Field Summary
protected static int TEXT_GAP_H
          gap on top and on the bottom of the title
protected static int TEXT_GAP_V
          gap on top and on the bottom of the title
protected  BufferedImage tmpBackground
          cache for gradient background
protected  Paint tmpForeground
          cached value from getForeground()
 
Constructor Summary
JxTitledBorder()
          Create a Border without a title
JxTitledBorder(String title)
          Create a border with the given title
JxTitledBorder(String title, int horizontalAlignment)
          Create a border with the given title and alignment
JxTitledBorder(String title, int horizontalAlignment, int position)
          Create a border with the given title and alignment and position
 
Method Summary
static GradientPaint adjustGradient(GradientPaint gp, int width, int height)
          Calculate a new GradientPaint wrt. to the settings of the given gp, but adjust it to the current size of this component.
static GradientPaint createGradient(Color dark, Color light)
          Create a default Gradient adjusted to the given width.
 int getAlignment()
          Get the horizontal alignment of the title.
 Paint getBackground()
           
 Insets getBorderInsets(Component c)
          
 Font getFont()
           
 Paint getForeground()
           
static com.ehsbe.commons.gui.JxTitledBorder.PaintType getPaintType(Paint paint)
          Determine the type of a paint object.
 int getPosition()
           
 String getTitle()
           
 boolean isBorderOpaque()
          
static boolean isGradient(com.ehsbe.commons.gui.JxTitledBorder.PaintType type)
          Check, whether the given paint type is a gradient paint.
protected  void paintBackground(Graphics2D g2d, int xs, int ys, int ws, int hs)
          Convinience method to draw the background.
 void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
          
 void setAlignment(int horizontalAlignment)
          Set the horizontal alignment of the title.
 void setBackground(Paint background)
           
 void setFont(Font font)
           
 void setForeground(Paint foreground)
           
protected  void setForegroundPaint(Graphics2D g2d, int width, int height)
          Convinience method to set the foreground paint within a paint method to reduce redundant code.
 void setPosition(int position)
           
 void setTitle(String title)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEXT_GAP_V

protected static int TEXT_GAP_V
gap on top and on the bottom of the title


TEXT_GAP_H

protected static int TEXT_GAP_H
gap on top and on the bottom of the title


tmpBackground

protected transient BufferedImage tmpBackground
cache for gradient background


tmpForeground

protected transient Paint tmpForeground
cached value from getForeground()

Constructor Detail

JxTitledBorder

public JxTitledBorder()
Create a Border without a title


JxTitledBorder

public JxTitledBorder(String title)
Create a border with the given title

Parameters:
title - title to use

JxTitledBorder

public JxTitledBorder(String title,
                      int horizontalAlignment)
Create a border with the given title and alignment

Parameters:
title - title to set.
horizontalAlignment - SwingConstants.LEFT | SwingConstants.CENTER | SwingConstants.RIGHT

JxTitledBorder

public JxTitledBorder(String title,
                      int horizontalAlignment,
                      int position)
Create a border with the given title and alignment and position

Parameters:
title - title to set.
horizontalAlignment - SwingConstants.LEFT | SwingConstants.CENTER | SwingConstants.RIGHT
position - SwingConstants.TOP | SwingConstants.LEFT | SwingConstants.BOTTOM | SwingConstants.RIGHT
Method Detail

isBorderOpaque

public boolean isBorderOpaque()

Specified by:
isBorderOpaque in interface Border

adjustGradient

public static GradientPaint adjustGradient(GradientPaint gp,
                                           int width,
                                           int height)
Calculate a new GradientPaint wrt. to the settings of the given gp, but adjust it to the current size of this component.

The first point in the user space is the vector product of the first point in user space of the given Paint gp and this components current width and height.

The second point in the user space is the vector product of the second point in user space of the given Paint gp and this components current width and height.

Colors and [none-]linearity are kept as they are in the given Paint gp.

Parameters:
gp - the original
width - the width of the border
height - the height of the border part used to draw the title
Returns:
a to this component's size adjusted GradientPaint.

getPaintType

public static com.ehsbe.commons.gui.JxTitledBorder.PaintType getPaintType(Paint paint)
Determine the type of a paint object. Helps to reduce introspection and thus improve performance, when painting components.

Parameters:
paint - the paint to analyze
Returns:
the paint type

isGradient

public static boolean isGradient(com.ehsbe.commons.gui.JxTitledBorder.PaintType type)
Check, whether the given paint type is a gradient paint.

Parameters:
type - the paint type
Returns:
true, if the given paint type indicates a gradient paint.

setForegroundPaint

protected void setForegroundPaint(Graphics2D g2d,
                                  int width,
                                  int height)
Convinience method to set the foreground paint within a paint method to reduce redundant code. It checks and updates (if required) the cached foreground paint of this component. If the foreground paint is a gradient pain, the gradient adjusted paint is set. Otherwise the unmodified foreground paint will be used.

Parameters:
g2d - context, where the foreground paint should be set.
width - to which width the gradient should be adjusted
height - to which height the gradient should be adjusted
See Also:
getForeground(), Graphics2D.setPaint(java.awt.Paint)

paintBackground

protected void paintBackground(Graphics2D g2d,
                               int xs,
                               int ys,
                               int ws,
                               int hs)
Convinience method to draw the background.

If the background for this component is not set, it does nothing.

If the g2d is not a batik graphics context and the set background is a Gradient, a buffered image is setup with the size of the component and filled with the gradient,adjusted to the size of this component. This image is cached and will be reused for drawing the background to the g2d as long as the component was not invalidated.

g2d.fillRect(0, 0, this.width, this.height) is used to fill the appropriate area, so make sure, you have translated the component to the correct location, before calling this method.

Parameters:
g2d - graphics context
xs - start position x
ys - start position y
ws - width to paint
hs - height to paint

paintBorder

public void paintBorder(Component c,
                        Graphics g,
                        int x,
                        int y,
                        int width,
                        int height)

Specified by:
paintBorder in interface Border

getBorderInsets

public Insets getBorderInsets(Component c)

Specified by:
getBorderInsets in interface Border

getAlignment

public int getAlignment()
Get the horizontal alignment of the title.

Returns:
the alignment.

setAlignment

public void setAlignment(int horizontalAlignment)
Set the horizontal alignment of the title.

Parameters:
horizontalAlignment - SwingConstants.LEFT | SwingConstants.CENTER | SwingConstants.RIGHT

createGradient

public static GradientPaint createGradient(Color dark,
                                           Color light)
Create a default Gradient adjusted to the given width.

Parameters:
dark - color to use for the darker part of the gradient
light - color to use for the lighter part of the gradient
Returns:
a new GradientPaint

getBackground

public Paint getBackground()
Returns:
the background.

setBackground

public void setBackground(Paint background)
Parameters:
background - background to set.

getFont

public Font getFont()
Returns:
the font.

setFont

public void setFont(Font font)
Parameters:
font - font to set.

getForeground

public Paint getForeground()
Returns:
the foreground.

setForeground

public void setForeground(Paint foreground)
Parameters:
foreground - foreground to set.

getPosition

public int getPosition()
Returns:
the position.

setPosition

public void setPosition(int position)
Parameters:
position - position to set.

getTitle

public String getTitle()
Returns:
the title.

setTitle

public void setTitle(String title)
Parameters:
title - title to set.

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.