/** * Iterate over the lines represented by the child elements of the element this view represents, * looking for the line that is the longest. The <em>longLine</em> variable is updated to * represent the longest line contained. The <em>font</em> variable is updated to indicate the * font used to calculate the longest line. */ void calculateLongestLine() { Component c = getContainer(); font = c.getFont(); metrics = c.getFontMetrics(font); tabSize = getTabSize() * metrics.charWidth(' '); Element lines = getElement(); int n = lines.getElementCount(); for (int i = 0; i < n; i++) { Element line = lines.getElement(i); float w = getLineWidth(i); if (w > longLineWidth) { longLineWidth = w; longLine = line; } } }
@Override public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { int titleHeight = getTitleHeight(c); // create image with vertical gradient and then use alpha composite to // render the image with a horizontal fade BufferedImage titleImage = Utilities.createTranslucentImage(width, titleHeight); GradientPaint gradient = new GradientPaint( 0, 0, titleGradientColors[0], 0, titleHeight, titleGradientColors[1], false); Graphics2D g2 = (Graphics2D) titleImage.getGraphics(); g2.setPaint(gradient); g2.fillRoundRect(x, y, width, height, 10, 10); g2.setColor(Utilities.deriveColorHSB(titleGradientColors[1], 0, 0, -.2f)); g2.drawLine(x + 1, titleHeight - 1, width - 2, titleHeight - 1); g2.setColor(Utilities.deriveColorHSB(titleGradientColors[1], 0, -.5f, .5f)); g2.drawLine(x + 1, titleHeight, width - 2, titleHeight); g2.setPaint( new GradientPaint( 0, 0, new Color(0.0f, 0.0f, 0.0f, 1.0f), width, 0, new Color(0.0f, 0.0f, 0.0f, 0.0f))); g2.setComposite(AlphaComposite.DstIn); g2.fillRect(x, y, width, titleHeight); g2.dispose(); g.drawImage(titleImage, x, y, c); // draw rounded border super.paintBorder(c, g, x, y, width, height); // draw title string g2 = (Graphics2D) g; g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setColor(c.getForeground()); g2.setFont(c.getFont()); FontMetrics metrics = c.getFontMetrics(c.getFont()); g2.drawString(title, x + 8, y + (titleHeight - metrics.getHeight()) / 2 + metrics.getAscent()); }
/** * Fetches the font metrics associated with the component hosting this view. * * @return the metrics */ protected FontMetrics getFontMetrics() { Component c = getContainer(); return c.getFontMetrics(c.getFont()); }
final void updateMetrics() { Component host = getContainer(); Font f = host.getFont(); metrics = host.getFontMetrics(f); // Metrics for the default font. tabSize = getTabSize() * metrics.charWidth('m'); }
protected int getTitleHeight(Component c) { FontMetrics metrics = c.getFontMetrics(c.getFont()); return (int) (metrics.getHeight() * 1.80); }