Example usage for java.awt FontMetrics getAscent

List of usage examples for java.awt FontMetrics getAscent

Introduction

In this page you can find the example usage for java.awt FontMetrics getAscent.

Prototype

public int getAscent() 

Source Link

Document

Determines the font ascent of the Font described by this FontMetrics object.

Usage

From source file:net.sourceforge.processdash.ui.lib.chart.StandardDiscItemRenderer.java

protected void drawDiscLabel(Graphics2D g2, Ellipse2D shape, Paint labelPaint, Font labelFont, String label) {
    g2.setFont(labelFont);//w w  w  .jav a 2 s .co  m
    g2.setPaint(labelPaint);
    FontMetrics m = g2.getFontMetrics();
    int height = m.getAscent();
    double halfHeight = height / 2.0;

    double radius = shape.getWidth() / 2;
    double availableRadius = radius - getLabelPadding();
    double halfWidth = Math.sqrt(availableRadius * availableRadius - halfHeight * halfHeight);

    int width = (int) Math.floor(halfWidth * 2 + 0.99);

    Rectangle viewR = new Rectangle(width, height);
    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    String text = SwingUtilities.layoutCompoundLabel(m, label, null, SwingConstants.CENTER,
            SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.TRAILING, viewR, iconR, textR, 0);
    if (text.equals(label) || text.length() >= 3 + minLabelChars) {
        double x = shape.getCenterX() - halfWidth + textR.x;
        double y = shape.getCenterY() + halfHeight + textR.y;
        g2.drawString(text, (float) x, (float) y);
    }
}

