Example usage for java.awt FontMetrics getHeight

List of usage examples for java.awt FontMetrics getHeight

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Gets the standard height of a line of text in this font.

Usage

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  w w w. j  a  v a  2  s  .co  m

    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);
}

From source file:org.squidy.designer.knowledgebase.RepositoryItem.java

private void initPane() {
    // Add Zoomable Component
    new Thread(new Runnable() {
        public void run() {

            // ProgressIndicator indicator = new
            // ProgressIndicator(InformationShape.this);

            if (information == null || "".equals(information)) {
                return;
            }//from  www  .  j av  a2  s  .co  m

            URL url = null;
            try {
                try {
                    if (information.endsWith(".pdf")) {
                        url = InformationShape.class.getResource(information);
                    } else if (information.endsWith(".html")) {
                        try {
                            url = new URL(information);
                        } catch (Exception e) {
                            url = InformationShape.class.getResource(information);
                        }
                    } else {
                        url = new URL(information);
                    }
                } catch (Exception e) {
                    // do nothing
                }

                PNode cropNode;
                // PDF
                if (information.endsWith(".pdf")) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Display information as PDF.");
                    }
                    cropNode = new PDFPane(url.getFile());
                }
                // HTML
                else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Display information as HTML.");
                    }

                    JEditorPane editorPane = new JEditorPane();
                    editorPane.setFont(internalFont.deriveFont(10f));

                    FontMetrics fm = editorPane.getFontMetrics(editorPane.getFont());
                    int editorWidth = 400;
                    editorPane.setPreferredSize(new Dimension(editorWidth,
                            FontUtils.getLineCount(information, editorWidth) * fm.getHeight()));

                    cropNode = JComponentWrapper.create(editorPane);
                    editorPane.setEditable(false);

                    if (information.endsWith(".html")) {
                        HTMLEditorKit editorKit = new HTMLEditorKit();
                        editorPane.setEditorKit(editorKit);
                        editorPane.setPage(url);
                        editorPane.setPreferredSize(new Dimension(800, 2000));
                    } else {
                        editorPane.setText(information);
                    }

                    // Prepare HTML Kit
                    // HTMLParser editorKit = new HTMLParser();
                    // HTMLParserCallback callback = new
                    // HTMLParserCallback();
                    // getComponentEditorPane().setEditorKit(editorKit);
                    // //Open connection
                    // InputStreamReader reader = new
                    // InputStreamReader(url.openStream());
                    // //Start parse process
                    // editorKit.getParser().parse(reader, callback, true);
                    // Wait until parsing process has finished
                    // try {
                    // Thread.sleep(2000);
                    // }
                    // catch (InterruptedException e) {
                    // if (LOG.isErrorEnabled()) {
                    // LOG.error("Error in " +
                    // InformationShape.class.getName() + ".", e);
                    // }
                    // }
                }

                cropScroll = new CropScroll(cropNode, new Dimension(1000, 700), 0.2);
                cropScroll.setOffset(
                        getBoundsReference().getCenterX() - cropScroll.getBoundsReference().getCenterX(), 250);
                addChild(cropScroll);
                invalidateFullBounds();
                invalidateLayout();
                invalidatePaint();
            } catch (MalformedURLException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Could not parse URL from input string: " + e.getMessage() + " in "
                            + RepositoryItem.class.getName() + ".\nInput was: " + information);
                }
            } catch (IOException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Could not create HTMLPane in " + RepositoryItem.class.getName(), e);
                }
            }

            // indicator.done();
        }
    }).start();
}

From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java

private byte[] createImage(int width, int height, String text) throws IOException {
    if (width < 0) {
        width = random.nextInt(DEFAULT_MAX_IMAGE_WIDTH - DEFAULT_MIN_IMAGE_WIDTH) + DEFAULT_MIN_IMAGE_WIDTH;
    }/*from   w w w.  j a  v  a2 s .c om*/
    if (height < 0) {
        height = random.nextInt(DEFAULT_MAX_IMAGE_HEIGHT - DEFAULT_MIN_IMAGE_HEIGHT) + DEFAULT_MIN_IMAGE_HEIGHT;
    }

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

    Graphics2D g2 = (Graphics2D) img.getGraphics();
    g2.setBackground(new Color(220, 220, 220));

    Dimension size;
    float fontSize = g2.getFont().getSize();
    // Make the text as large as possible.
    do {
        g2.setFont(g2.getFont().deriveFont(fontSize));
        FontMetrics metrics = g2.getFontMetrics(g2.getFont());
        int hgt = metrics.getHeight();
        int adv = metrics.stringWidth(text);
        size = new Dimension(adv + 2, hgt + 2);
        fontSize = fontSize + 1f;
    } while (size.width < Math.round(0.9 * width) && size.height < Math.round(0.9 * height));

    g2.setColor(Color.DARK_GRAY);
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.drawString(text, (width - size.width) / 2, (height - size.height) / 2);
    g2.setColor(Color.LIGHT_GRAY);
    g2.drawRect(0, 0, width - 1, height - 1);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(width * height);
    ImageIO.write(img, "png", baos);
    baos.flush();
    byte[] rawBytes = baos.toByteArray();
    baos.close();

    return rawBytes;
}

