Example usage for java.awt Graphics2D setFont

List of usage examples for java.awt Graphics2D setFont

Introduction

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

Prototype

public abstract void setFont(Font font);

Source Link

Document

Sets this graphics context's font to the specified font.

Usage

From source file:net.geoprism.dashboard.DashboardMap.java

private BufferedImage getLegendTitleImage(DashboardLayer layer) {

    FontMetrics fm;/*from  ww  w. j  a v a 2s.  c  o m*/
    int textWidth;
    int textHeight;
    int textBoxHorizontalPadding = 4;
    int textBoxVerticalPadding = 4;
    int borderWidth = 2;
    int paddedTitleHeight;
    int paddedTitleWidth;
    int titleLeftPadding = textBoxHorizontalPadding;
    BufferedImage newLegendTitleBase;
    Graphics2D newLegendTitleBaseGraphic = null;

    try {
        // Build the Font object
        Font titleFont = new Font(layer.getName(), Font.BOLD, 14);

        // Build variables for base legend graphic construction
        try {
            newLegendTitleBase = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics();

            newLegendTitleBaseGraphic.setFont(titleFont);

            fm = newLegendTitleBaseGraphic.getFontMetrics();
            textHeight = fm.getHeight();
            textWidth = fm.stringWidth(layer.getName());

            paddedTitleWidth = textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2);

            paddedTitleHeight = textHeight + (textBoxVerticalPadding * 2) + (borderWidth * 2);
        } finally {
            // dispose of temporary graphics context
            if (newLegendTitleBaseGraphic != null) {
                newLegendTitleBaseGraphic.dispose();
            }
        }

        titleLeftPadding = ((paddedTitleWidth / 2)
                - ((textWidth + (textBoxHorizontalPadding * 2) + (borderWidth * 2)) / 2))
                + textBoxHorizontalPadding;

        newLegendTitleBase = new BufferedImage(paddedTitleWidth, paddedTitleHeight,
                BufferedImage.TYPE_INT_ARGB);
        newLegendTitleBaseGraphic = newLegendTitleBase.createGraphics();
        newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null);

        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_DITHERING,
                RenderingHints.VALUE_DITHER_ENABLE);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        newLegendTitleBaseGraphic.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                RenderingHints.VALUE_STROKE_PURE);
        newLegendTitleBaseGraphic.setFont(titleFont);

        // draw title text
        fm = newLegendTitleBaseGraphic.getFontMetrics();
        newLegendTitleBaseGraphic.setColor(Color.WHITE);
        newLegendTitleBaseGraphic.drawString(layer.getName(), titleLeftPadding,
                fm.getAscent() + textBoxVerticalPadding);

        newLegendTitleBaseGraphic.drawImage(newLegendTitleBase, 0, 0, null);
    } finally {
        if (newLegendTitleBaseGraphic != null) {
            newLegendTitleBaseGraphic.dispose();
        }
    }

    return newLegendTitleBase;
}

From source file:org.n52.v3d.terrainserver.povraywts.WebTerrainServlet.java

