Example usage for javax.swing JLabel getGraphics

List of usage examples for javax.swing JLabel getGraphics

Introduction

In this page you can find the example usage for javax.swing JLabel getGraphics.

Prototype

@BeanProperty(bound = false)
public Graphics getGraphics() 

Source Link

Document

Returns this component's graphics context, which lets you draw on a component.

Usage

From source file:Main.java

public static void setLabelTop(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    label.setText(str);//  w w  w  .  ja  va2  s .  co  m
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth / 2;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelAboveOut(JLabel label, String str, int x, int y, int width) {
    Graphics g = label.getGraphics();
    label.setText(str);//www .j a v a2  s .  c  o m
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = x + width / 2 - strWidth / 2;
    int lY = y - g.getFont().getSize();
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
}

From source file:Main.java

public static void setLabelCenter(JLabel label, String str, int width, int height) {
    Graphics g = label.getGraphics();
    Color c = g.getColor();/*from  w  ww. java2s  .  c o  m*/
    g.setColor(Color.BLUE);
    label.setText(str);
    int strWidth = SwingUtilities.computeStringWidth(g.getFontMetrics(), str);
    int lX = width / 2 - strWidth;
    int lY = g.getFont().getSize() / 2;
    //System.out.printf("\"label\"+%s X:%d,Y:%d\n",str,lX,lY);
    label.setBounds(lX, lY, strWidth, g.getFont().getSize());
    g.setColor(c);
}

From source file:Main.java

public static Rectangle getTextRectangle(JLabel label) {

    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

    if ((icon == null) && (text == null)) {
        return null;
    }// w w w  . ja v  a 2s  .com

    Rectangle paintIconR = new Rectangle();
    Rectangle paintTextR = new Rectangle();
    Rectangle paintViewR = new Rectangle();
    Insets paintViewInsets = new Insets(0, 0, 0, 0);

    paintViewInsets = label.getInsets(paintViewInsets);
    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;
    paintViewR.width = label.getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.height = label.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    Graphics g = label.getGraphics();
    if (g == null) {
        return null;
    }
    String clippedText = SwingUtilities.layoutCompoundLabel(label, g.getFontMetrics(), text, icon,
            label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getVerticalTextPosition(),
            label.getHorizontalTextPosition(), paintViewR, paintIconR, paintTextR, label.getIconTextGap());

    return paintTextR;
}