From source file:org.executequery.gui.erd.ErdTable.java

protected void drawTable(Graphics2D g, int offsetX, int offsetY) {

    if (parent == null) {

        return;//  w ww .  ja  v  a 2 s . c o m
    }

    Font tableNameFont = parent.getTableNameFont();
    Font columnNameFont = parent.getColumnNameFont();

    // set the table value background
    g.setColor(TITLE_BAR_BG_COLOR);
    g.fillRect(offsetX, offsetY, FINAL_WIDTH - 1, TITLE_BAR_HEIGHT);

    // set the table value
    FontMetrics fm = g.getFontMetrics(tableNameFont);
    int lineHeight = fm.getHeight();
    int titleXPosn = (FINAL_WIDTH / 2) - (fm.stringWidth(tableName) / 2) + offsetX;

    g.setColor(Color.BLACK);
    g.setFont(tableNameFont);
    g.drawString(tableName, titleXPosn, lineHeight + offsetY);

    // draw the line separator
    lineHeight = TITLE_BAR_HEIGHT + offsetY - 1;
    g.drawLine(offsetX, lineHeight, offsetX + FINAL_WIDTH - 1, lineHeight);

    // fill the white background
    g.setColor(tableBackground);
    g.fillRect(offsetX, TITLE_BAR_HEIGHT + offsetY, FINAL_WIDTH - 1, FINAL_HEIGHT - TITLE_BAR_HEIGHT - 1);

    // add the column names
    fm = g.getFontMetrics(columnNameFont);
    int heightPlusSep = 1 + TITLE_BAR_HEIGHT + offsetY;
    int leftMargin = 5 + offsetX;

    lineHeight = fm.getHeight();
    g.setColor(Color.BLACK);
    g.setFont(columnNameFont);

    int drawCount = 0;
    String value = null;
    if (ArrayUtils.isNotEmpty(columns)) {

        for (int i = 0; i < columns.length; i++) {
            ColumnData column = columns[i];
            if (displayReferencedKeysOnly && !column.isKey()) {
                continue;
            }

            int y = (((drawCount++) + 1) * lineHeight) + heightPlusSep;
            int x = leftMargin;

            // draw the column value string
            value = column.getColumnName();
            g.drawString(value, x, y);

            // draw the data type and size string
            x = leftMargin + dataTypeOffset;
            value = column.getFormattedDataType();
            g.drawString(value, x, y);

            // draw the key label
            if (column.isKey()) {

                if (column.isPrimaryKey() && column.isForeignKey()) {

                    value = PRIMARY + FOREIGN;

                } else if (column.isPrimaryKey()) {

                    value = PRIMARY;

                } else if (column.isForeignKey()) {

                    value = FOREIGN;
                }

                x = leftMargin + dataTypeOffset + keyLabelOffset;
                g.drawString(value, x, y);
            }

        }

    }

    // draw the rectangle border
    double scale = g.getTransform().getScaleX();

    if (selected && scale != ErdPrintable.PRINT_SCALE) {
        g.setStroke(focusBorderStroke);
        g.setColor(Color.BLUE);
    } else {
        g.setColor(Color.BLACK);
    }

    g.drawRect(offsetX, offsetY, FINAL_WIDTH - 1, FINAL_HEIGHT - 1);
    //    g.setColor(Color.DARK_GRAY);
    //    g.draw3DRect(offsetX, offsetY, FINAL_WIDTH - 2, FINAL_HEIGHT - 2, true);
}

From source file:org.squidy.designer.zoom.impl.VisualizationShape.java

/**
 * @param paintContext//  w  w w.  j  av  a2  s  . c om
 */