private void addAnnotations(BufferedImage pImage, int pHeight, int pWidth, double pPitch, double pYaw,
        boolean pDrawNorthArrow) {
    if (mCopyrightTextContent.length() > 0) {
        Graphics2D g = pImage.createGraphics();
        g.drawImage(pImage, 0, 0, null);
        g.setColor(new java.awt.Color(mCopyrightTextColor.getRed(), mCopyrightTextColor.getGreen(),
                mCopyrightTextColor.getBlue()));

        // 1. Copyright-Vermerk
        // Etwas unschn: Durch JPEG-Komprimierung wird Text (insb. bei kleiner Font-Gre) wird unscharf...
        // TODO: Abhilfe evtl. durch Hintergrund?
        Font font = new Font(mCopyrightTextFont, Font.BOLD /* Style als int, siehe ggf. API-Dok.*/,
                mCopyrightTextSize);/*  w  w w  . j  a v  a2s . co  m*/
        g.setFont(font);
        // mehrzeilige Copyright-Texte erlauben:
        StringTokenizer str = new StringTokenizer(mCopyrightTextContent, "\n");
        int spacePerRow = mCopyrightTextSize;
        int rows = str.countTokens();
        int startPos = spacePerRow * rows;
        int currRow = 0;
        while (str.hasMoreTokens()) {
            int yPos = pHeight - (startPos - (currRow * spacePerRow)) + spacePerRow / 2;
            g.drawString(str.nextToken().trim(), 5, yPos);
            currRow++;
        }

        // 2. Nordpfeil
        if (pDrawNorthArrow) {
            // Zeichenparameter:
            double radius = 35.;
            double phi = 15.;
            // Symbolkonstruktion:
            int rx = (int) radius;
            int ry = (int) Math.round(radius * Math.sin(-pPitch * Math.PI / 180.));
            int mx = pWidth - rx - 5;
            int my = pHeight - ry - 5;
            int dx = (int) (radius * Math.sin(pYaw * Math.PI / 180.));
            int dy = (int) (radius * Math.sin(-pPitch * Math.PI / 180.) * Math.cos(pYaw * Math.PI / 180.));
            int px = mx - dx, py = my - dy; // Pfeilspitze
            int qlx = mx + (int) (radius * Math.sin((pYaw + phi) * Math.PI / 180.));
            int qly = my + (int) (radius * Math.sin(-pPitch * Math.PI / 180.)
                    * Math.cos((pYaw + phi) * Math.PI / 180.));
            int qrx = mx + (int) (radius * Math.sin((pYaw - phi) * Math.PI / 180.));
            int qry = my + (int) (radius * Math.sin(-pPitch * Math.PI / 180.)
                    * Math.cos((pYaw - phi) * Math.PI / 180.));
            // Ellipse zeichnen:
            g.setStroke(new BasicStroke(2.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
            g.drawOval(mx - rx, my - ry, 2 * rx, 2 * ry);
            // Striche fr Pfeil zeichnen:

            g.setStroke(new BasicStroke(1.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));

            boolean fillArrow = true;
            if (fillArrow)
                g.fill(new Polygon(new int[] { px, qlx, qrx }, new int[] { py, qly, qry }, 3));
            else {
                g.drawLine(px, py, qlx, qly);
                g.drawLine(px, py, qrx, qry);
                g.drawLine(qlx, qly, qrx, qry);
            }
        }

        g.dispose();
    }
}

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

/**
 * Draw two read-shapes: one for forward and one for reverse.
 *///from w w w  . j ava 2s. co  m
private void drawStrandLegends(Graphics2D g2, int x, int y) {
    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);
    Shape pointyBar = getPointyBar(false, x, y - 12, 36.0, 12.0);
    g2.setColor(cs.getColor(ColourKey.FORWARD_STRAND));
    g2.fill(pointyBar);
    g2.setColor(cs.getColor(ColourKey.INTERVAL_LINE));
    g2.draw(pointyBar);

    pointyBar = getPointyBar(true, x, y - 12 + LEGEND_LINE_HEIGHT, 36.0, 12.0);
    g2.setColor(cs.getColor(ColourKey.REVERSE_STRAND));
    g2.fill(pointyBar);
    g2.setColor(cs.getColor(ColourKey.INTERVAL_LINE));
    g2.draw(pointyBar);

    g2.setColor(Color.BLACK);
    g2.setFont(LEGEND_FONT);
    g2.drawString(ColourKey.FORWARD_STRAND.getName(), x + 45, y);
    g2.drawString(ColourKey.REVERSE_STRAND.getName(), x + 45, y + LEGEND_LINE_HEIGHT);
}

From source file:org.broad.igv.variant.VariantTrack.java

/**
 * Draws the "greenbar" type background.  Also, rather bizarrely, draws the sample names.
 *
 * @param g2D//from  www .  java2  s. c o m
 * @param bandRectangle
 * @param visibleRectangle
 * @param type
 */
private void drawBackground(Graphics2D g2D, Rectangle bandRectangle, Rectangle visibleRectangle,
        BackgroundType type) {

    if (getDisplayMode() == DisplayMode.COLLAPSED) {
        return;
    }

    boolean coloredLast = true;
    Rectangle textRectangle = new Rectangle(bandRectangle);
    textRectangle.height--;

    int bandFontSize = Math.min(fontSize, (int) bandRectangle.getHeight() - 1);
    Font font = FontManager.getFont(bandFontSize);
    Font oldFont = g2D.getFont();
    g2D.setFont(font);

    if (grouped) {
        for (Map.Entry<String, List<String>> sampleGroup : samplesByGroups.entrySet()) {
            int y0 = bandRectangle.y;

            List<String> sampleList = sampleGroup.getValue();
            coloredLast = colorBand(g2D, bandRectangle, visibleRectangle, coloredLast, textRectangle,
                    sampleList, type);
            bandRectangle.y += GROUP_BORDER_WIDTH;

            if (type == BackgroundType.NAME && bandRectangle.height < 3) {
                String group = sampleGroup.getKey();
                if (group != null) {
                    g2D.setColor(Color.black);
                    g2D.setFont(oldFont);
                    int y2 = bandRectangle.y;
                    Rectangle textRect = new Rectangle(bandRectangle.x, y0, bandRectangle.width, y2 - y0);
                    GraphicUtils.drawWrappedText(group, textRect, g2D, true);
                }
            }

        }

    } else {
        coloredLast = colorBand(g2D, bandRectangle, visibleRectangle, coloredLast, textRectangle, allSamples,
                type);
    }
    g2D.setFont(oldFont);
}

From source file:edu.ku.brc.ui.UIRegistry.java

/**
 * Writes a string message into the BufferedImage on GlassPane and sets the main component's visibility to false and
 * shows the GlassPane./*from   ww  w.ja  va 2  s . co m*/
 * @param msg the message
 * @param pointSize the Font point size for the message to be writen in
 */
public static GhostGlassPane writeGlassPaneMsg(final String msg, final int pointSize) {
    GhostGlassPane glassPane = getGlassPane();
    if (glassPane != null) {
        glassPane.finishDnD();
    }

    glassPane.setMaskingEvents(true);

    Component mainComp = get(MAINPANE);
    if (mainComp != null && glassPane != null) {
        JFrame frame = (JFrame) get(FRAME);
        frameRect = frame.getBounds();

        int y = 0;
        JMenuBar menuBar = null;
        Dimension size = mainComp.getSize();
        if (UIHelper.getOSType() != UIHelper.OSTYPE.MacOSX) {
            menuBar = frame.getJMenuBar();
            size.height += menuBar.getSize().height;
            y += menuBar.getSize().height;
        }
        BufferedImage buffer = getGlassPaneBufferedImage(size.width, size.height);
        Graphics2D g2 = buffer.createGraphics();
        if (menuBar != null) {
            menuBar.paint(g2);
        }
        g2.translate(0, y);
        mainComp.paint(g2);
        g2.translate(0, -y);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(new Color(255, 255, 255, 128));
        g2.fillRect(0, 0, size.width, size.height);

        g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize));
        FontMetrics fm = g2.getFontMetrics();

        int tw = fm.stringWidth(msg);
        int th = fm.getHeight();
        int tx = (size.width - tw) / 2;
        int ty = (size.height - th) / 2;

        int expand = 20;
        int arc = expand * 2;
        g2.setColor(Color.WHITE);
        g2.fillRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.DARK_GRAY);
        g2.drawRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.BLACK);
        g2.drawString(msg, tx, ty);
        g2.dispose();

        glassPane.setImage(buffer);
        glassPane.setPoint(new Point(0, 0), GhostGlassPane.ImagePaintMode.ABSOLUTE);
        glassPane.setOffset(new Point(0, 0));

        glassPane.setVisible(true);
        mainComp.setVisible(false);

        //Using paintImmediately fixes problems with glass pane not showing, such as for workbench saves initialed
        //during workbench or app shutdown. Don't know if there is a better way to fix it.
        //glassPane.repaint();
        glassPane.paintImmediately(glassPane.getBounds());
        showingGlassPane = true;
    }

    return glassPane;
}