From source file:org.tinymediamanager.ui.components.ImageLabel.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (scaledImage != null) {
        int originalWidth = scaledImage.getWidth(null);
        int originalHeight = scaledImage.getHeight(null);

        // calculate new height/width
        int newWidth = 0;
        int newHeight = 0;

        int offsetX = 0;
        int offsetY = 0;

        if (drawBorder && !drawFullWidth) {
            Point size = ImageCache.calculateSize(this.getWidth() - 8, this.getHeight() - 8, originalWidth,
                    originalHeight, true);

            // calculate offsets
            if (position == Position.TOP_RIGHT || position == Position.BOTTOM_RIGHT) {
                offsetX = this.getWidth() - size.x - 8;
            }//from   www  .ja v  a2s  .  c o  m

            if (position == Position.BOTTOM_LEFT || position == Position.BOTTOM_RIGHT) {
                offsetY = this.getHeight() - size.y - 8;
            }

            if (position == Position.CENTER) {
                offsetX = (this.getWidth() - size.x - 8) / 2;
                offsetY = (this.getHeight() - size.y - 8) / 2;
            }

            newWidth = size.x;
            newHeight = size.y;

            // when the image size differs too much - reload and rescale the original image
            recreateScaledImageIfNeeded(originalWidth, originalHeight, newWidth, newHeight);

            g.setColor(Color.BLACK);
            g.drawRect(offsetX, offsetY, size.x + 7, size.y + 7);
            g.setColor(Color.WHITE);
            g.fillRect(offsetX + 1, offsetY + 1, size.x + 6, size.y + 6);
            // g.drawImage(Scaling.scale(originalImage, newWidth, newHeight), offsetX + 4, offsetY + 4, newWidth, newHeight, this);
            g.drawImage(scaledImage, offsetX + 4, offsetY + 4, newWidth, newHeight, this);
        } else {
            Point size = null;
            if (drawFullWidth) {
                size = new Point(this.getWidth(), this.getWidth() * originalHeight / originalWidth);
            } else {
                size = ImageCache.calculateSize(this.getWidth(), this.getHeight(), originalWidth,
                        originalHeight, true);
            }

            // calculate offsets
            if (position == Position.TOP_RIGHT || position == Position.BOTTOM_RIGHT) {
                offsetX = this.getWidth() - size.x;
            }

            if (position == Position.BOTTOM_LEFT || position == Position.BOTTOM_RIGHT) {
                offsetY = this.getHeight() - size.y;
            }

            if (position == Position.CENTER) {
                offsetX = (this.getWidth() - size.x) / 2;
                offsetY = (this.getHeight() - size.y) / 2;
            }

            newWidth = size.x;
            newHeight = size.y;

            // when the image size differs too much - reload and rescale the original image
            recreateScaledImageIfNeeded(originalWidth, originalHeight, newWidth, newHeight);

            // g.drawImage(Scaling.scale(originalImage, newWidth, newHeight), offsetX, offsetY, newWidth, newHeight, this);
            g.drawImage(scaledImage, offsetX, offsetY, newWidth, newHeight, this);
        }
    } else {
        // draw border and background
        if (drawBorder) {
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
            if (getParent().isOpaque()) {
                g.setColor(getParent().getBackground());
                g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);
            }
        }

        // calculate diagonal
        int diagonalSize = (int) Math
                .sqrt(this.getWidth() * this.getWidth() + this.getHeight() * this.getHeight());

        // draw text
        String text = "";
        if (alternativeText != null) {
            text = alternativeText;
        } else {
            text = BUNDLE.getString("image.nonefound"); //$NON-NLS-1$
        }
        if (!getParent().isOpaque()) {
            text = "";
        }
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        AffineTransform orig = g2.getTransform();
        AffineTransform at = new AffineTransform(orig);
        at.translate(0, this.getHeight());
        at.rotate(this.getWidth(), -this.getHeight());
        g2.setTransform(at);
        g2.setColor(Color.BLACK);
        g2.setFont(FONT);

        FontMetrics fm = g2.getFontMetrics();
        int x = (diagonalSize - fm.stringWidth(text)) / 2;
        int y = (fm.getAscent() - fm.getDescent()) / 2;

        g2.drawString(text, x, y);
        // g2.drawLine(0, 0, diagonalSize, 0);
        at.translate(0, -this.getHeight());
        g2.setTransform(orig);
    }
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreeRenderer.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension d = getSize();/*from w  w  w. ja v  a 2  s.c  o  m*/
    //System.out.println("d: "+d+"     "+g.getClipBounds());

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHints(renderingHints);

    FontMetrics fm = g2d.getFontMetrics();
    int imgY = img1 != null ? (d.height - 24) / 2 : 0;
    int imgY2 = img1 != null ? (d.height - 16) / 2 : 0;
    int txtY = ((d.height - fm.getHeight()) / 2) + fm.getAscent();
    int x = 0;

    Color color = g2d.getColor();

    if (img1 != null) {
        g2d.drawImage(img1.getImage(), x, imgY, null);
        x += img1.getIconWidth();
    }

    int iconInx = 0;

    if (txt1 != null) {
        x += getIconTextGap();
        g2d.setColor(getForeground());
        g.drawString(txt1, x, txtY);
        x += fm.stringWidth(txt1);
        g2d.setColor(color);
    }

    if (isContainer) {
        //if (isSelected  && isEditable)
        //{
        //    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++); // Delete the container
        //}

        if (hasColObj) {
            if (img2 != null) {
                x += 1;
                x += iconSep;
                g2d.drawImage(img2.getImage(), x, imgY2, null);
                x += img2.getIconWidth();
            }

            if (txt2 != null) {
                x += getIconTextGap();
                g2d.setColor(getForeground());
                g.drawString(txt2, x, txtY);
                x += fm.stringWidth(txt2);
                g2d.setColor(color);
            }

            if (isSelected) {
                x += iconSep;
                x += drawIcon(g2d, x, imgY2, viewImgIcon, iconInx++);

                if (isEditable) {
                    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++);
                }
            }
        } else if (isSelected) // No Col Obj
        {
            x += iconSep;
            x += drawIcon(g2d, x, imgY2, schImgIcon, iconInx++);
            x += drawIcon(g2d, x, imgY2, addImgIcon, iconInx++);
        }

    } else if (isSelected) {
        x += iconSep;
        x += drawIcon(g2d, x, imgY2, viewImgIcon, iconInx++); // View for Collection Object

        //if (!isViewMode)
        //{
        //    x += iconSep;
        //    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++); // Delete for Collection Object
        //}
    }

    g2d.dispose();
}

From source file:net.java.sip.communicator.impl.osdependent.jdic.SystrayServiceJdicImpl.java