protected void paintHeadline(PPaintContext paintContext) {

    Graphics2D g = paintContext.getGraphics();

    g.setFont(fontHeadline);
    FontMetrics fm = g.getFontMetrics();

    PBounds bounds = getBoundsReference();
    double x = bounds.getX();
    double width = bounds.getWidth();

    int titleWidth = FontUtils.getWidthOfText(fm, getTitle());
    g.drawString(getTitle(), (int) (x + width / 2) - (titleWidth / 2), 100);

    int breadcrumbWidth = FontUtils.getWidthOfText(fm, getBreadcrumb());
    g.drawString(getBreadcrumb(), (int) (x + width / 2) - (breadcrumbWidth / 2), 100 + fm.getHeight());
}

From source file:odcplot.OdcPlot.java

private void initImage() {
    img = new BufferedImage(outX, outY, BufferedImage.TYPE_BYTE_INDEXED, getColorModel());
    grph = img.createGraphics();/*from   w w  w  . jav a 2  s  .  c om*/
    grph.setBackground(Color.white);
    grph.clearRect(0, 0, outX, outY);
    if (outX < 600) {
        lblFontSize = 9;
        titleFontSize = 11;
        addLegend = false;
        addGPS = false;
    } else if (outX < 800) {
        lblFontSize = 12;
        titleFontSize = 14;
        addLegend = true;
        addGPS = true;
    } else if (outX < 1025) {
        lblFontSize = 14;
        titleFontSize = 16;
        addLegend = true;
        addGPS = true;
    } else {
        lblFontSize = 24;
        titleFontSize = 32;
        addLegend = true;
        addGPS = true;
    }

    String title;
    String utcDate = TimeAndDate.gpsAsUtcString(startGPS);
    String hrTime = TimeAndDate.hrTime(duration);
    String cname;
    if (useTestData) {
        cname = String.format("Testing at %1$.f Hz", sampleRate);
    } else {
        cname = channelName;
    }
    title = String.format("%1$s %2$s UTC (%3$,d) t=%4$s", cname, utcDate, startGPS, hrTime);

    lblFont = new Font("Liberation Sans", Font.PLAIN, lblFontSize);
    titleFont = new Font("Liberation Serif", Font.BOLD, titleFontSize);
    FontMetrics titleMetrics = grph.getFontMetrics(titleFont);
    labelMetrics = grph.getFontMetrics(lblFont);

    titleHeight = titleMetrics.getHeight();
    int titleWidth = titleMetrics.stringWidth(title);

    while (titleWidth > outX && lblFontSize > 7) {
        titleFontSize--;
        lblFontSize--;
        lblFont = new Font("Dialog", Font.PLAIN, lblFontSize);
        titleFont = new Font("Serif", Font.BOLD, titleFontSize);
        titleMetrics = grph.getFontMetrics(titleFont);
        labelMetrics = grph.getFontMetrics(lblFont);

        titleHeight = titleMetrics.getHeight();
        titleWidth = titleMetrics.stringWidth(title);

    }

    imgY0 = titleHeight + titleFontSize / 2;

    lblHeight = labelMetrics.getHeight();
    int lblWidth = Integer.MIN_VALUE;
    for (String lbl : bitNames) {
        int lw = labelMetrics.stringWidth(lbl);
        lblWidth = Math.max(lblWidth, lw);
    }
    imgX0 = lblWidth + 10;
    dimX = outX - imgX0 - 5;
    int bLblLines = addLegend ? 4 : 3;
    bLblLines = addGPS ? bLblLines + 1 : bLblLines;

    dimY = outY - imgY0 - lblHeight * bLblLines;

    int nsamples = (int) (duration * sampleRate);
    colPerSample = 1;

    if (nsamples < dimX) {
        // we have more pixels available than we have samples so making it pretty takes some work
        colPerSample = dimX / nsamples;
        dimX = colPerSample * nsamples;
    }
    sample2colFact = (double) (dimX - 1) / (double) nsamples;
    // create and clear the data buffer
    data = new byte[dimX][maxBits];
    for (int x = 0; x < dimX; x++) {
        for (int y = 0; y < maxBits; y++) {
            data[x][y] = 0;
        }
    }
    int tx;
    if (titleWidth < dimX) {
        tx = (dimX - titleWidth) / 2 + imgX0;
    } else if (titleWidth < outX) {
        tx = (outX - titleWidth) / 2;
    } else {
        tx = 0;
    }
    tx = tx < 0 ? 0 : tx;
    tx = tx + titleWidth > outX ? 0 : tx;

    int ty = 5 + titleMetrics.getAscent();
    grph.setPaint(Color.BLACK);
    grph.setFont(titleFont);
    grph.drawString(title, tx, ty);
    rast = img.getRaster();
}