From source file:org.jfree.experimental.chart.plot.dial.DialValueIndicator.java

/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been 
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted). 
 */// ww w  . j  a v  a  2s.  c  o  m
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    // work out the anchor point
    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius, this.radius);
    Arc2D arc = new Arc2D.Double(f, this.angle, 0.0, Arc2D.OPEN);
    Point2D pt = arc.getStartPoint();

    // calculate the bounds of the template value
    FontMetrics fm = g2.getFontMetrics(this.font);
    String s = this.formatter.format(this.templateValue);
    Rectangle2D tb = TextUtilities.getTextBounds(s, g2, fm);

    // align this rectangle to the frameAnchor
    Rectangle2D bounds = RectangleAnchor.createRectangle(new Size2D(tb.getWidth(), tb.getHeight()), pt.getX(),
            pt.getY(), this.frameAnchor);

    // add the insets
    Rectangle2D fb = this.insets.createOutsetRectangle(bounds);

    // draw the background
    g2.setPaint(this.backgroundPaint);
    g2.fill(fb);

    // draw the border
    g2.setStroke(this.outlineStroke);
    g2.setPaint(this.outlinePaint);
    g2.draw(fb);

    // now find the text anchor point
    double value = plot.getValue(this.datasetIndex);
    String valueStr = this.formatter.format(value);
    Point2D pt2 = RectangleAnchor.coordinates(bounds, this.valueAnchor);
    g2.setPaint(this.paint);
    g2.setFont(this.font);
    TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(), (float) pt2.getY(), this.textAnchor);

}

