📄 basicgraphicsutils.java
字号:
* @param g the graphics into which the rectangle is drawn. * @param x the x coordinate of the rectangle. * @param y the y coordinate of the rectangle. * @param width the width of the rectangle in pixels. * @param height the height of the rectangle in pixels. * * @param shadow the color that will be used for painting * the inner side of the top and left edges. * * @param darkShadow the color that will be used for painting * the outer side of the top and left edges. * * @param highlight the color that will be used for painting * the inner side of the bottom and right edges. * * @param lightHighlight the color that will be used for painting * the outer side of the bottom and right edges. */ public static void drawLoweredBezel(Graphics g, int x, int y, int width, int height, Color shadow, Color darkShadow, Color highlight, Color lightHighlight) { /* Like drawEtchedRect, but swapping darkShadow and shadow. * * To understand this, it might be helpful to look at the image * "BasicGraphicsUtils-4.png" that is included with the JavaDoc, * and to compare it with "BasicGraphicsUtils-1.png" which shows * the pixels painted by drawEtchedRect. These image files are * located in the "doc-files" subdirectory. */ drawEtchedRect(g, x, y, width, height, darkShadow, shadow, highlight, lightHighlight); } /** * Draws a String at the given location, underlining the first * occurence of a specified character. The algorithm for determining * the underlined position is not sensitive to case. If the * character is not part of <code>text</code>, the text will be * drawn without underlining. Drawing is performed in the current * color and font of <code>g</code>. * * <p><img src="doc-files/BasicGraphicsUtils-5.png" width="500" * height="100" alt="[An illustration showing how to use the * method]" /> * * @param g the graphics into which the String is drawn. * * @param text the String to draw. * * @param underlinedChar the character whose first occurence in * <code>text</code> will be underlined. It is not clear * why the API specification declares this argument to be * of type <code>int</code> instead of <code>char</code>. * While this would allow to pass Unicode characters outside * Basic Multilingual Plane 0 (U+0000 .. U+FFFE), at least * the GNU Classpath implementation does not underline * anything if <code>underlinedChar</code> is outside * the range of <code>char</code>. * * @param x the x coordinate of the text, as it would be passed to * {@link java.awt.Graphics#drawString(java.lang.String, * int, int)}. * * @param y the y coordinate of the text, as it would be passed to * {@link java.awt.Graphics#drawString(java.lang.String, * int, int)}. */ public static void drawString(Graphics g, String text, int underlinedChar, int x, int y) { int index = -1; /* It is intentional that lower case is used. In some languages, * the set of lowercase characters is larger than the set of * uppercase ones. Therefore, it is good practice to use lowercase * for such comparisons (which really means that the author of this * code can vaguely remember having read some Unicode techreport * with this recommendation, but is too lazy to look for the URL). */ if ((underlinedChar >= 0) || (underlinedChar <= 0xffff)) index = text.toLowerCase().indexOf( Character.toLowerCase((char) underlinedChar)); drawStringUnderlineCharAt(g, text, index, x, y); } /** * Draws a String at the given location, underlining the character * at the specified index. Drawing is performed in the current color * and font of <code>g</code>. * * <p><img src="doc-files/BasicGraphicsUtils-5.png" width="500" * height="100" alt="[An illustration showing how to use the * method]" /> * * @param g the graphics into which the String is drawn. * * @param text the String to draw. * * @param underlinedIndex the index of the underlined character in * <code>text</code>. If <code>underlinedIndex</code> falls * outside the range <code>[0, text.length() - 1]</code>, the * text will be drawn without underlining anything. * * @param x the x coordinate of the text, as it would be passed to * {@link java.awt.Graphics#drawString(java.lang.String, * int, int)}. * * @param y the y coordinate of the text, as it would be passed to * {@link java.awt.Graphics#drawString(java.lang.String, * int, int)}. * * @since 1.4 */ public static void drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y) { Graphics2D g2; Rectangle2D.Double underline; FontRenderContext frc; FontMetrics fmet; LineMetrics lineMetrics; Font font; TextLayout layout; double underlineX1, underlineX2; boolean drawUnderline; int textLength; textLength = text.length(); if (textLength == 0) return; drawUnderline = (underlinedIndex >= 0) && (underlinedIndex < textLength); if (!(g instanceof Graphics2D)) { /* Fall-back. This is likely to produce garbage for any text * containing right-to-left (Hebrew or Arabic) characters, even * if the underlined character is left-to-right. */ g.drawString(text, x, y); if (drawUnderline) { fmet = g.getFontMetrics(); g.fillRect( /* x */ x + fmet.stringWidth(text.substring(0, underlinedIndex)), /* y */ y + fmet.getDescent() - 1, /* width */ fmet.charWidth(text.charAt(underlinedIndex)), /* height */ 1); } return; } g2 = (Graphics2D) g; font = g2.getFont(); frc = g2.getFontRenderContext(); lineMetrics = font.getLineMetrics(text, frc); layout = new TextLayout(text, font, frc); /* Draw the text. */ layout.draw(g2, x, y); if (!drawUnderline) return; underlineX1 = x + layout.getLogicalHighlightShape( underlinedIndex, underlinedIndex).getBounds2D().getX(); underlineX2 = x + layout.getLogicalHighlightShape( underlinedIndex + 1, underlinedIndex + 1).getBounds2D().getX(); underline = new Rectangle2D.Double(); if (underlineX1 < underlineX2) { underline.x = underlineX1; underline.width = underlineX2 - underlineX1; } else { underline.x = underlineX2; underline.width = underlineX1 - underlineX2; } underline.height = lineMetrics.getUnderlineThickness(); underline.y = lineMetrics.getUnderlineOffset(); if (underline.y == 0) { /* Some fonts do not specify an underline offset, although they * actually should do so. In that case, the result of calling * lineMetrics.getUnderlineOffset() will be zero. Since it would * look very ugly if the underline was be positioned immediately * below the baseline, we check for this and move the underline * below the descent, as shown in the following ASCII picture: * * ##### ##### # * # # # # * # # # # * # # # # * ##### ###### ---- baseline (0) * # * # * ------------------###----------- lineMetrics.getDescent() */ underline.y = lineMetrics.getDescent(); } underline.y += y; g2.fill(underline); } /** * Draws a rectangle, simulating a dotted stroke by painting only * every second pixel along the one-pixel thick edge. The color of * those pixels is the current color of the Graphics <code>g</code>. * Any other pixels are left unchanged. * * <p><img src="doc-files/BasicGraphicsUtils-7.png" width="360" * height="200" alt="[An illustration that shows which pixels * get painted]" /> * * @param g the graphics into which the rectangle is drawn. * @param x the x coordinate of the rectangle. * @param y the y coordinate of the rectangle. * @param width the width of the rectangle in pixels. * @param height the height of the rectangle in pixels. */ public static void drawDashedRect(Graphics g, int x, int y, int width, int height) { int right = x + width - 1; int bottom = y + height - 1; /* Draw the top and bottom edge of the dotted rectangle. */ for (int i = x; i <= right; i += 2) { g.drawLine(i, y, i, y); g.drawLine(i, bottom, i, bottom); } /* Draw the left and right edge of the dotted rectangle. */ for (int i = y; i <= bottom; i += 2) { g.drawLine(x, i, x, i); g.drawLine(right, i, right, i); } } /** * Determines the preferred width and height of an AbstractButton, * given the gap between the button’s text and icon. * * @param b the button whose preferred size is determined. * * @param textIconGap the gap between the button’s text and * icon. * * @return a <code>Dimension</code> object whose <code>width</code> * and <code>height</code> fields indicate the preferred * extent in pixels. * * @see javax.swing.SwingUtilities#layoutCompoundLabel */ public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap) { Rectangle contentRect; Rectangle viewRect; Rectangle iconRect = new Rectangle(); Rectangle textRect = new Rectangle(); Insets insets = b.getInsets(); /* For determining the ideal size, do not assume a size restriction. */ viewRect = new Rectangle(0, 0, /* width */ Integer.MAX_VALUE, /* height */ Integer.MAX_VALUE); /* java.awt.Toolkit.getFontMetrics is deprecated. However, it * seems not obvious how to get to the correct FontMetrics object * otherwise. The real problem probably is that the method * javax.swing.SwingUtilities.layoutCompundLabel should take a * LineMetrics, not a FontMetrics argument. But fixing this that * would change the public API. */ SwingUtilities.layoutCompoundLabel( b, // for the component orientation b.getToolkit().getFontMetrics(b.getFont()), // see comment above b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, textIconGap); /* +------------------------+ +------------------------+ * | | | | * | ICON | | CONTENTCONTENTCONTENT | * | TEXTTEXTTEXT | --> | CONTENTCONTENTCONTENT | * | TEXTTEXTTEXT | | CONTENTCONTENTCONTENT | * +------------------------+ +------------------------+ */ contentRect = textRect.union(iconRect); return new Dimension(insets.left + contentRect.width + insets.right, insets.top + contentRect.height + insets.bottom); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -