当前位置: 首页>>代码示例>>Java>>正文


Java Font.stringWidth方法代码示例

本文整理汇总了Java中javax.microedition.lcdui.Font.stringWidth方法的典型用法代码示例。如果您正苦于以下问题:Java Font.stringWidth方法的具体用法?Java Font.stringWidth怎么用?Java Font.stringWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.microedition.lcdui.Font的用法示例。


在下文中一共展示了Font.stringWidth方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: truncateText

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
/**
 * Returns the given text truncated to fit the specified width. 
 * If text needs to be truncated then three dots are appended to the end.
 * 
 * @param original the original text.
 * @param width the width in pixels.
 * @param font the font used in drawing the text.
 * @return the truncated text or the original if no truncation is needed.
 * 
 * @throws NullPointerException if <code>text</code> or <code>font</code> is <code>null</code>.
 */
public static final String truncateText(String original, int width, Font font) {
    int textWidth = font.stringWidth(original);
    
    if (textWidth > width) {
        // Text needs to be truncated.
        final int indicatorWidth = font.stringWidth(TRUNCATION_INDICATOR);
        final int truncateToWidth = width - indicatorWidth;
        
        if (indicatorWidth >= truncateToWidth) {
            // Unlikely situation normally but there isn't enough space
            // for even the indicator.
            return "";
        }
        
        int len = 0;
        
        // Find out how many chars can be added before exceeding the reserved width:
        while (font.substringWidth(original, 0, ++len) <= truncateToWidth) {
        }

        len--;
        StringBuffer sb = new StringBuffer(len + TRUNCATION_INDICATOR.length());    
        sb.append(original.substring(0, len));
        sb.append(TRUNCATION_INDICATOR);
        
        return sb.toString();
    } else {
        return original;
    }
}
 
开发者ID:mozilla,项目名称:pluotsorbet,代码行数:42,代码来源:Util.java

示例2: stringWidth

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
public int stringWidth(String str) {
	Font f = graphics.getFont();
	return f.stringWidth(str);
}
 
开发者ID:NeiroNext,项目名称:JccAPV,代码行数:5,代码来源:Graphics.java

示例3: drawDateBox

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
private void drawDateBox(int xPos, int yPos)
{
    int prevColor = gObj.getColor();
    Font font = Font.getDefaultFont();
    int boxWidth = font.stringWidth("30") + 5;
    int boxHeight = font.getHeight() - 2;

    String monthStr = (new Integer(currMonth+1)).toString() + " ";
    String dayStr = (new Integer(currDay)).toString() + " ";
    String yearStr = (new Integer(currYear)).toString();

    //Clear the previous rectangle on the date field
    if(prevSelectionMode != -1)
    {
        gObj.setColor(0x00FFFFFF);
        if(prevSelectionMode == MONTH)
        {
            boxWidth = font.stringWidth(monthStr);
            gObj.fillRect(xPos, yPos, boxWidth, boxHeight);
        }
        else if(prevSelectionMode == DAY)
        {
            boxWidth = font.stringWidth(dayStr);
            gObj.fillRect(xPos + font.stringWidth(monthStr), yPos, boxWidth, boxHeight);
        }
        else if(prevSelectionMode == YEAR)
        {
            boxWidth = font.stringWidth(yearStr);
            gObj.fillRect(xPos + font.stringWidth(monthStr) + font.stringWidth(dayStr), yPos, boxWidth, boxHeight);
        }
        else if(prevSelectionMode == NONE)
        {
            boxWidth = font.stringWidth(NO_DATE_STR) + 5;
            gObj.fillRect(xPos + font.stringWidth(monthStr) + font.stringWidth(dayStr) + font.stringWidth(yearStr) + font.stringWidth("  "), yPos, boxWidth, boxHeight);
        }
    }
    gObj.setColor(0x0000FF0A);
    if(selectionMode == MONTH)
    {
        boxWidth = font.stringWidth(monthStr);
        gObj.fillRect(xPos, yPos, boxWidth, boxHeight);
    }
    else if(selectionMode == DAY)
    {
        boxWidth = font.stringWidth(dayStr);
        gObj.fillRect(xPos + font.stringWidth(monthStr), yPos, boxWidth, boxHeight);
    }
    else if(selectionMode == YEAR)
    {
        boxWidth = font.stringWidth(yearStr);
        gObj.fillRect(xPos + font.stringWidth(monthStr) + font.stringWidth(dayStr), yPos, boxWidth, boxHeight);
    }
    else if(selectionMode == NONE)
    {
        boxWidth = font.stringWidth(NO_DATE_STR);
        gObj.fillRect(xPos + font.stringWidth(monthStr) + font.stringWidth(dayStr) + font.stringWidth(yearStr) + font.stringWidth("  ") , yPos, boxWidth, boxHeight);
    }
    gObj.setColor(prevColor);
}
 