From source file:com.AandR.beans.plotting.imagePlotPanel.CanvasPanel.java

private void drawTextBoxes(Graphics2D g2) {
    Overlay thisOverlay;//w  ww.  j  a v  a  2  s  .co  m
    Iterator<Overlay> thisBox = overlays.iterator();
    while (thisBox.hasNext()) {
        thisOverlay = thisBox.next();
        g2.setFont(thisOverlay.getFont());
        g2.setColor(thisOverlay.getShapeColor());
        thisOverlay.paintItem(g2);
    }
}

From source file:forseti.JUtil.java

public static synchronized Image generarImagenMensaje(String mensaje, String nombreFuente, int tamanioFuente) {
    Frame f = new Frame();
    f.addNotify();//from  ww  w  . j  av  a 2s. c o m
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    env.getAvailableFontFamilyNames();
    Font fuente = new Font(nombreFuente, Font.PLAIN, tamanioFuente);
    FontMetrics medidas = f.getFontMetrics(fuente);
    int anchoMensaje = medidas.stringWidth(mensaje);
    int lineaBaseX = anchoMensaje / 10;
    int ancho = anchoMensaje + 2 * (lineaBaseX + tamanioFuente);
    int alto = tamanioFuente * 7 / 2;
    int lineaBaseY = alto * 8 / 10;
    Image imagenMensaje = f.createImage(ancho, alto);
    Graphics2D g2d = (Graphics2D) imagenMensaje.getGraphics();
    g2d.setFont(fuente);
    g2d.translate(lineaBaseX, lineaBaseY);
    g2d.setPaint(Color.lightGray);
    AffineTransform origTransform = g2d.getTransform();
    g2d.shear(-0.95, 0);
    g2d.scale(1, 3);
    g2d.drawString(mensaje, 0, 0);
    g2d.setTransform(origTransform);
    g2d.setPaint(Color.black);
    g2d.drawString(mensaje, 0, 0);

    return (imagenMensaje);
}

From source file:org.multibit.viewsystem.swing.view.panels.ShowPreferencesPanel.java

private ImageIcon createImageIcon(String text) {
    Font font = new Font("Dialog", Font.PLAIN, LANGUAGE_CODE_IMAGE_HEIGHT - 2 * LANGUAGE_CODE_VERTICAL_INSET);

    BufferedImage bimg = new BufferedImage(LANGUAGE_CODE_IMAGE_WIDTH, LANGUAGE_CODE_IMAGE_HEIGHT,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bimg.createGraphics();

    g2.setColor(Color.WHITE);/*from   w ww  .ja  v a 2  s  .  c o m*/
    g2.setFont(font);
    g2.drawString(text, LANGUAGE_CODE_VERTICAL_INSET + 1,
            LANGUAGE_CODE_IMAGE_HEIGHT - 2 * LANGUAGE_CODE_VERTICAL_INSET);

    return new ImageIcon(bimg);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.graphics.internal.LogicalPageDrawable.java

protected void configureGraphics(final StyleSheet layoutContext, final Graphics2D g2) {
    final boolean bold = layoutContext.getBooleanStyleProperty(TextStyleKeys.BOLD);
    final boolean italics = layoutContext.getBooleanStyleProperty(TextStyleKeys.ITALIC);

    int style = Font.PLAIN;
    if (bold) {/*  ww w  .  j  a v  a2 s  .co  m*/
        style |= Font.BOLD;
    }
    if (italics) {
        style |= Font.ITALIC;
    }

    final Color cssColor = (Color) layoutContext.getStyleProperty(ElementStyleKeys.PAINT);
    g2.setColor(cssColor);

    final int fontSize = layoutContext.getIntStyleProperty(TextStyleKeys.FONTSIZE,
            (int) metaData.getNumericFeatureValue(OutputProcessorFeature.DEFAULT_FONT_SIZE));

    final String fontName = metaData
            .getNormalizedFontFamilyName((String) layoutContext.getStyleProperty(TextStyleKeys.FONT));
    g2.setFont(new Font(fontName, style, fontSize));
}