Example usage for java.awt Graphics2D getFontMetrics

List of usage examples for java.awt Graphics2D getFontMetrics

Introduction

In this page you can find the example usage for java.awt Graphics2D getFontMetrics.

Prototype

public FontMetrics getFontMetrics() 

Source Link

Document

Gets the font metrics of the current font.

Usage

From source file:uk.co.real_logic.aeron.tools.perf_tools.AeronLatencyUnderLoadPublisher.java

private void plotSubset(final Graphics2D g, final int start, final int end, final String title,
        final double startX, final double width, final double stepY, final double mean) {
    final FontMetrics fm = g.getFontMetrics();
    final Color color = g.getColor();
    g.setColor(Color.black);// www. j  a va  2s  . co  m
    g.drawString(title, (int) (startX + width / 2 - fm.stringWidth(title) / 2), 975);
    final String tmp = String.format("Mean: %.3fus", mean);
    g.drawString(tmp, (int) (startX + width / 2 - fm.stringWidth(tmp) / 2), 990);
    g.setColor(color);
    final double stepX = width / (end - start);
    for (int i = start; i < end; i++) {
        final int posX = (int) (startX + stepX * (i - start));
        final int posY = 960 - ((int) (stepY * (timestamps[i] / 1000.0)) + 1);
        g.drawLine(posX, posY, posX, 960);
    }
}

From source file:savant.view.tracks.TrackRenderer.java

public void drawFeatureLabel(Graphics2D g2, String geneName, double startXPos, double y) {
    FontMetrics fm = g2.getFontMetrics();
    double stringstartx = startXPos - fm.stringWidth(geneName) - 5;

    if (stringstartx <= 0) {
        Rectangle2D r = fm.getStringBounds(geneName, g2);

        int b = 2;
        Color textColor = g2.getColor();
        g2.setColor(new Color(255, 255, 255, 200));
        g2.fill(new RoundRectangle2D.Double(3.0, y - (fm.getHeight() - fm.getDescent()) - b,
                r.getWidth() + 2 * b, r.getHeight() + 2 * b, 8.0, 8.0));
        g2.setColor(textColor);/*  w ww  .ja v  a2s.co m*/
        g2.drawString(geneName, 5.0F, (float) y);
    } else {
        g2.drawString(geneName, (float) stringstartx, (float) y);
    }
}

From source file:nl.b3p.kaartenbalie.core.server.b3pLayering.ConfigLayer.java

protected void drawTitledMessageBox(Graphics2D g2d, String title, String message, int x, int y, int w, int h) {
    /* Do some calculations and init variables. */
    g2d.setFont(KBConfiguration.OHD_messageBoxFont);
    FontMetrics fm = g2d.getFontMetrics();
    int labelHeight = KBConfiguration.OHD_messageBoxFont.getSize() + (KBConfiguration.OHD_padding * 2);
    int angling = labelHeight;
    Rectangle2D testRectangle = fm.getStringBounds(title, g2d);
    int labelWidth = (int) testRectangle.getWidth();

    if (w < labelWidth + (2 * angling)) {
        w = labelWidth + (2 * angling);//from  w  w  w  . ja v a  2s  .c o m
    }
    y += labelHeight;
    /* Now draw the box...    */
    drawMessageBox(g2d, message, x, y, w, h);

    /* Draw the label background */
    g2d.setColor(KBConfiguration.OHD_labelBoxColor);
    GeneralPath label = new GeneralPath();
    label.moveTo(x, y);
    label.lineTo(x + angling, y - labelHeight);
    label.lineTo(x + angling + labelWidth, y - labelHeight);
    label.lineTo(x + (angling * 2) + labelWidth, y);
    label.closePath();
    g2d.fill(label);

    /* Draw the label Lines..  */
    g2d.setColor(KBConfiguration.OHD_borderBoxTopLeft);
    g2d.drawLine(x, y, x + angling, y - labelHeight);
    g2d.drawLine(x + angling, y - labelHeight, x + angling + labelWidth, y - labelHeight);
    g2d.setColor(KBConfiguration.OHD_borderBoxBottomRight);
    g2d.drawLine(x + angling + labelWidth, y - labelHeight, x + (angling * 2) + labelWidth, y);
    g2d.setColor(KBConfiguration.OHD_borderBoxBackground);
    g2d.drawLine(x + (angling * 2) + labelWidth, y, x, y);
    /*Then add the title... */
    g2d.setColor(KBConfiguration.OHD_labelFontBoxColor);
    g2d.drawString(title, x + angling, y - KBConfiguration.OHD_padding);

}

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 va 2s.  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:org.springframework.cloud.stream.app.pose.estimation.processor.PoseEstimateOutputMessageBuilder.java

