Java Code Examples for java.awt.Font#getStringBounds()
The following examples show how to use
java.awt.Font#getStringBounds() .
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: ProgressAnimation.java From rapidminer-studio with GNU Affero General Public License v3.0 | 6 votes |
/** * Finds the font that fits into the progress arc. Stores the associated height and width. */ private static void initializeFontMeasures() { platformSpecificFont = DEFAULT_FONT; Graphics2D testGraphics = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB).createGraphics(); float[] possibleFontSizes = { 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10 }; for (float size : possibleFontSizes) { Font deriveFont = DEFAULT_FONT.deriveFont(size); testGraphics.setFont(deriveFont); String testString = "54"; Rectangle2D stringBounds = deriveFont.getStringBounds(testString, testGraphics.getFontRenderContext()); if (stringBounds.getWidth() <= MAXIMAL_TEXT_WIDTH) { platformSpecificFont = deriveFont; textWidth = stringBounds.getWidth(); textHeight = deriveFont.createGlyphVector(testGraphics.getFontRenderContext(), testString).getVisualBounds() .getHeight(); break; } } }
Example 2
Source File: StyledSpaceAdvance.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 3
Source File: StyledSpaceAdvance.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 4
Source File: StyledSpaceAdvance.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 5
Source File: StyledSpaceAdvance.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 6
Source File: StyledSpaceAdvance.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 7
Source File: SVGUtils.java From gsn with GNU General Public License v3.0 | 6 votes |
public static Width_Height_Decendent_ValueBean getBoundsOfAString ( String s , Font f ) { int width = 0; s = s.replace( "\t" , " " ); StringTokenizer stringTokenizer = new StringTokenizer( s , "\n" , false ); FontRenderContext context = new FontRenderContext( null , false , false ); String [ ] tokens = new String [ stringTokenizer.countTokens( ) ]; int [ ] heights = new int [ stringTokenizer.countTokens( ) ]; int [ ] decendents = new int [ stringTokenizer.countTokens( ) ]; int i = 0; while ( stringTokenizer.hasMoreElements( ) ) { String nextLine = stringTokenizer.nextToken( ).trim( ); // TextLayout textLayout = new TextLayout ( nextLine, f, context // ); Rectangle2D temp = f.getStringBounds( nextLine , context ); // Rectangle2D temp = f.createGlyphVector ( context, nextLine // ).getLogicalBounds (); decendents[ i ] = ( int ) f.getLineMetrics( s , context ).getDescent( ); width = Math.max( width , ( int ) temp.getWidth( ) ); heights[ i ] = ( int ) temp.getHeight( ); tokens[ i++ ] = nextLine; } return new Width_Height_Decendent_ValueBean( tokens , width , heights , decendents ); }
Example 8
Source File: WPathGraphics.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private boolean okGDIMetrics(String str, Font font, FontRenderContext frc, double scaleX) { Rectangle2D bds = font.getStringBounds(str, frc); double jdkAdvance = bds.getWidth(); jdkAdvance = Math.round(jdkAdvance*scaleX); int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str); if (jdkAdvance > 0 && gdiAdvance > 0) { double diff = Math.abs(gdiAdvance-jdkAdvance); double ratio = gdiAdvance/jdkAdvance; if (ratio < 1) { ratio = 1/ratio; } return diff <= 1 || ratio < 1.002; } return true; }
Example 9
Source File: StyledSpaceAdvance.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 10
Source File: WPathGraphics.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private boolean okGDIMetrics(String str, Font font, FontRenderContext frc, double scaleX) { Rectangle2D bds = font.getStringBounds(str, frc); double jdkAdvance = bds.getWidth(); jdkAdvance = Math.round(jdkAdvance*scaleX); int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str); if (jdkAdvance > 0 && gdiAdvance > 0) { double diff = Math.abs(gdiAdvance-jdkAdvance); double ratio = gdiAdvance/jdkAdvance; if (ratio < 1) { ratio = 1/ratio; } return diff <= 1 || ratio < 1.002; } return true; }
Example 11
Source File: StyledSpaceAdvance.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 12
Source File: WPathGraphics.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private boolean okGDIMetrics(String str, Font font, FontRenderContext frc, double scaleX) { Rectangle2D bds = font.getStringBounds(str, frc); double jdkAdvance = bds.getWidth(); jdkAdvance = Math.round(jdkAdvance*scaleX); int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str); if (jdkAdvance > 0 && gdiAdvance > 0) { double diff = Math.abs(gdiAdvance-jdkAdvance); double ratio = gdiAdvance/jdkAdvance; if (ratio < 1) { ratio = 1/ratio; } return diff <= 1 || ratio < 1.01; } return true; }
Example 13
Source File: StyledSpaceAdvance.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
static void test(int sz) { Font reg = new Font(name, Font.PLAIN, sz); Font bold = new Font(name, Font.BOLD, sz); //System.out.println("reg="+reg); //System.out.println("bold="+bold); FontRenderContext frc = new FontRenderContext(null, false, false); if (reg.getFontName(Locale.ENGLISH).equals(name) && bold.getFontName(Locale.ENGLISH).equals(name)) { Rectangle2D rb = reg.getStringBounds(" ", frc); Rectangle2D bb = bold.getStringBounds(" ", frc); if (bb.getWidth() > rb.getWidth() + 1.01f) { System.err.println("reg="+reg+" bds = " + rb); System.err.println("bold="+bold+" bds = " + bb); throw new RuntimeException("Advance difference too great."); } } else { System.out.println("Skipping test because fonts aren't as expected"); } }
Example 14
Source File: AllOpenController.java From JavaWeb with Apache License 2.0 | 5 votes |
private String drawImg(ByteArrayOutputStream output){ //final int verifyCodeLength = 10; final int verifyCodeLength = 4; String code = getVerifyCode(verifyCodeLength); //int width = 125; int width = 70; int height = 25; BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); Font font = new Font("Times New Roman",Font.PLAIN,20); Graphics2D g = bi.createGraphics(); g.setFont(font); g.setColor(new Color(66,2,82)); g.setBackground(new Color(226,226,240)); g.clearRect(0, 0, width, height); FontRenderContext context = g.getFontRenderContext(); Rectangle2D bounds = font.getStringBounds(code, context); double x = (width - bounds.getWidth()) / 2; double y = (height - bounds.getHeight()) / 2; double ascent = bounds.getY(); double baseY = y - ascent; g.drawString(code, (int)x, (int)baseY); g.dispose(); try { ImageIO.write(bi, "jpg", output); } catch (IOException e) { //do nothing } return code; }
Example 15
Source File: MemoryStatus.java From nextreports-designer with Apache License 2.0 | 5 votes |
public MemoryStatus() { // Font font = new JLabel().getFont(); Font font = UIManager.getFont("Label.font"); MemoryStatus.this.setFont(font); FontRenderContext fontRendererContext = new FontRenderContext(null, false, false); Rectangle2D bounds = font.getStringBounds(MEMORY_TEST_STRING, fontRendererContext); Dimension dimension = new Dimension((int) bounds.getWidth(), (int) bounds.getHeight()); setPreferredSize(dimension); setMaximumSize(dimension); lineMetrics = font.getLineMetrics(MEMORY_TEST_STRING, fontRendererContext); addMouseListener(new MouseHandler()); }
Example 16
Source File: FontCache.java From gama with GNU General Public License v3.0 | 4 votes |
@Override public Rectangle2D getBounds(final CharSequence str, final Font font, final FontRenderContext frc) { return font.getStringBounds(str.toString(), frc); }
Example 17
Source File: TextServiceImpl.java From geomajas-project-server with GNU Affero General Public License v3.0 | 4 votes |
@Override public Rectangle2D getStringBounds(String text, FontStyleInfo fontStyle) { Font font = getFont(fontStyle); return font.getStringBounds(text, 0, text.length(), new FontRenderContext(new AffineTransform(), true, true)); }
Example 18
Source File: StyledFontLayoutTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private static void runTest() { im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = im.createGraphics(); g2d.setColor(Color.white); g2d.fillRect(0, 0, W, H); g2d.setColor(Color.black); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); char[] chs = "Sample Text.".toCharArray(); int len = chs.length; int x = 50, y = 100; FontRenderContext frc = g2d.getFontRenderContext(); Font plain = new Font("Serif", Font.PLAIN, 48); GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0); g2d.setFont(plain); g2d.drawChars(chs, 0, len, x, y); y +=50; g2d.drawGlyphVector(pgv, x, y); y += 50; Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc); Rectangle2D plainGVBounds = pgv.getLogicalBounds(); Font bold = new Font("Serif", Font.BOLD, 48); GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0); Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc); Rectangle2D boldGVBounds = bgv.getLogicalBounds(); g2d.setFont(bold); g2d.drawChars(chs, 0, len, x, y); y +=50; g2d.drawGlyphVector(bgv, x, y); System.out.println("Plain String Bounds = " + plainStrBounds); System.out.println("Bold String Bounds = " + boldStrBounds); System.out.println("Plain GlyphVector Bounds = " + plainGVBounds); System.out.println("Bold GlyphVector Bounds = " + boldGVBounds); if (!plainStrBounds.equals(boldStrBounds) && plainGVBounds.equals(boldGVBounds)) { System.out.println("Test failed: Plain GV bounds same as Bold"); if (!interactive) { throw new RuntimeException("Plain GV bounds same as Bold"); } } }
Example 19
Source File: StyledFontLayoutTest.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
private static void runTest() { im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = im.createGraphics(); g2d.setColor(Color.white); g2d.fillRect(0, 0, W, H); g2d.setColor(Color.black); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); char[] chs = "Sample Text.".toCharArray(); int len = chs.length; int x = 50, y = 100; FontRenderContext frc = g2d.getFontRenderContext(); Font plain = new Font("Serif", Font.PLAIN, 48); GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0); g2d.setFont(plain); g2d.drawChars(chs, 0, len, x, y); y +=50; g2d.drawGlyphVector(pgv, x, y); y += 50; Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc); Rectangle2D plainGVBounds = pgv.getLogicalBounds(); Font bold = new Font("Serif", Font.BOLD, 48); GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0); Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc); Rectangle2D boldGVBounds = bgv.getLogicalBounds(); g2d.setFont(bold); g2d.drawChars(chs, 0, len, x, y); y +=50; g2d.drawGlyphVector(bgv, x, y); System.out.println("Plain String Bounds = " + plainStrBounds); System.out.println("Bold String Bounds = " + boldStrBounds); System.out.println("Plain GlyphVector Bounds = " + plainGVBounds); System.out.println("Bold GlyphVector Bounds = " + boldGVBounds); if (!plainStrBounds.equals(boldStrBounds) && plainGVBounds.equals(boldGVBounds)) { System.out.println("Test failed: Plain GV bounds same as Bold"); if (!interactive) { throw new RuntimeException("Plain GV bounds same as Bold"); } } }
Example 20
Source File: FontCache.java From gama with GNU General Public License v3.0 | 4 votes |
@Override public Rectangle2D getBounds(final String str, final Font font, final FontRenderContext frc) { return font.getStringBounds(str, frc); }