private BufferedImage createOverlayImage(String text) {
    int size = 16;
    BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    //background//w ww.  j  a  v  a2s .  c o  m
    g.setPaint(new Color(0, 0, 0, 102));
    g.fillRoundRect(0, 0, size, size, size, size);

    //filling
    int mainRadius = 14;
    g.setPaint(new Color(255, 98, 89));
    g.fillRoundRect(size / 2 - mainRadius / 2, size / 2 - mainRadius / 2, mainRadius, mainRadius, size, size);

    //text
    Font font = g.getFont();
    g.setFont(new Font(font.getName(), Font.BOLD, 9));
    FontMetrics fontMetrics = g.getFontMetrics();
    int textWidth = fontMetrics.stringWidth(text);
    g.setColor(Color.white);
    g.drawString(text, size / 2 - textWidth / 2,
            size / 2 - fontMetrics.getHeight() / 2 + fontMetrics.getAscent());

    return image;
}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Overrides paint to draw in name at top with separator AND the Label
 *//*from   w ww. j  a v a2s  .c  om*/
public void paint(Graphics g) {
    super.paint(g);

    if (!useSeparator) {
        Dimension dim = getSize();

        FontMetrics fm = g.getFontMetrics();
        int strW = fm.stringWidth(name);

        int x = (dim.width - strW) / 2;
        Insets ins = getBorder().getBorderInsets(this);
        int y = 2 + fm.getAscent();

        int lineW = dim.width - ins.left - ins.right;
        g.setColor(Color.BLUE.darker());
        g.drawString(name, x, y);
        x = ins.left;
        y += fm.getDescent() + fm.getLeading();

        g.setColor(Color.LIGHT_GRAY.brighter());
        g.drawLine(x, y, x + lineW, y);
        y++;
        x++;
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(x, y, x + lineW, y);
    }
}

From source file:net.pms.util.GenericIcons.java

/**
 * Add the format(container) name of the media to the generic icon image.
 *
 * @param image BufferdImage to be the label added
 * @param label the media container name to be added as a label
 * @param renderer the renderer configuration
 *
 * @return the generic icon with the container label added and scaled in accordance with renderer setting
 *//*from  w  ww  . j  a va  2s. c  om*/
private DLNAThumbnail addFormatLabelToImage(String label, ImageFormat imageFormat, IconType iconType)
        throws IOException {

    BufferedImage image;
    switch (iconType) {
    case AUDIO:
        image = genericAudioIcon;
        break;
    case IMAGE:
        image = genericImageIcon;
        break;
    case VIDEO:
        image = genericVideoIcon;
        break;
    default:
        image = genericUnknownIcon;
    }

    if (image != null) {
        // Make a copy
        ColorModel colorModel = image.getColorModel();
        image = new BufferedImage(colorModel, image.copyData(null), colorModel.isAlphaPremultiplied(), null);
    }

    ByteArrayOutputStream out = null;

    if (label != null && image != null) {
        out = new ByteArrayOutputStream();
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        try {
            int size = 40;
            Font font = new Font(Font.SANS_SERIF, Font.BOLD, size);
            FontMetrics metrics = g.getFontMetrics(font);
            while (size > 7 && metrics.stringWidth(label) > 135) {
                size--;
                font = new Font(Font.SANS_SERIF, Font.BOLD, size);
                metrics = g.getFontMetrics(font);
            }
            // Text center point 127x, 49y - calculate centering coordinates
            int x = 127 - metrics.stringWidth(label) / 2;
            int y = 46 + metrics.getAscent() / 2;
            g.drawImage(image, 0, 0, null);
            g.setColor(Color.WHITE);
            g.setFont(font);
            g.drawString(label, x, y);

            ImageIO.setUseCache(false);
            ImageIOTools.imageIOWrite(image, imageFormat.toString(), out);
        } finally {
            g.dispose();
        }
    }
    return out != null ? DLNAThumbnail.toThumbnail(out.toByteArray(), 0, 0, ScaleType.MAX, imageFormat, false)
            : null;
}

From source file:BeanContainer.java

public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color colorRetainer = g.getColor();

        g.setColor(getBackground());//  w  w w.ja va  2 s  .c o m
        g.fillRect(0, 0, getWidth(), getHeight());
        getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

        m_calendar.setTime(new Date()); // get current time
        int hrs = m_calendar.get(Calendar.HOUR_OF_DAY);
        int min = m_calendar.get(Calendar.MINUTE);

        g.setColor(getForeground());
        if (m_digital) {
            String time = "" + hrs + ":" + min;
            g.setFont(getFont());
            FontMetrics fm = g.getFontMetrics();
            int y = (getHeight() + fm.getAscent()) / 2;
            int x = (getWidth() - fm.stringWidth(time)) / 2;
            g.drawString(time, x, y);
        } else {
            int x = getWidth() / 2;
            int y = getHeight() / 2;
            int rh = getHeight() / 4;
            int rm = getHeight() / 3;

            double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
            double am = min / 30.0 * Math.PI;

            g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
            g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
        }

        g.setColor(colorRetainer);
    }

