Java Code Examples for javax.swing.JLabel#getWidth()

The following examples show how to use javax.swing.JLabel#getWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PopupPanel.java    From SmartIM with Apache License 2.0 6 votes vote down vote up
void setLabelText(JLabel label, String text) {
    StringBuilder builder = new StringBuilder("<html>");
    char[] chars = text.toCharArray();
    FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
    int start = 0;
    int len = 0;
    while (start + len < text.length()) {
        while (true) {
            len++;
            if (start + len > text.length())
                break;
            if (fontMetrics.charsWidth(chars, start, len) > label.getWidth()) {
                break;
            }
        }
        builder.append(chars, start, len - 1).append("<br/>");
        start = start + len - 1;
        len = 0;
    }
    builder.append(chars, start, text.length() - start);
    builder.append("</html>");
    label.setText(builder.toString());
}
 
Example 2
Source File: ThumbnailLabelUI.java    From pumpernickel with MIT License 6 votes vote down vote up
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();
}
 
Example 3
Source File: bug6302464.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static HashSet getAntialiasedColors(Object aaHint, int lcdContrast) {

        JLabel label = new JLabel("ABCD");
        label.setSize(label.getPreferredSize());
        label.putClientProperty(KEY_TEXT_ANTIALIASING, aaHint);
        label.putClientProperty(KEY_TEXT_LCD_CONTRAST, lcdContrast);

        int w = label.getWidth();
        int h = label.getHeight();

        BufferedImage buffImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImage.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        label.paint(g);
        g.dispose();

        HashSet<Color> colors = new HashSet<>();

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                Color color = new Color(buffImage.getRGB(i, j));
                colors.add(color);
            }
        }

        return colors;
    }
 
Example 4
Source File: CategoryList.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private static Icon centeredIcon(final Icon icon, final int width, final int height) {
    JLabel l = new JLabel(icon);
    l.setIconTextGap(0);
    l.setBorder(null);
    l.setSize(width, height);
    
    BufferedImage img = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_ARGB);
    l.paint(img.getGraphics());
    
    return new ImageIcon(img);
}
 
Example 5
Source File: AquaThumbnailLabelUI.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
protected void calculateGeometry(JLabel label) {
	super.calculateGeometry(label);
	iconRect.y += 4;
	iconRect.height = 64;
	iconRect.width = 64;
	iconRect.x = label.getWidth() / 2 - 32;

	textRect.y = 72 + label.getIconTextGap();
}
 
Example 6
Source File: ProgressPanel.java    From swift-explorer with Apache License 2.0 5 votes vote down vote up
private String processTextToFit (JLabel lbl, String text)
{
	if (text == null)
		return "" ;
	FontMetrics fm = lbl.getFontMetrics(lbl.getFont());
	if (fm == null)
		return text ;
	int textWidth = fm.stringWidth(text);
	int lblWidth = lbl.getWidth() ;
	if (lblWidth >= textWidth)
		return text ;
	
	// we assume that the set of all characters have a mean length equal or smaller than 'Aa' / 2
	String dots = " ... " ;
	int charWidth = fm.stringWidth("Aa") / 2;
	int dotsWidth = fm.stringWidth(dots);
	int maxNumChar = (lblWidth - dotsWidth) / charWidth ;
	int blockWidth = maxNumChar / 2 ;
	if (blockWidth <= 0)
		return text ; 
	
	StringBuilder sb = new StringBuilder () ;
	sb.append(text.substring(0, blockWidth)) ;
	sb.append(dots) ;
	sb.append(text.substring(text.length() - blockWidth)) ;
	return sb.toString() ;
}
 
