multi colour titledborder

Hello,Anyone know how to do a tltled border with more than one colour in the title. I have tried using html but it dont work.Ta,Jim
[159 byte] By [patumairea] at [2007-11-27 8:00:59]
# 1
http://www.java2s.com/Code/Java/2D-Graphics-GUI/GradientPaintdemo.htm http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GradientPaint.html just make your own border...... like look at the source code for Ttiled boder and then extend it but over right the paint border method
Nibura at 2007-7-12 19:43:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

hi!

try this

[your component].setBorder(new TitledBorder("Hello") {

boolean isLeftToRight( Component c ) {

return c.getComponentOrientation().isLeftToRight();

}

private Point textLoc = new Point();

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

Border border = getBorder();

if (getTitle() == null || getTitle().equals("")) {

if (border != null) {

border.paintBorder(c, g, x, y, width, height);

}

return;

}

Rectangle grooveRect = new Rectangle(x + EDGE_SPACING, y + EDGE_SPACING,

width - (EDGE_SPACING * 2),

height - (EDGE_SPACING * 2));

Font font = g.getFont();

Color color = g.getColor();

g.setFont(getFont(c));

JComponent jc = (c instanceof JComponent) ? (JComponent)c : null;

FontMetrics fm = SwingUtilities2.getFontMetrics(jc, g);

int fontHeight = fm.getHeight();

int descent = fm.getDescent();

int ascent = fm.getAscent();

int diff;

int stringWidth = SwingUtilities2.stringWidth(jc, fm,

getTitle());

Insetsinsets;

if (border != null) {

insets = border.getBorderInsets(c);

} else {

insets = new Insets(0, 0, 0, 0);

}

int titlePos = getTitlePosition();

switch (titlePos) {

case ABOVE_TOP:

diff = ascent + descent + (Math.max(EDGE_SPACING,

TEXT_SPACING*2) - EDGE_SPACING);

grooveRect.y += diff;

grooveRect.height -= diff;

textLoc.y = grooveRect.y - (descent + TEXT_SPACING);

break;

case TOP:

case DEFAULT_POSITION:

diff = Math.max(0, ((ascent/2) + TEXT_SPACING) - EDGE_SPACING);

grooveRect.y += diff;

grooveRect.height -= diff;

textLoc.y = (grooveRect.y - descent) +

(insets.top + ascent + descent)/2;

break;

case BELOW_TOP:

textLoc.y = grooveRect.y + insets.top + ascent + TEXT_SPACING;

break;

case ABOVE_BOTTOM:

textLoc.y = (grooveRect.y + grooveRect.height) -

(insets.bottom + descent + TEXT_SPACING);

break;

case BOTTOM:

grooveRect.height -= fontHeight/2;

textLoc.y = ((grooveRect.y + grooveRect.height) - descent) +

((ascent + descent) - insets.bottom)/2;

break;

case BELOW_BOTTOM:

grooveRect.height -= fontHeight;

textLoc.y = grooveRect.y + grooveRect.height + ascent +

TEXT_SPACING;

break;

}

int justification = getTitleJustification();

if(isLeftToRight(c)) {

if(justification==LEADING ||

justification==DEFAULT_JUSTIFICATION) {

justification = LEFT;

}

else if(justification==TRAILING) {

justification = RIGHT;

}

}

else {

if(justification==LEADING ||

justification==DEFAULT_JUSTIFICATION) {

justification = RIGHT;

}

else if(justification==TRAILING) {

justification = LEFT;

}

}

switch (justification) {

case LEFT:

textLoc.x = grooveRect.x + TEXT_INSET_H + insets.left;

break;

case RIGHT:

textLoc.x = (grooveRect.x + grooveRect.width) -

(stringWidth + TEXT_INSET_H + insets.right);

break;

case CENTER:

textLoc.x = grooveRect.x +

((grooveRect.width - stringWidth) / 2);

break;

}

// If title is positioned in middle of border AND its fontsize

// is greater than the border's thickness, we'll need to paint

// the border in sections to leave space for the component's background

// to show through the title.

//

if (border != null) {

if (((titlePos == TOP || titlePos == DEFAULT_POSITION) &&

(grooveRect.y > textLoc.y - ascent)) ||

(titlePos == BOTTOM &&

(grooveRect.y + grooveRect.height < textLoc.y + descent))) {

Rectangle clipRect = new Rectangle();

// save original clip

Rectangle saveClip = g.getClipBounds();

// paint strip left of text

clipRect.setBounds(saveClip);

if (computeIntersection(clipRect, x, y, textLoc.x-1-x, height)) {

g.setClip(clipRect);

border.paintBorder(c, g, grooveRect.x, grooveRect.y,

grooveRect.width, grooveRect.height);

}

// paint strip right of text

clipRect.setBounds(saveClip);

if (computeIntersection(clipRect, textLoc.x+stringWidth+1, y,

x+width-(textLoc.x+stringWidth+1), height)) {

g.setClip(clipRect);

border.paintBorder(c, g, grooveRect.x, grooveRect.y,

grooveRect.width, grooveRect.height);

}

if (titlePos == TOP || titlePos == DEFAULT_POSITION) {

// paint strip below text

clipRect.setBounds(saveClip);

if (computeIntersection(clipRect, textLoc.x-1, textLoc.y+descent,

stringWidth+2, y+height-textLoc.y-descent)) {

g.setClip(clipRect);

border.paintBorder(c, g, grooveRect.x, grooveRect.y,

grooveRect.width, grooveRect.height);

}

} else { // titlePos == BOTTOM

// paint strip above text

clipRect.setBounds(saveClip);

if (computeIntersection(clipRect, textLoc.x-1, y,

stringWidth+2, textLoc.y - ascent - y)) {

g.setClip(clipRect);

border.paintBorder(c, g, grooveRect.x, grooveRect.y,

grooveRect.width, grooveRect.height);

}

}

// restore clip

g.setClip(saveClip);

} else {

border.paintBorder(c, g, grooveRect.x, grooveRect.y,

grooveRect.width, grooveRect.height);

}

}

//g.setColor(getTitleColor());

//SwingUtilities2.drawString(jc, g, getTitle(), textLoc.x, textLoc.y);

g.setColor(Color.RED);

SwingUtilities2.drawString(jc, g, "H", textLoc.x, textLoc.y);

g.setColor(Color.BLUE);

SwingUtilities2.drawString(jc, g, "e", textLoc.x + SwingUtilities2.stringWidth(jc, fm, "e"), textLoc.y);

g.setColor(Color.GREEN);

SwingUtilities2.drawString(jc, g, "llo", textLoc.x + SwingUtilities2.stringWidth(jc, fm, "He"), textLoc.y);

g.setFont(font);

g.setColor(color);

}

private boolean computeIntersection(Rectangle dest,

int rx, int ry, int rw, int rh) {

int x1 = Math.max(rx, dest.x);

int x2 = Math.min(rx + rw, dest.x + dest.width);

int y1 = Math.max(ry, dest.y);

int y2 = Math.min(ry + rh, dest.y + dest.height);

dest.x = x1;

dest.y = y1;

dest.width = x2 - x1;

dest.height = y2 - y1;

if (dest.width <= 0 || dest.height <= 0) {

return false;

}

return true;

}

});

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 19:43:02 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks a lot guys, especially Aniruddha-Here for the code.It does exactly what I want.Have a good un,Jim
patumairea at 2007-7-12 19:43:02 > top of Java-index,Desktop,Core GUI APIs...