From source file:edu.ku.brc.af.core.NavBox.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension dim = getSize();/*from   w w  w. ja  v a 2  s  .  c  o m*/

    FontMetrics fm = g.getFontMetrics();
    int strW = fm.stringWidth(name);

    int x = (dim.width - strW) / 2;
    Insets ins = getBorder().getBorderInsets(this);
    int y = 2 + fm.getAscent();

    int lineW = dim.width - ins.left - ins.right;
    x = ins.left;
    int txtY = y;
    y += 3;

    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(x, y, x + lineW, y);
    y++;
    minHeight = y;

    x++;
    g.setColor(Color.LIGHT_GRAY.darker());
    g.drawLine(x, y, x + lineW, y);

    ((Graphics2D) g).setRenderingHints(UIHelper.createTextRenderingHints());
    g.setColor(Color.BLUE.darker());
    g.drawString(name, x, txtY);

    if (collapsableIconOpen != null) {
        if (iconRect == null) {
            iconRect = getBounds();
            iconRect.x = iconRect.width - collapsableIconOpen.getIconWidth();
            iconRect.y = 0;
        }
        g.drawImage(icon.getImage(), iconRect.x, iconRect.y, null);
    }
}

From source file:com.fluidops.iwb.deepzoom.ImageLoader.java

private void generateIDCard(URI uri, Map<URI, Set<Value>> facets, String url, File file) {
    int width = 200;
    int height = 200;

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    ig2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    /* Special ID card handling for certain entity types */

    /*  TODO: special images based on type
    if(facets.containsKey(RDF.TYPE)) {//from  w w w .  j  a v  a  2 s  . com
    Set<Value> facet = facets.get(RDF.TYPE);
            
    for(Value v : facet)
    {
        if(v.equals(Vocabulary.DCAT_DATASET))
        {
            
            Image img = null;
            try
            {
                img = ImageIO.read( new File( "webapps/ROOT/images/rdf.jpg" ) );
            }
            catch (MalformedURLException e)
            {
                logger.error(e.getMessage(), e);
            }
            catch (IOException e)
            {
                logger.error("Could not get image");
            }
            
            ig2.drawImage( img, 0, 0, null );        
            break;
        }
    }
    } */

    String label = EndpointImpl.api().getDataManager().getLabel(uri);
    Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20);
    ig2.setFont(font);

    FontMetrics fontMetrics = ig2.getFontMetrics();
    int labelwidth = fontMetrics.stringWidth(label);
    if (labelwidth >= width) {
        int fontsize = 20 * width / labelwidth;
        font = new Font(Font.SANS_SERIF, Font.BOLD, fontsize);
        ig2.setFont(font);
        fontMetrics = ig2.getFontMetrics();
    }

    int x = (width - fontMetrics.stringWidth(label)) / 2;
    int y = (fontMetrics.getAscent() + (height - (fontMetrics.getAscent() + fontMetrics.getDescent())) / 2);

    ig2.setPaint(Color.black);

    ig2.drawString(label, x, y);

    BufferedOutputStream out;
    try {
        out = new BufferedOutputStream(new FileOutputStream(file));
        ImageIO.write(bi, "PNG", out);
        out.flush();
        out.close();

    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:com.celements.photo.image.GenerateThumbnail.java

private void drawWatermark(String watermark, Graphics2D g2d, int width, int height) {
    //TODO implement 'secure' watermark (i.e. check if this algorithm secure or can it be easily reversed?)
    g2d.setColor(new Color(255, 255, 150));
    AlphaComposite transprency = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.45f);
    g2d.setComposite(transprency);/*from ww  w. j a va2  s . c om*/

    FontMetrics metrics = calcWatermarkFontSize(watermark, width, g2d);
    g2d.setFont(calcWatermarkFontSize(watermark, width, g2d).getFont());

    // rotate around image center
    double angle = (Math.sin(height / (double) width));
    g2d.rotate(-angle, width / 2.0, height / 2.0);

    g2d.drawString(watermark, (width - metrics.stringWidth(watermark)) / 2,
            ((height - metrics.getHeight()) / 2) + metrics.getAscent());

    // undo rotation for correct copyright positioning
    g2d.rotate(angle, width / 2.0, height / 2.0);
}