下面列出了javax.swing.SwingUtilities#layoutCompoundLabel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void layoutLabel(int tabPlacement, FontMetrics metrics, int tabIndex, String title, Icon icon, Rectangle tabRect, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
View v = getTextViewForTab(tabIndex);
if (v != null) {
tabPane.putClientProperty("html", v);
}
SwingUtilities.layoutCompoundLabel(tabPane, metrics, title, icon, SwingConstants.CENTER, SwingConstants.LEFT, SwingConstants.CENTER,
SwingConstants.CENTER, tabRect, iconRect, textRect, textIconGap);
tabPane.putClientProperty("html", null);
iconRect.x = tabRect.x + 8;
textRect.x = iconRect.x + iconRect.width + textIconGap;
}
protected void calculateGeometry(JLabel label) {
FontMetrics fm = label.getFontMetrics(label.getFont());
String text = label.getText();
Icon icon = label.getIcon();
int verticalAlignment = label.getVerticalAlignment();
int horizontalAlignment = label.getHorizontalAlignment();
int verticalTextPosition = label.getVerticalTextPosition();
int horizontalTextPosition = label.getHorizontalTextPosition();
int textIconGap = label.getIconTextGap();
viewR.setFrame(0, 0, getViewWidth(label), label.getHeight());
SwingUtilities.layoutCompoundLabel(fm, text, icon, verticalAlignment,
horizontalAlignment, verticalTextPosition,
horizontalTextPosition, viewR, iconRect, textRect, textIconGap);
textRect.x = label.getWidth() / 2 - textRect.width / 2;
iconRect.x = label.getWidth() / 2 - iconRect.width / 2;
iconRect.y = 0;
textRect.y = iconRect.height + label.getIconTextGap();
}
private String layout(AbstractButton b, FontMetrics fm, int width, int height) {
Insets i = b.getInsets();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = width - (i.right + viewRect.x);
viewRect.height = height - (i.bottom + viewRect.y);
textRect.x = textRect.y = textRect.width = textRect.height = 0;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
return SwingUtilities.layoutCompoundLabel(b, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(),
b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect,
iconRect, textRect, b.getText() == null ? 0 : b.getIconTextGap());
}
protected boolean checkIfPointInsideHyperlink(Point p) {
hitColumnIndex = table.columnAtPoint(p);
hitRowIndex = table.rowAtPoint(p);
if (hitColumnIndex != -1 && hitRowIndex != -1 &&
columnModelIndeces.contains(table.getColumnModel().
getColumn(hitColumnIndex).getModelIndex())) {
// We know point is within a hyperlink column, however we do further hit testing
// to see if point is within the text bounds on the hyperlink
TableCellRenderer renderer = table.getCellRenderer(hitRowIndex, hitColumnIndex);
JHyperlink hyperlink = (JHyperlink) table.prepareRenderer(renderer, hitRowIndex, hitColumnIndex);
// Convert the event to the renderer's coordinate system
cellRect = table.getCellRect(hitRowIndex, hitColumnIndex, false);
hyperlink.setSize(cellRect.width, cellRect.height);
p.translate(-cellRect.x, -cellRect.y);
cellRect.x = cellRect.y = 0;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
textRect.x = textRect.y = textRect.width = textRect.height = 0;
SwingUtilities.layoutCompoundLabel(
hyperlink.getFontMetrics(hyperlink.getFont()),
hyperlink.getText(), hyperlink.getIcon(),
hyperlink.getVerticalAlignment(),
hyperlink.getHorizontalAlignment(),
hyperlink.getVerticalTextPosition(),
hyperlink.getHorizontalTextPosition(),
cellRect, iconRect, textRect, hyperlink.getIconTextGap());
if (textRect.contains(p)) {
// point is within hyperlink text bounds
return true;
}
}
// point is not within a hyperlink's text bounds
hitRowIndex = -1;
hitColumnIndex = -1;
return false;
}
/**
* Layouts the label
*
* @param tabPlacement the placement of the tabs
* @param metrics the font metrics
* @param tabIndex the index of the tab
* @param title the title of the tab
* @param icon the icon of the tab
* @param tabRect the tab boundaries
* @param iconRect the icon boundaries
* @param textRect the text boundaries
* @param isSelected true whether the tab is selected, false otherwise
*/
@Override
protected void layoutLabel(int tabPlacement, FontMetrics metrics,
int tabIndex, String title, Icon icon,
Rectangle tabRect, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
javax.swing.text.View v = getTextViewForTab(tabIndex);
if (v != null) {
tabPane.putClientProperty("html", v);
}
SwingUtilities.layoutCompoundLabel((JComponent) tabPane,
metrics, title, icon,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
//SwingUtilities.TRAILING,
horizontalTextPosition,
tabRect,
iconRect,
textRect,
textIconGap + 2);
tabPane.putClientProperty("html", null);
int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
iconRect.x += xNudge;
iconRect.y += yNudge;
textRect.x += xNudge;
textRect.y += yNudge;
}
/**
* Layouts the label
*
* @param tabPlacement the placement of the tabs
* @param metrics the font metrics
* @param tabIndex the index of the tab
* @param title the title of the tab
* @param icon the icon of the tab
* @param tabRect the tab boundaries
* @param iconRect the icon boundaries
* @param textRect the text boundaries
* @param isSelected true whether the tab is selected, false otherwise
*/
@Override
protected void layoutLabel(int tabPlacement, FontMetrics metrics,
int tabIndex, String title, Icon icon,
Rectangle tabRect, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
javax.swing.text.View v = getTextViewForTab(tabIndex);
if (v != null) {
tabPane.putClientProperty("html", v);
}
SwingUtilities.layoutCompoundLabel((JComponent) tabPane,
metrics, title, icon,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
//SwingUtilities.TRAILING,
horizontalTextPosition,
tabRect,
iconRect,
textRect,
textIconGap + 2);
tabPane.putClientProperty("html", null);
int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
iconRect.x += xNudge;
iconRect.y += yNudge;
textRect.x += xNudge;
textRect.y += yNudge;
}
/**
* Layouts the label
*
* @param tabPlacement the placement of the tabs
* @param metrics the font metrics
* @param tabIndex the index of the tab
* @param title the title of the tab
* @param icon the icon of the tab
* @param tabRect the tab boundaries
* @param iconRect the icon boundaries
* @param textRect the text boundaries
* @param isSelected true whether the tab is selected, false otherwise
*/
@Override
protected void layoutLabel(int tabPlacement, FontMetrics metrics,
int tabIndex, String title, Icon icon,
Rectangle tabRect, Rectangle iconRect,
Rectangle textRect, boolean isSelected) {
textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
javax.swing.text.View v = getTextViewForTab(tabIndex);
if (v != null) {
tabPane.putClientProperty("html", v);
}
SwingUtilities.layoutCompoundLabel((JComponent) tabPane,
metrics, title, icon,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
SwingUtilities.CENTER,
//SwingUtilities.TRAILING,
horizontalTextPosition,
tabRect,
iconRect,
textRect,
textIconGap + 2);
tabPane.putClientProperty("html", null);
int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
iconRect.x += xNudge;
iconRect.y += yNudge;
textRect.x += xNudge;
textRect.y += yNudge;
}
/**
* Layouts the label
*
* @param tabPlacement the placement of the tabs
* @param metrics the font metrics
* @param tabIndex the index of the tab
* @param title the title of the tab
* @param icon the icon of the tab
* @param tabRect the tab boundaries
* @param iconRect the icon boundaries
* @param textRect the text boundaries
* @param isSelected true whether the tab is selected, false otherwise
*/
protected void layoutLabel(int tabPlacement, FontMetrics metrics,
int tabIndex, String title, Icon icon, Rectangle tabRect,
Rectangle iconRect, Rectangle textRect, boolean isSelected) {
textRect.x = textRect.y = iconRect.x = iconRect.y = 0;
javax.swing.text.View v = getTextViewForTab(tabIndex);
if (v != null) {
tabPane.putClientProperty("html", v);
}
SwingUtilities.layoutCompoundLabel(tabPane, metrics, title, icon,
SwingUtilities.CENTER, SwingUtilities.CENTER,
SwingUtilities.CENTER,
// SwingUtilities.TRAILING,
horizontalTextPosition, tabRect, iconRect, textRect,
textIconGap + 2);
tabPane.putClientProperty("html", null);
int xNudge = getTabLabelShiftX(tabPlacement, tabIndex, isSelected);
int yNudge = getTabLabelShiftY(tabPlacement, tabIndex, isSelected);
iconRect.x += xNudge;
iconRect.y += yNudge;
textRect.x += xNudge;
textRect.y += yNudge;
}
/**
* Positions tool tips exactly over the label text, also avoids showing empty tool tips (by returning <code>null</code>).
*/
@Override
public Point getToolTipLocation( MouseEvent event ) {
// If tool tip is provided by the renderer or the table itself, use default location:
if ( super.getToolTipText( event ) != null )
return super.getToolTipLocation( event );
// If no tool tip, return null to prevent displaying an empty tool tip
if ( getToolTipText( event ) == null )
return null;
// Now we need the position of the rendered text. This depends on many things (e.g. space reserved for the label,
// horizontal, vertical alignments, has icon, icon size, icon text gap etc.)
// The calculation is not easy, but we have built-in help:
final Point p = new Point( -4, -4 );
final Rectangle textR = new Rectangle();
SwingUtilities.layoutCompoundLabel( this, getFontMetrics( getFont() ), getText(), getIcon(), getVerticalAlignment(), getHorizontalAlignment(),
getVerticalTextPosition(), getHorizontalTextPosition(), getVisibleRect(), new Rectangle(), textR, getIconTextGap() );
p.x += textR.x;
p.y += textR.y;
// We have to account for border ourselves
if ( getBorder() != null ) {
final Insets insets = getBorder().getBorderInsets( this );
p.x += insets.left;
p.y += ( insets.top - insets.bottom ) / 2; // Might be negative? (that's why I don't use shift)
}
return p;
}
/**
* This method is called from Toolkit Thread and so it should not call any
* client code.
*/
@Override
void paintPeer(final Graphics g) {
if (!disposed) {
Dimension size = getPeerSize();
g.setColor( getPeerBackground() ); /* erase the existing button remains */
g.fillRect(0,0, size.width , size.height);
paintBorder(g,borderInsets.left,
borderInsets.top,
size.width-(borderInsets.left+borderInsets.right),
size.height-(borderInsets.top+borderInsets.bottom));
FontMetrics fm = g.getFontMetrics();
Rectangle textRect,iconRect,viewRect;
textRect = new Rectangle();
viewRect = new Rectangle();
iconRect = new Rectangle();
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
viewRect.x = contentAreaInsets.left;
viewRect.y = contentAreaInsets.top;
String llabel = (label != null) ? label : "";
// layout the text and icon
String text = SwingUtilities.layoutCompoundLabel(
fm, llabel, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, iconRect, textRect, 0);
Font f = getPeerFont();
g.setFont(f);
// perform UI specific press action, e.g. Windows L&F shifts text
if (pressed && armed) {
paintButtonPressed(g,target);
}
paintText(g, target, textRect, text);
if (hasFocus()) {
// paint UI specific focus
paintFocus(g,focusInsets.left,
focusInsets.top,
size.width-(focusInsets.left+focusInsets.right)-1,
size.height-(focusInsets.top+focusInsets.bottom)-1);
}
}
flush();
}
/**
* This method is called from Toolkit Thread and so it should not call any
* client code.
*/
@Override
void paintPeer(final Graphics g) {
if (!disposed) {
Dimension size = getPeerSize();
g.setColor( getPeerBackground() ); /* erase the existing button remains */
g.fillRect(0,0, size.width , size.height);
paintBorder(g,borderInsets.left,
borderInsets.top,
size.width-(borderInsets.left+borderInsets.right),
size.height-(borderInsets.top+borderInsets.bottom));
FontMetrics fm = g.getFontMetrics();
Rectangle textRect,iconRect,viewRect;
textRect = new Rectangle();
viewRect = new Rectangle();
iconRect = new Rectangle();
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
viewRect.x = contentAreaInsets.left;
viewRect.y = contentAreaInsets.top;
String llabel = (label != null) ? label : "";
// layout the text and icon
String text = SwingUtilities.layoutCompoundLabel(
fm, llabel, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, iconRect, textRect, 0);
Font f = getPeerFont();
g.setFont(f);
// perform UI specific press action, e.g. Windows L&F shifts text
if (pressed && armed) {
paintButtonPressed(g,target);
}
paintText(g, target, textRect, text);
if (hasFocus()) {
// paint UI specific focus
paintFocus(g,focusInsets.left,
focusInsets.top,
size.width-(focusInsets.left+focusInsets.right)-1,
size.height-(focusInsets.top+focusInsets.bottom)-1);
}
}
flush();
}
/**
* This method is called from Toolkit Thread and so it should not call any
* client code.
*/
@Override
void paintPeer(final Graphics g) {
if (!disposed) {
Dimension size = getPeerSize();
g.setColor( getPeerBackground() ); /* erase the existing button remains */
g.fillRect(0,0, size.width , size.height);
paintBorder(g,borderInsets.left,
borderInsets.top,
size.width-(borderInsets.left+borderInsets.right),
size.height-(borderInsets.top+borderInsets.bottom));
FontMetrics fm = g.getFontMetrics();
Rectangle textRect,iconRect,viewRect;
textRect = new Rectangle();
viewRect = new Rectangle();
iconRect = new Rectangle();
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
viewRect.x = contentAreaInsets.left;
viewRect.y = contentAreaInsets.top;
String llabel = (label != null) ? label : "";
// layout the text and icon
String text = SwingUtilities.layoutCompoundLabel(
fm, llabel, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, iconRect, textRect, 0);
Font f = getPeerFont();
g.setFont(f);
// perform UI specific press action, e.g. Windows L&F shifts text
if (pressed && armed) {
paintButtonPressed(g,target);
}
paintText(g, target, textRect, text);
if (hasFocus()) {
// paint UI specific focus
paintFocus(g,focusInsets.left,
focusInsets.top,
size.width-(focusInsets.left+focusInsets.right)-1,
size.height-(focusInsets.top+focusInsets.bottom)-1);
}
}
flush();
}
/**
* This method is called from Toolkit Thread and so it should not call any
* client code.
*/
@Override
void paintPeer(final Graphics g) {
if (!disposed) {
Dimension size = getPeerSize();
g.setColor( getPeerBackground() ); /* erase the existing button remains */
g.fillRect(0,0, size.width , size.height);
paintBorder(g,borderInsets.left,
borderInsets.top,
size.width-(borderInsets.left+borderInsets.right),
size.height-(borderInsets.top+borderInsets.bottom));
FontMetrics fm = g.getFontMetrics();
Rectangle textRect,iconRect,viewRect;
textRect = new Rectangle();
viewRect = new Rectangle();
iconRect = new Rectangle();
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
viewRect.x = contentAreaInsets.left;
viewRect.y = contentAreaInsets.top;
String llabel = (label != null) ? label : "";
// layout the text and icon
String text = SwingUtilities.layoutCompoundLabel(
fm, llabel, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, iconRect, textRect, 0);
Font f = getPeerFont();
g.setFont(f);
// perform UI specific press action, e.g. Windows L&F shifts text
if (pressed && armed) {
paintButtonPressed(g,target);
}
paintText(g, target, textRect, text);
if (hasFocus()) {
// paint UI specific focus
paintFocus(g,focusInsets.left,
focusInsets.top,
size.width-(focusInsets.left+focusInsets.right)-1,
size.height-(focusInsets.top+focusInsets.bottom)-1);
}
}
flush();
}
public void originalPaint(Graphics graphics) {
Graphics2D g2d = (Graphics2D)graphics;
if (!isEnabled()) {
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.3f));
}
graphics.setColor(color_text);
graphics.drawImage(image, 0, 0, null);
if (text == null) {
// no text to draw
return;
}
if (fm == null) {
// XXX(-ttran) this happened on Japanese Windows NT, don't
// fully understand why
return;
}
SwingUtilities.layoutCompoundLabel(fm, text, null,
SwingConstants.BOTTOM, SwingConstants.LEFT, SwingConstants.BOTTOM, SwingConstants.LEFT,
this.view, new Rectangle(), rect, 0);
// turn anti-aliasing on for the splash text
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString(text, rect.x, rect.y + fm.getAscent());
// Draw progress bar if applicable
if (draw_bar && Boolean.getBoolean("netbeans.splash.nobar") == false && maxSteps > 0/* && barLength > 0*/) {
graphics.setColor(color_bar);
graphics.fillRect(bar.x, bar.y, barStart + barLength, bar.height);
graphics.setColor(color_corner);
graphics.drawLine(bar.x, bar.y, bar.x, bar.y + bar.height);
graphics.drawLine(bar.x + barStart + barLength, bar.y, bar.x + barStart + barLength, bar.y + bar.height);
graphics.setColor(color_edge);
graphics.drawLine(bar.x, bar.y + bar.height / 2, bar.x, bar.y + bar.height / 2);
graphics.drawLine(bar.x + barStart + barLength, bar.y + bar.height / 2, bar.x + barStart + barLength, bar.y + bar.height / 2);
barStart += barLength;
barLength = 0;
}
}
/**
* This method is called from Toolkit Thread and so it should not call any
* client code.
*/
@Override
void paintPeer(final Graphics g) {
if (!disposed) {
Dimension size = getPeerSize();
g.setColor( getPeerBackground() ); /* erase the existing button remains */
g.fillRect(0,0, size.width , size.height);
paintBorder(g,borderInsets.left,
borderInsets.top,
size.width-(borderInsets.left+borderInsets.right),
size.height-(borderInsets.top+borderInsets.bottom));
FontMetrics fm = g.getFontMetrics();
Rectangle textRect,iconRect,viewRect;
textRect = new Rectangle();
viewRect = new Rectangle();
iconRect = new Rectangle();
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
viewRect.x = contentAreaInsets.left;
viewRect.y = contentAreaInsets.top;
String llabel = (label != null) ? label : "";
// layout the text and icon
String text = SwingUtilities.layoutCompoundLabel(
fm, llabel, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, iconRect, textRect, 0);
Font f = getPeerFont();
g.setFont(f);
// perform UI specific press action, e.g. Windows L&F shifts text
if (pressed && armed) {
paintButtonPressed(g,target);
}
paintText(g, target, textRect, text);
if (hasFocus()) {
// paint UI specific focus
paintFocus(g,focusInsets.left,
focusInsets.top,
size.width-(focusInsets.left+focusInsets.right)-1,
size.height-(focusInsets.top+focusInsets.bottom)-1);
}
}
flush();
}
@Override
protected void paintComponent(Graphics g) {
// Set the foreground on the fly to ensure the text is painted
// with the proper color in super.paintComponent
ButtonModel model = getModel();
if (model.isArmed()) {
super.setForeground(activeForeground);
} else if (visited) {
super.setForeground(visitedForeground);
} else {
super.setForeground(normalForeground);
}
super.paintComponent(g);
if (drawUnderline) {
Insets insets = getInsets();
viewRect.x = insets.left;
viewRect.y = insets.top;
viewRect.width = getWidth() - insets.left - insets.right;
viewRect.height = getHeight() - insets.top - insets.bottom;
int baseline = getBaseline(viewRect.width, viewRect.height);
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
textRect.x = textRect.y = textRect.width = textRect.height = 0;
SwingUtilities.layoutCompoundLabel(g.getFontMetrics(), getText(),
getIcon(), getVerticalAlignment(), getHorizontalAlignment(),
getVerticalTextPosition(), getHorizontalTextPosition(),
viewRect, iconRect, textRect, getIconTextGap());
// getBaseline not returning correct results, so workaround for now
if (UIManager.getLookAndFeel().getName().equals("Nimbus")) {
baseline += 7;
} else {
baseline += 3;
}
g.setColor(getForeground());
g.drawLine(textRect.x,
baseline,
textRect.x + textRect.width,
baseline);
}
}
@Override
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
Dimension size = b.getSize();
FontMetrics fm = g.getFontMetrics();
Insets i = c.getInsets();
Rectangle viewRect = new Rectangle(size);
viewRect.x += i.left;
viewRect.y += i.top;
viewRect.width -= i.right + viewRect.x;
viewRect.height -= i.bottom + viewRect.y;
Rectangle iconRect = new Rectangle();
Rectangle textRect = new Rectangle();
Font f = c.getFont();
g.setFont(f);
String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(),
b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect,
textRect, b.getText() == null ? 0 : b.getIconTextGap());
g.setColor(b.getBackground());
if (b.isContentAreaFilled()) {
if (RapidLookTools.isToolbarButton(b)) {
RapidLookTools.drawToolbarButton(g, b);
} else {
RapidLookTools.drawButton(b, g, RapidLookTools.createShapeForButton(b));
}
}
if (b.getIcon() != null) {
paintIcon(g, b, iconRect);
}
if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g, textRect);
} else {
paintText(g, b, textRect, text);
}
}
if (b.isFocusPainted() && b.hasFocus()) {
paintFocus(g, b, viewRect, textRect, iconRect);
}
if (!RapidLookTools.isToolbarButton(b)) {
if (b.isBorderPainted()) {
RapidLookTools.drawButtonBorder(b, g, RapidLookTools.createBorderShapeForButton(b));
}
}
}
/**
* This method is called from Toolkit Thread and so it should not call any
* client code.
*/
@Override
void paintPeer(final Graphics g) {
if (!disposed) {
Dimension size = getPeerSize();
g.setColor( getPeerBackground() ); /* erase the existing button remains */
g.fillRect(0,0, size.width , size.height);
paintBorder(g,borderInsets.left,
borderInsets.top,
size.width-(borderInsets.left+borderInsets.right),
size.height-(borderInsets.top+borderInsets.bottom));
FontMetrics fm = g.getFontMetrics();
Rectangle textRect,iconRect,viewRect;
textRect = new Rectangle();
viewRect = new Rectangle();
iconRect = new Rectangle();
viewRect.width = size.width - (contentAreaInsets.left+contentAreaInsets.right);
viewRect.height = size.height - (contentAreaInsets.top+contentAreaInsets.bottom);
viewRect.x = contentAreaInsets.left;
viewRect.y = contentAreaInsets.top;
String llabel = (label != null) ? label : "";
// layout the text and icon
String text = SwingUtilities.layoutCompoundLabel(
fm, llabel, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, iconRect, textRect, 0);
Font f = getPeerFont();
g.setFont(f);
// perform UI specific press action, e.g. Windows L&F shifts text
if (pressed && armed) {
paintButtonPressed(g,target);
}
paintText(g, target, textRect, text);
if (hasFocus()) {
// paint UI specific focus
paintFocus(g,focusInsets.left,
focusInsets.top,
size.width-(focusInsets.left+focusInsets.right)-1,
size.height-(focusInsets.top+focusInsets.bottom)-1);
}
}
flush();
}
@Override
protected void paintComponent(Graphics g) {
// Set the foreground on the fly to ensure the text is painted
// with the proper color in super.paintComponent
ButtonModel model = getModel();
if (model.isArmed()) {
super.setForeground(activeForeground);
} else if (visited) {
super.setForeground(visitedForeground);
} else {
super.setForeground(normalForeground);
}
super.paintComponent(g);
if (drawUnderline) {
Insets insets = getInsets();
viewRect.x = insets.left;
viewRect.y = insets.top;
viewRect.width = getWidth() - insets.left - insets.right;
viewRect.height = getHeight() - insets.top - insets.bottom;
int baseline = getBaseline(viewRect.width, viewRect.height);
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
textRect.x = textRect.y = textRect.width = textRect.height = 0;
SwingUtilities.layoutCompoundLabel(g.getFontMetrics(), getText(),
getIcon(), getVerticalAlignment(), getHorizontalAlignment(),
getVerticalTextPosition(), getHorizontalTextPosition(),
viewRect, iconRect, textRect, getIconTextGap());
// getBaseline not returning correct results, so workaround for now
if (UIManager.getLookAndFeel().getName().equals("Nimbus")) {
baseline += 7;
} else {
baseline += 3;
}
g.setColor(getForeground());
g.drawLine(textRect.x,
baseline,
textRect.x + textRect.width,
baseline);
}
}
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
Insets i = c.getInsets();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = b.getWidth() - (i.right + viewRect.x);
viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
textRect.x = textRect.y = textRect.width = textRect.height = 0;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
Font f = c.getFont();
g.setFont(f);
// layout the text and icon
String text =
SwingUtilities.layoutCompoundLabel(
c,
fm,
b.getText(),
b.getIcon(),
b.getVerticalAlignment(),
b.getHorizontalAlignment(),
b.getVerticalTextPosition(),
b.getHorizontalTextPosition(),
viewRect,
iconRect,
textRect,
b.getText() == null ? 0 : b.getIconTextGap());
clearTextShiftOffset();
// perform UI specific press action, e.g. Windows L&F shifts text
if (model.isArmed() && model.isPressed()) {
paintButtonPressed(g, b);
}
// Paint the Icon
if (b.getIcon() != null) {
paintIcon(g, c, iconRect);
}
Composite oldComposite = ((Graphics2D)g).getComposite();
if (model.isRollover()) {
((Graphics2D)g).setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
}
if (text != null && !text.equals("")) {
View v = (View)c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
textRect.x += getTextShiftOffset();
textRect.y += getTextShiftOffset();
v.paint(g, textRect);
textRect.x -= getTextShiftOffset();
textRect.y -= getTextShiftOffset();
} else {
paintText(g, b, textRect, text);
}
}
if (b.isFocusPainted() && b.hasFocus()) {
// paint UI specific focus
paintFocus(g, b, viewRect, textRect, iconRect);
}
((Graphics2D)g).setComposite(oldComposite);
}