Java Code Examples for java.awt.FontMetrics#getMaxDescent()
The following examples show how to use
java.awt.FontMetrics#getMaxDescent() .
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: LightweightEventTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Override public Dimension getMinimumSize() { Dimension minSize = new Dimension(); if (superIsButton) { minSize = super.getMinimumSize(); } else { Graphics g = getGraphics(); FontMetrics metrics = g.getFontMetrics(); minSize.width = metrics.stringWidth(labelString) + 14; minSize.height = metrics.getMaxAscent() + metrics.getMaxDescent() + 9; g.dispose(); g = null; } return minSize; }
Example 2
Source File: XpGlobesOverlay.java From plugins with GNU General Public License v3.0 | 6 votes |
private void drawProgressLabel(Graphics2D graphics, XpGlobe globe, int startXp, int goalXp, int x, int y) { if (goalXp <= globe.getCurrentXp()) { return; } // Convert to int just to limit the decimal cases String progress = (int) (getSkillProgress(startXp, globe.getCurrentXp(), goalXp)) + "%"; final FontMetrics metrics = graphics.getFontMetrics(); int drawX = x + (config.xpOrbSize() / 2) - (metrics.stringWidth(progress) / 2); int drawY = y + (config.xpOrbSize() / 2) + (metrics.getHeight() / 2) - metrics.getMaxDescent(); OverlayUtil.renderTextLocation(graphics, new Point(drawX, drawY), progress, Color.WHITE); }
Example 3
Source File: ByteViewerLayoutModel.java From ghidra with Apache License 2.0 | 5 votes |
@Override public Layout getLayout(BigInteger index) { if (index.compareTo(numIndexes) >= 0) { return null; } List<Field> fields = new ArrayList<Field>(8); for (FieldFactory factory : factorys) { Field field = factory.getField(index); if (field != null) { fields.add(field); } } if (fields.size() == 0) { if (factorys.length > 0) { FontMetrics fm = factorys[0].getMetrics(); int height = fm.getMaxAscent() + fm.getMaxDescent(); fields.add( new EmptyTextField(height, factorys[0].getStartX(), 0, factorys[0].getWidth())); } else { fields.add(new EmptyTextField(20, 0, 0, 10)); } } Field[] fieldArray = new Field[fields.size()]; fields.toArray(fieldArray); return new SingleRowLayout(fieldArray); }
Example 4
Source File: LightweightEventTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void paint(Graphics g) { super.paint(g); Rectangle bounds = getBounds(); if (superIsButton) { return; } Dimension size = getSize(); Color oldColor = g.getColor(); // draw border g.setColor(getBackground()); g.fill3DRect(0, 0, size.width, size.height, false); g.fill3DRect(3, 3, size.width - 6, size.height - 6, true); // draw text FontMetrics metrics = g.getFontMetrics(); int centerX = size.width / 2; int centerY = size.height / 2; int textX = centerX - (metrics.stringWidth(labelString) / 2); int textY = centerY + ((metrics.getMaxAscent() + metrics.getMaxDescent()) / 2); g.setColor(getForeground()); g.drawString(labelString, textX, textY); g.setColor(oldColor); }
Example 5
Source File: SwingTerminal.java From jexer with MIT License | 5 votes |
/** * Figure out my font dimensions. This code path is needed to lazy-load * the information inside paint(). * * @param gr Graphics object to use */ private void getFontDimensions(final Graphics gr) { swing.setFont(font); FontMetrics fm = gr.getFontMetrics(); maxDescent = fm.getMaxDescent(); Rectangle2D bounds = fm.getMaxCharBounds(gr); int leading = fm.getLeading(); textWidth = (int)Math.round(bounds.getWidth()); // textHeight = (int)Math.round(bounds.getHeight()) - maxDescent; // This produces the same number, but works better for ugly // monospace. textHeight = fm.getMaxAscent() + maxDescent - leading; if (gotTerminus == true) { textHeight++; } if (getFontAdjustments() == false) { // We were unable to programmatically determine textAdjustX and // textAdjustY, so try some guesses based on VM vendor. String runtime = System.getProperty("java.runtime.name"); if ((runtime != null) && (runtime.contains("Java(TM)"))) { textAdjustY = -1; textAdjustX = 0; } } if (sessionInfo != null) { sessionInfo.setTextCellDimensions(textWidth, textHeight); } gotFontDimensions = true; }
Example 6
Source File: PMSimpleLabel.java From megamek with GNU General Public License v2.0 | 5 votes |
public PMSimpleLabel(String s, FontMetrics fm, Color c) { string = s; this.fm = fm; width = fm.stringWidth(string); height = fm.getHeight(); descent = fm.getMaxDescent(); color = c; }
Example 7
Source File: HtmlLabelUI.java From netbeans with Apache License 2.0 | 4 votes |
/** Actually paint the icon and text using our own html rendering engine. */ private void paintIconAndText(Graphics g, HtmlRendererImpl r) { Font f = font(r); g.setFont(f); FontMetrics fm = g.getFontMetrics(); //Find out what height we need int txtH = fm.getMaxAscent() + fm.getMaxDescent(); Insets ins = r.getInsets(); //find out the available height less the insets int rHeight = r.getHeight(); int availH = rHeight - (ins.top + ins.bottom); int txtY; if (availH >= txtH) { //Center the text if we have space txtY = (txtH + ins.top + ((availH / 2) - (txtH / 2))) - fm.getMaxDescent(); } else if (r.getHeight() > txtH) { txtY = txtH + (rHeight - txtH) / 2 - fm.getMaxDescent(); } else { //Okay, it's not going to fit, punt. txtY = fm.getMaxAscent(); } int txtX = r.getIndent(); Icon icon = r.getIcon(); //Check the icon non-null and height (see TabData.NO_ICON for why) if ((icon != null) && (icon.getIconWidth() > 0) && (icon.getIconHeight() > 0)) { int iconY; if (availH > icon.getIconHeight()) { //add 2 to make sure icon top pixels are not cut off by outline iconY = ins.top + ((availH / 2) - (icon.getIconHeight() / 2)); // + 2; } else if (availH == icon.getIconHeight()) { //They're an exact match, make it 0 iconY = ins.top; } else { //Won't fit; make the top visible and cut the rest off (option: //center it and clip it on top and bottom - probably even harder //to recognize that way, though) iconY = ins.top; } //add in the insets int iconX = ins.left + r.getIndent() + 1; //+1 to get it out of the way of the focus border try { //Diagnostic - the CPP module currently is constructing //some ImageIcon from a null image in Options. So, catch it and at //least give a meaningful message that indicates what node //is the culprit icon.paintIcon(r, g, iconX, iconY); } catch (NullPointerException npe) { Exceptions.attachMessage(npe, "Probably an ImageIcon with a null source image: " + icon + " - " + r.getText()); //NOI18N Exceptions.printStackTrace(npe); } txtX = iconX + icon.getIconWidth() + r.getIconTextGap(); } else { //If there's no icon, paint the text where the icon would start txtX += ins.left; } String text = r.getText(); if (text == null) { //No text, we're done return; } //Get the available horizontal pixels for text int txtW = (icon != null) ? (r.getWidth() - (ins.left + ins.right + icon.getIconWidth() + r.getIconTextGap() + r.getIndent())) : (r.getWidth() - (ins.left + ins.right + r.getIndent())); Color background = getBackgroundFor(r); Color foreground = ensureContrastingColor(getForegroundFor(r), background); if (r.isHtml()) { HtmlRenderer._renderHTML(text, 0, g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true, background, r.isSelected()); } else { HtmlRenderer.renderPlainString(text, g, txtX, txtY, txtW, txtH, f, foreground, r.getRenderStyle(), true); } }
Example 8
Source File: FontPanel.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 9
Source File: FontPanel.java From hottub with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 10
Source File: FontPanel.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 11
Source File: FontPanel.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 12
Source File: FontPanel.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 13
Source File: FontPanel.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 14
Source File: HtmlLabelUI.java From netbeans with Apache License 2.0 | 4 votes |
private Dimension calcPreferredSize(HtmlRendererImpl r) { Insets ins = r.getInsets(); Dimension prefSize = new java.awt.Dimension(ins.left + ins.right, ins.top + ins.bottom); String text = r.getText(); Graphics g = r.getGraphics(); Icon icon = r.getIcon(); Font font = font(r); if (text != null) { FontMetrics fm = g.getFontMetrics(font); prefSize.height += (fm.getMaxAscent() + fm.getMaxDescent()); } if (icon != null) { if (r.isCentered()) { prefSize.height += (icon.getIconHeight() + r.getIconTextGap()); prefSize.width += icon.getIconWidth(); } else { prefSize.height = Math.max(icon.getIconHeight() + ins.top + ins.bottom, prefSize.height); prefSize.width += (icon.getIconWidth() + r.getIconTextGap()); } } //Antialiasing affects the text metrics, so use it if needed when //calculating preferred size or the result here will be narrower //than the space actually needed ((Graphics2D) g).addRenderingHints(getHints()); int textwidth = textWidth(text, g, font, r.isHtml()) + 4; if (r.isCentered()) { prefSize.width = Math.max(prefSize.width, textwidth + ins.right + ins.left); } else { prefSize.width += (textwidth + r.getIndent()); } if (FIXED_HEIGHT > 0) { prefSize.height = FIXED_HEIGHT; } return prefSize; }
Example 15
Source File: FontPanel.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 16
Source File: FontPanel.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 17
Source File: FontPanel.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 18
Source File: FontPanel.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
private void calcFontMetrics( Graphics2D g2d, int w, int h ) { FontMetrics fm; Graphics2D g2 = (Graphics2D)g2d.create(); /// ABP if ( g2Transform != NONE && textToUse != FILE_TEXT ) { g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) ); fm = g2.getFontMetrics(); } else { fm = g2.getFontMetrics(); } maxAscent = fm.getMaxAscent(); maxDescent = fm.getMaxDescent(); if (maxAscent == 0) maxAscent = 10; if (maxDescent == 0) maxDescent = 5; if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) { /// Give slight extra room for each character maxAscent += 3; maxDescent += 3; gridWidth = fm.getMaxAdvance() + 6; gridHeight = maxAscent + maxDescent; if ( force16Cols ) numCharAcross = 16; else numCharAcross = ( w - 10 ) / gridWidth; numCharDown = ( h - 10 ) / gridHeight; canvasInset_X = ( w - numCharAcross * gridWidth ) / 2; canvasInset_Y = ( h - numCharDown * gridHeight ) / 2; if ( numCharDown == 0 || numCharAcross == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); if ( !isPrinting ) resetScrollbar( verticalBar.getValue() * numCharAcross ); } else { maxDescent += fm.getLeading(); canvasInset_X = 5; canvasInset_Y = 5; /// gridWidth and numCharAcross will not be used in this mode... gridHeight = maxAscent + maxDescent; numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight; if ( numCharDown == 0 ) throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW ); /// If this is text loaded from file, prepares the LineBreak'ed /// text layout at this point if ( textToUse == FILE_TEXT ) { if ( !isPrinting ) f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false ); lineBreakTLs = new Vector(); for ( int i = 0; i < fileText.length; i++ ) { AttributedString as = new AttributedString( fileText[i], g2.getFont().getAttributes() ); LineBreakMeasurer lbm = new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() ); while ( lbm.getPosition() < fileText[i].length() ) lineBreakTLs.add( lbm.nextLayout( (float) w )); } } if ( !isPrinting ) resetScrollbar( verticalBar.getValue() ); } }
Example 19
Source File: LightButton.java From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ButtonModel m = getModel(); Insets insets = getInsets(); int width = getWidth() - insets.left - insets.right; int height = getHeight() - insets.top - insets.bottom; tileStretchPaint(g2,this,(BufferedImage) getImage(m.isArmed()), sourceInsets); if (getModel().isRollover()) { g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(buttonHighlight, insets.left + 2, insets.top + 2, width - 4, height - 4, null); } FontMetrics fm = getFontMetrics(getFont()); TextLayout layout = new TextLayout(getText(), getFont(), g2.getFontRenderContext()); Rectangle2D bounds = layout.getBounds(); int x = (int) (getWidth() - insets.left - insets.right - bounds.getWidth()) / 2; //x -= 2; int y = (getHeight() - insets.top - insets.bottom - fm.getMaxAscent() - fm.getMaxDescent()) / 2; y += fm.getAscent() - 1; if (m.isArmed()) { x += 1; y += 1; } g2.setColor(shadowColor); layout.draw(g2, x + (int) Math.ceil(shadowOffsetX), y + (int) Math.ceil(shadowOffsetY)); g2.setColor(getForeground()); layout.draw(g2, x, y); }
Example 20
Source File: JEditTextArea.java From rapidminer-studio with GNU Affero General Public License v3.0 | 2 votes |
/** * Converts a line index to a y co-ordinate. * * @param line * The line */ public int lineToY(int line) { FontMetrics fm = painter.getFontMetrics(); return (line - firstLine) * fm.getHeight() - (fm.getLeading() + fm.getMaxDescent()); }