开发者ID:dimagi,项目名称:commcare-j2me,代码行数:60,代码来源:SimpleDateField.java

示例4: paintContent

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
        if(player != null) {
            //center stuff
            width = rightBorder - leftBorder;
            int offsetX = (width - cw) / 2;
            vc.setDisplayLocation(x + offsetX, y);
            if(!playing) {
                vc.setVisible(false);

                Font f = g.getFont();
                int fnth = f.getHeight();


                //Calculate margins and locations
                int mx = x + offsetX;
                int my = y;

                int mw = vc.getDisplayWidth();
                int mh = vc.getDisplayHeight();


                int hi = Math.max((int)Math.floor(.2 * mh), fnth);
                int fh = mh - hi * 2;

                //int fw = mw  - wi * 2;
                //int wi = (int)Math.floor(.2 * mw);
                int fw = (int)Math.floor(.9 * fh);

                int wi = (mw - fw)/2;


                int wu = (int)Math.floor(fw / 5.0);
                int hu = (int)Math.floor(fh / 7.0);

                if(hashKeyImage == null) {
                    hashKeyImage = ImageUtils.getImage(imageLocation);
                    if(hashKeyImage != null){

                        //scale
                        int[] newDimension = ImageUtils.getNewDimensions(hashKeyImage, fw,fh);

                        if(newDimension[0] != height || newDimension[1] != width) {
                            hashKeyImage = ImageUtils.resizeImage(hashKeyImage, newDimension[1], newDimension[0]);
                        }
                    }
                }

                if(hashKeyImage != null) {
                    g.drawImage(hashKeyImage, mx + wi + fw / 2, my + hi + fh / 2, Graphics.HCENTER  | Graphics.VCENTER);
                } else {

                    //Draw us a big 'ol hash
                    g.setColor(0, 0, 0);

                    g.fillRect(mx + wi + wu, my + hi, wu, fh);
                    g.fillRect(mx + wi + 3 * wu, my + hi, wu, fh);

                    g.fillRect(mx + wi, my + hi + 2 * hu, fw, hu);
                    g.fillRect(mx + wi, my + hi + 4 * hu, fw, hu);
                }

                int tw = f.stringWidth(top);
                int bw = f.stringWidth(bottom);

                int tx = (mw - tw)/2 + mx;
                int tyo = (hi - fnth) /2;
                int ty = my + tyo;


                g.drawString(top, tx, ty, Graphics.TOP | Graphics.LEFT);

                int bx = (mw - bw)/2 + mx;
                int by = (my + mh - hi) + tyo;

                g.drawString(bottom, bx, by, Graphics.TOP | Graphics.LEFT);
            }
        }
}
 
开发者ID:dimagi,项目名称:commcare-j2me,代码行数:79,代码来源:VideoItem.java

示例5: wrapTextToWidth

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
/**
 * Wrap text to a given width with a specified font.
 *
 * @param text Text to be wrapped
 * @param wrapWidth Max width of one line in pixels
 * @param font Font to be used in calculating 
 * @return
 */
public static Vector wrapTextToWidth(String text, int wrapWidth, Font font) {
    if (wrapWidth < 20) {
        wrapWidth = 240;
    }

    Vector lines = new Vector();

    int start = 0;
    int position = 0;
    int length = text.length();
    while (position < length - 1) {
        start = position;
        int lastBreak = -1;
        int i = position;
        for (; i < length && font.stringWidth(text.substring(position, i))
            <= wrapWidth; i++) {
            if (text.charAt(i) == ' ') {
                lastBreak = i;
            }
            else if (text.charAt(i) == '\n') {
                lastBreak = i;
                break;
            }
        }
        if (i == length) {
            position = i;
        }
        else if (lastBreak <= position) {
            position = i;
        }
        else {
            position = lastBreak;
        }

        lines.addElement(text.substring(start, position));

        if (position == lastBreak) {
            position++;
        }
    }

    return lines;
}
 
开发者ID:mozilla,项目名称:pluotsorbet,代码行数:52,代码来源:TextWrapper.java