From source file:com.ctsim.dmi.MainFrame.java

private void drawButtonYard(int x, int y, boolean isRequest) {
    FontMetrics metrics = g2.getFontMetrics();
    int strWidth = metrics.stringWidth("Yard");

    if (App.atpStatus == 1) {
        g2.setColor(Color.GREEN);
        g2.fillRect(x, y, bttnWidth, bttnHeight);
        g2.setColor(Color.BLACK);

    } else if (isRequest) {

        if (isFlashOn) {
            g2.setStroke(new BasicStroke(4));
            g2.setColor(Color.YELLOW);
        } else {/*from   w w w.j  av  a  2s.c  o m*/
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.LIGHT_GRAY);
        }

    } else {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(Color.LIGHT_GRAY);
    }

    g2.drawRect(x, y, bttnWidth, bttnHeight);
    g2.drawString("Yard", x + (bttnWidth / 2) - strWidth / 2, y + bttnHeight - 5 - metrics.getHeight() / 2);
}

From source file:com.ctsim.dmi.MainFrame.java

private void drawButton(String name, int status, int x, int y, boolean isRequest) {

    FontMetrics metrics = g2.getFontMetrics();
    int strWidth = metrics.stringWidth(name);
    g2.setStroke(new BasicStroke(2));

    if (App.atpStatus == status) {
        g2.setColor(Color.GREEN);
        g2.fillRect(x, y, bttnWidth, bttnHeight);
        g2.setColor(Color.BLACK);

    } else if (isRequest) {
        if (isFlashOn) {
            g2.setStroke(new BasicStroke(4));
            g2.setColor(Color.YELLOW);
        } else {/*w ww  . ja  va 2s .  c o  m*/
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.LIGHT_GRAY);
        }

    } else {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(Color.LIGHT_GRAY);
    }

    g2.drawRect(x, y, bttnWidth, bttnHeight);
    g2.drawString(name, x + (bttnWidth / 2) - strWidth / 2, y + bttnHeight - 5 - metrics.getHeight() / 2);
}

From source file:savant.view.swing.GraphPane.java

private void drawMessageHelper(Graphics2D g2, String message, Font font, int w, int h, int offset) {
    g2.setFont(font);/*ww w . j  a  v a  2 s  .  c  o  m*/
    FontMetrics metrics = g2.getFontMetrics();

    Rectangle2D stringBounds = font.getStringBounds(message, g2.getFontRenderContext());

    int preferredWidth = (int) stringBounds.getWidth() + metrics.getHeight();
    int preferredHeight = (int) stringBounds.getHeight() + metrics.getHeight();

    w = Math.min(preferredWidth, w);
    h = Math.min(preferredHeight, h);

    int x = (getWidth() - (int) stringBounds.getWidth()) / 2;
    int y = (getHeight() / 2) + ((metrics.getAscent() - metrics.getDescent()) / 2) + offset;

    g2.drawString(message, x, y);
}

From source file:org.sakaiproject.sitestats.impl.chart.ChartServiceImpl.java

private byte[] generateNoDataChart(int width, int height) {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setBackground(parseColor(M_sm.getChartBackgroundColor()));
    g2d.clearRect(0, 0, width - 1, height - 1);
    g2d.setColor(parseColor("#cccccc"));
    g2d.drawRect(0, 0, width - 1, height - 1);
    Font f = new Font("SansSerif", Font.PLAIN, 12);
    g2d.setFont(f);// www  . j  a  v  a  2 s .  c om
    FontMetrics fm = g2d.getFontMetrics(f);
    String noData = msgs.getString("no_data");
    int noDataWidth = fm.stringWidth(noData);
    int noDataHeight = fm.getHeight();
    g2d.setColor(parseColor("#555555"));
    g2d.drawString(noData, width / 2 - noDataWidth / 2, height / 2 - noDataHeight / 2 + 2);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(img, "png", out);
    } catch (IOException e) {
        LOG.warn("Error occurred while generating SiteStats chart image data", e);
    }
    return out.toByteArray();
}