Example 7
Source File: ThumbNailBuilder.java    From nordpos with GNU General Public License v3.0 5 votes vote down vote up
public Image getThumbNailText(Image img, String text) {

        img = getThumbNail(img);

        BufferedImage imgtext = new BufferedImage(img.getWidth(null), img.getHeight(null),  BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = imgtext.createGraphics();

        // The text
        JLabel label = new JLabel();
        label.setOpaque(false);
        label.setFont(label.getFont().deriveFont((float)m_font_size));
        //label.setText(text);
        label.setText("<html>" + text + "</html>");
        label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        Dimension d = label.getPreferredSize();
        //label.setBounds(0, 0, imgtext.getWidth(), d.height);
        label.setBounds(0, 0, imgtext.getWidth(), imgtext.getHeight());

        // The background
        Color c1 = new Color(0xff, 0xff, 0xff, 0x40);
        Color c2 = new Color(0xff, 0xff, 0xff, 0xd0);

//        Point2D center = new Point2D.Float(imgtext.getWidth() / 2, label.getHeight());
//        float radius = imgtext.getWidth() / 3;
//        float[] dist = {0.1f, 1.0f};
//        Color[] colors = {c2, c1};
//        Paint gpaint = new RadialGradientPaint(center, radius, dist, colors);
        Paint gpaint = new GradientPaint(new Point(0,0), c1, new Point(label.getWidth() / 2, 0), c2, true);

        g2d.drawImage(img, 0, 0, null);
        g2d.translate(0, imgtext.getHeight() - label.getHeight());
        g2d.setPaint(gpaint);
        g2d.fillRect(0 , 0, imgtext.getWidth(), label.getHeight());
        label.paint(g2d);

        g2d.dispose();

        return imgtext;
    }
 
Example 8
Source File: UnitMoveAnimation.java    From freecol with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void executeWithUnitOutForAnimation(JLabel unitLabel) {
    final GUI gui = getGUI();
    final float scale = gui.getAnimationScale();
    final int movementRatio = (int)(Math.pow(2, this.speed + 1) * scale);
    final Rectangle r1 = gui.getAnimationTileBounds(this.sourceTile);
    final Rectangle r2 = gui.getAnimationTileBounds(this.destinationTile);
    final Rectangle bounds = r1.union(r2);
    final double xratio = ImageLibrary.TILE_SIZE.width
        / (double)ImageLibrary.TILE_SIZE.height;

    // Tile positions should be valid at this point.
    final Point srcP = gui.getAnimationTilePosition(this.sourceTile);
    if (srcP == null) {
        logger.warning("Failed move animation for " + this.unit
            + " at source tile: " + this.sourceTile);
        return;
    }
    final Point dstP = gui.getAnimationTilePosition(this.destinationTile);
    if (dstP == null) {
        logger.warning("Failed move animation for " + this.unit
            + " at destination tile: " + this.destinationTile);
        return;
    }
        
    final int labelWidth = unitLabel.getWidth();
    final int labelHeight = unitLabel.getHeight();
    final Point srcPoint = gui.getAnimationPosition(labelWidth, labelHeight, srcP);
    final Point dstPoint = gui.getAnimationPosition(labelWidth, labelHeight, dstP);
    final int stepX = (int)Math.signum(dstPoint.getX() - srcPoint.getX());
    final int stepY = (int)Math.signum(dstPoint.getY() - srcPoint.getY());

    Point point = srcPoint;
    long time = now(), dropFrames = 0;
    while (!point.equals(dstPoint)) {
        point.x += stepX * xratio * movementRatio;
        point.y += stepY * movementRatio;
        if ((stepX < 0 && point.x < dstPoint.x)
            || (stepX > 0 && point.x > dstPoint.x)) {
            point.x = dstPoint.x;
        }
        if ((stepY < 0 && point.y < dstPoint.y)
            || (stepY > 0 && point.y > dstPoint.y)) {
            point.y = dstPoint.y;
        }
        if (dropFrames <= 0) {
            unitLabel.setLocation(point);
            gui.paintImmediately(bounds);
            long newTime = now();
            long timeTaken = newTime - time;
            time = newTime;
            final long waitTime = ANIMATION_DELAY - timeTaken;
            if (waitTime > 0) {
                delay(waitTime, "Animation interrupted.");
                dropFrames = 0;
            } else {
                dropFrames = timeTaken / ANIMATION_DELAY - 1;
            }
        } else {
            dropFrames--;
        }
    }
    gui.refresh();
}
 
Example 9
Source File: AquaThumbnailLabelUI.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
protected int getViewWidth(JLabel label) {
	return label.getWidth() - 8;
}
 
Example 10
Source File: ThumbnailLabelUI.java    From pumpernickel with MIT License 4 votes vote down vote up
protected int getViewWidth(JLabel label) {
	return label.getWidth();
}