示例6: paint

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
public void paint(Graphics g) {
	int width = getWidth();
       int height = getHeight();

	g.setGrayScale(255);
	g.fillRect(0, 0, width, height);

	g.setColor(0x5691F0);
	g.drawRect(0, 0, width - 1, height - 1);

	g.setGrayScale(0);
	g.drawRect(2, 2, width - 5, height - 5);

	int pos = posX;
	while (pos < width - 5) {
		g.drawLine(3 + pos, 3, 3 + pos, height - 4);
		pos += POSNUMBER;
	}
	pos = posY;
	while (pos < height - 5) {
		g.drawLine(3, 3 + pos, width - 4, 3 + pos);
		pos += POSNUMBER;
	}

	// Paint canvas info in the middle
	String text = width + " x " + height;

	Font f = g.getFont();
	int w = f.stringWidth(text) + 4;
	int h = 2 * f.getHeight() + 4;

	int arcWidth = w;
	int arcHeight = h;
	g.setColor(0xFFCC11);
	g.drawRoundRect((width - w)/2, (height - h)/2, w, h, arcWidth, arcHeight);
	g.setColor(0xFFEE99);
	g.fillRoundRect((width - w)/2, (height - h)/2, w, h, arcWidth, arcHeight);

	g.setColor(0xBB5500);
	g.drawString(text, width/2, (height - f.getHeight())/2, Graphics.HCENTER | Graphics.TOP);

	// Pint Ball
	g.setColor(ballColor);
	g.fillRoundRect(ballPosX - 4, ballPosY - 4, 8, 8, 8, 8);

	ballPosX += ballMoveX;
	ballPosY += ballMoveY;

	boolean changeColor = false;
	if ((ballPosX < 4) || (ballPosX > width - 4)) {
		ballMoveX = -ballMoveX;
		changeColor = true;
	}
	if ((ballPosY < 4) || (ballPosY > height - 4)) {
		ballMoveY = -ballMoveY;
		changeColor = true;
	}
	if (changeColor) {
		ballColor = ballRandom.nextInt(0xFF) + (ballRandom.nextInt(0xFF) << 8) + (ballRandom.nextInt(0xFF) << 16);
	}
}
 
开发者ID:freeVM,项目名称:freeVM,代码行数:62,代码来源:CanvasPanel.java

示例7: paintContent

import javax.microedition.lcdui.Font; //导入方法依赖的package包/类
protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
        if(player != null) {
            //center stuff
            width = rightBorder - leftBorder;
            int offsetX = (width - cw) / 2;
            vc.setDisplayLocation(x + offsetX, y);
            if(!playing) {
                vc.setVisible(false);

                Font f = g.getFont();
                int fnth = f.getHeight();


                //Calculate margins and locations
                int mx = x + offsetX;
                int my = y;

                int mw = vc.getDisplayWidth();
                int mh = vc.getDisplayHeight();


                int hi = Math.max((int)Math.floor(.2 * mh), fnth);
                int fh = mh - hi * 2;

                //int fw = mw  - wi * 2;
                //int wi = (int)Math.floor(.2 * mw);
                int fw = (int)Math.floor(.9 * fh);

                int wi = (mw - fw)/2;


                int wu = (int)Math.floor(fw / 5.0);
                int hu = (int)Math.floor(fh / 7.0);

                //Draw us a big 'ol hash
                g.setColor(0, 0, 0);

                g.fillRect(mx + wi + wu, my + hi, wu, fh);
                g.fillRect(mx + wi + 3 * wu, my + hi, wu, fh);

                g.fillRect(mx + wi, my + hi + 2 * hu, fw, hu);
                g.fillRect(mx + wi, my + hi + 4 * hu, fw, hu);


                String top = Localization.get("video.playback.top");
                //String top = "lw: " + vc.getSourceWidth() + " lh: " + vc.getSourceHeight();
                String bottom = Localization.get("video.playback.bottom");
                //String bottom = "cw: " + cw + " ch: " + ch;


                int tw = f.stringWidth(top);
                int bw = f.stringWidth(bottom);

                int tx = (mw - tw)/2 + mx;
                int tyo = (hi - fnth) /2;
                int ty = my + tyo;


                g.drawString(top, tx, ty, Graphics.TOP | Graphics.LEFT);

                int bx = (mw - bw)/2 + mx;
                int by = (my + mh - hi) + tyo;

                g.drawString(bottom, bx, by, Graphics.TOP | Graphics.LEFT);
            }
        }
}
 
开发者ID:dimagi,项目名称:commcare-j2me,代码行数:68,代码来源:CameraItem.java


注:本文中的javax.microedition.lcdui.Font.stringWidth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。