private void drawPartOval(Part part, int radius, Graphics2D g) {
    int partX = part.getNormalizedX();
    int partY = part.getNormalizedY();

    g.setColor(GraphicsUtils.LIMBS_COLORS[part.getPartType().getId()]);
    g.fillOval(partX - radius, partY - radius, 2 * radius, 2 * radius);

    if (this.poseProperties.isDrawPartLabels()) {
        String label = part.getPartType().getId() + ":" + part.getPartType().name();
        FontMetrics fm = g.getFontMetrics();
        int labelX = partX + 5;
        int labelY = partY - 5;
        AffineTransform t = g.getTransform();
        g.setTransform(AffineTransform.getRotateInstance(Math.toRadians(-35), labelX, labelY));

        g.drawString(label, labelX, labelY);
        g.setTransform(t);//from   w  ww . j  ava2  s  . c  o m
    }

}

From source file:edu.csudh.goTorosBank.WithdrawServlet.java

public String writeIntoCheck(String filePath, String username, String theAmount, String amountInWords,
        String dateWrote, String person_payingto, String billType) throws IOException, NullPointerException {

    File blueCheck = new File("blank-blue-check.jpg");
    String pathToOriginal = getServletContext().getRealPath("/" + blueCheck);

    File file = new File(pathToOriginal);
    BufferedImage imageToCopy = null;
    try {//w ww .j a v  a2  s.c  o m
        imageToCopy = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String amount = theAmount;
    String person_gettingPayed = person_payingto;
    String amountinWords = amountInWords;
    String date = dateWrote;
    String bill_type = billType;

    int w = imageToCopy.getWidth();
    int h = imageToCopy.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    g2d.setColor(g2d.getBackground());
    g2d.fillRect(0, 0, w, h);
    g2d.drawImage(imageToCopy, 0, -100, null);
    g2d.setPaint(Color.black);
    g2d.setFont(new java.awt.Font("Serif", Font.BOLD, 36));
    //g2d.setFont(new Font("Serif", Font.BOLD, 36));

    FontMetrics fm = g2d.getFontMetrics();
    int x = img.getWidth() - fm.stringWidth(amount) - 100;
    int y = fm.getHeight();
    g2d.drawString(amount, x - 70, y + 335);
    g2d.drawString(person_gettingPayed, x - 800, y + 329);
    g2d.drawString(amountinWords, x - 940, y + 390);
    g2d.drawString(date, x - 340, y + 245);
    g2d.drawString(bill_type, x - 900, y + 530);

    String signature = "Use The Force";
    g2d.setFont(new java.awt.Font("Monotype Corsiva", Font.BOLD | Font.ITALIC, 36));
    g2d.drawString(signature, x - 340, y + 530);
    g2d.dispose();
    /*write check to file*/
    String filename = fileNameGenerator(username);
    String fullname = filePath + "_" + filename + ".jpg";
    ImageIO.write(img, "jpg", new File(fullname));
    return fullname;
}

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);/*from w  w  w.  j av  a2  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:savant.amino.AminoCanvas.java

@Override
public void paintComponent(Graphics g) {
    if (GenomeUtils.getGenome().isSequenceSet()) {

        double aminoWidth = track.transformXPos(3) - track.transformXPos(0);
        if (aminoWidth > 0.5) {

            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // We'll be drawing labels if a 'W' (tryptophan) will fit into the space of 3 bases.
            g2.setFont(g2.getFont().deriveFont(Font.PLAIN, 8));
            boolean labelled = g2.getFontMetrics().charWidth('W') < aminoWidth;

            try {
                List<Record> records = track.getDataInRange();
                if (records != null) {
                    for (Record r : records) {
                        RichIntervalRecord rr = (RichIntervalRecord) r;
                        int recordStart = rr.getInterval().getStart();
                        thickStart = rr.getThickStart();
                        int thickEnd = rr.getThickEnd() + 1;
                        LOG.debug(rr.getAlternateName() + ": thickStart=" + thickStart + ", thickEnd="
                                + thickEnd);

                        if (thickEnd > thickStart) {
                            sequence = GenomeUtils.getGenome().getSequence(
                                    NavigationUtils.getCurrentReferenceName(),
                                    RangeUtils.createRange(thickStart, thickEnd));

                            int pos = thickStart;
                            int leftovers = -1; // Left-overs from the previous block.
                            List<Block> blocks = rr.getBlocks();
                            if (blocks != null) {
                                for (Block b : blocks) {

                                    if (pos + 3 <= thickEnd) {
                                        // Block positions are relative to the start of the record.
                                        int blockStart = b.getPosition() + recordStart;
                                        int blockEnd = b.getEnd() + recordStart;
                                        LOG.debug(rr.getAlternateName() + ": blockStart=" + blockStart
                                                + ", blockEnd=" + blockEnd);

                                        AminoAcid a;

                                        // If we have leftovers, take care of them first.
                                        switch (leftovers) {
                                        case -1:
                                            // Fresh record with no leftovers.
                                            break;
                                        case 0:
                                            // No leftovers, so we can start immediately on the new block.
                                            pos = blockStart;
                                            if (pos < thickStart) {
                                                pos = thickStart;
                                            }
                                            break;
                                        case 1:
                                            // One base from previous block, two bases from current one.
                                            LOG.debug(rr.getAlternateName() + ": handling leftover "
                                                    + getBase(pos) + " at " + pos);
                                            if (rr.getStrand() == Strand.FORWARD) {
                                                a = AminoAcid.lookup(getBase(pos), getBase(blockStart),
                                                        getBase(blockStart + 1));
                                            } else {
                                                a = AminoAcid.lookup(getComplement(blockStart + 1),
                                                        getComplement(blockStart), getComplement(pos));
                                            }
                                            paintAminoAcid(g2, a, pos, 1, pos, labelled);
                                            paintAminoAcid(g2, a, blockStart, 2, blockStart - 1, labelled);
                                            pos = blockStart + 2;
                                            break;
                                        case 2:
                                            // Two bases from previous block, one base from current one.
                                            LOG.debug(rr.getAlternateName() + ": handling leftover "
                                                    + getBase(pos) + "," + getBase(pos + 1) + " at " + pos + ","
                                                    + (pos + 1));
                                            if (rr.getStrand() == Strand.FORWARD) {
                                                a = AminoAcid.lookup(getBase(pos), getBase(pos + 1),
                                                        getBase(blockStart));
                                            } else {
                                                a = AminoAcid.lookup(getComplement(blockStart),
                                                        getComplement(pos + 1), getComplement(pos));
                                            }
                                            paintAminoAcid(g2, a, pos, 2, pos, labelled);
                                            paintAminoAcid(g2, a, blockStart, 1, blockStart - 2, labelled);
                                            pos = blockStart + 1;
                                            break;
                                        }

                                        // Now, handle codons which are entirely contained within the block.
                                        while (pos + 3 <= blockEnd && pos + 3 <= thickEnd) {
                                            if (rr.getStrand() == Strand.FORWARD) {
                                                a = AminoAcid.lookup(getBase(pos), getBase(pos + 1),
                                                        getBase(pos + 2));
                                            } else {
                                                a = AminoAcid.lookup(getComplement(pos + 2),
                                                        getComplement(pos + 1), getComplement(pos));
                                            }
                                            paintAminoAcid(g2, a, pos, 3, pos, labelled);
                                            pos += 3;
                                        }
                                        leftovers = (blockEnd - pos) % 3;
                                        LOG.debug(rr.getAlternateName() + ": breaking out of loop: pos=" + pos
                                                + ", blockEnd=" + blockEnd + ", leftovers=" + leftovers);
                                    }//from   w  ww  .  j  a  v  a2s  .c om
                                }
                            }
                        }
                    }
                }
            } catch (Exception x) {
                LOG.info("Unable to retrieve sequence.", x);
            }
        }
    }
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

/**Render default view if there is no timeframe yet*/
private void renderNoInfoView(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(new TexturePaint(STROKED, new Rectangle(0, 0, 3, 3)));
    g2d.fillRect(0, 0, getWidth(), getHeight());
    Font f = g2d.getFont().deriveFont(Font.BOLD, 14.0f);
    g2d.setFont(f);/*w  ww . j ava2s.c  om*/
    Rectangle2D bounds = g2d.getFontMetrics().getStringBounds("Kein Zeitfenster aktiv", g);
    int dx = 10;
    if (getWidth() > bounds.getWidth()) {
        dx = (int) Math.rint((getWidth() - bounds.getWidth()) / 2);
    }

    int dy = 10;
    if (getHeight() > bounds.getHeight()) {
        dy = (int) Math.rint((getHeight() - bounds.getHeight()) / 2);
    }
    g2d.setColor(Color.black);
    g2d.drawString("Kein Zeitfenster aktiv", dx, dy);
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

private void renderPopup(HashMap<String, Object> pPopupInfo, Graphics2D pG2D) {
    Point location = (Point) pPopupInfo.get("popup.location");
    String label = (String) pPopupInfo.get("popup.label");
    if (location == null || label == null) {
        return;//from   w ww. j av a  2 s .  com
    }
    pG2D.setColor(new Color(255, 255, 204));
    Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(label, pG2D);
    pG2D.fillRect(location.x, location.y - (int) labelBounds.getHeight(), (int) labelBounds.getWidth() + 5,
            (int) labelBounds.getHeight() + 5);
    pG2D.setColor(Color.BLACK);
    pG2D.drawRect(location.x, location.y - (int) labelBounds.getHeight(), (int) labelBounds.getWidth() + 5,
            (int) labelBounds.getHeight() + 5);
    pG2D.drawString(label, location.x + 2, location.y);

}