Example usage for java.awt Graphics2D getFontRenderContext

List of usage examples for java.awt Graphics2D getFontRenderContext

Introduction

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

Prototype


public abstract FontRenderContext getFontRenderContext();

Source Link

Document

Get the rendering context of the Font within this Graphics2D context.

Usage

From source file:BookTest.java

/**
 * Gets the page count of this section./*  w  ww .  ja va 2 s .  c o  m*/
 * @param g2 the graphics context
 * @param pf the page format
 * @return the number of pages needed
 */
public int getPageCount(Graphics2D g2, PageFormat pf) {
    if (message.equals(""))
        return 0;
    FontRenderContext context = g2.getFontRenderContext();
    Font f = new Font("Serif", Font.PLAIN, 72);
    Rectangle2D bounds = f.getStringBounds(message, context);
    scale = pf.getImageableHeight() / bounds.getHeight();
    double width = scale * bounds.getWidth();
    int pages = (int) Math.ceil(width / pf.getImageableWidth());
    return pages;
}

From source file:Highlights.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    String s = "Drag the text to highlight Java Source and Support.";
    Font font = new Font("Serif", Font.PLAIN, 32);

    if (textLayout == null) {
        FontRenderContext frc = g2.getFontRenderContext();
        textLayout = new TextLayout(s, font, frc);
    }//from  ww w  . java2 s  .com

    // Draw the highlight.
    if (firstHit != null && secondHit != null) {
        Shape base = textLayout.getLogicalHighlightShape(firstHit.getInsertionIndex(),
                secondHit.getInsertionIndex());
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        Shape highlight = at.createTransformedShape(base);
        g2.setPaint(Color.white);
        g2.fill(highlight);
    }

    g2.setPaint(Color.black);
    textLayout.draw(g2, x, y);
}

From source file:Bouncer.java

protected void setClip(Graphics2D g2) {
    if (mClip == false)
        return;/*from w  w  w .j ava2s  .  c o  m*/
    if (mClipShape == null) {
        Dimension d = getSize();
        FontRenderContext frc = g2.getFontRenderContext();
        Font font = new Font("Serif", Font.PLAIN, 144);
        String s = "Java Source and Support!";
        GlyphVector gv = font.createGlyphVector(frc, s);
        Rectangle2D bounds = font.getStringBounds(s, frc);
        mClipShape = gv.getOutline((d.width - (float) bounds.getWidth()) / 2,
                (d.height + (float) bounds.getHeight()) / 2);
    }
    g2.clip(mClipShape);
}

From source file:BookTest.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page >= 1)
        return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.black);//from   w w w.j  a v  a 2s.  c o m
    g2.translate(pf.getImageableX(), pf.getImageableY());
    FontRenderContext context = g2.getFontRenderContext();
    Font f = g2.getFont();
    TextLayout layout = new TextLayout(title, f, context);
    float ascent = layout.getAscent();
    g2.drawString(title, 0, ascent);
    return Printable.PAGE_EXISTS;
}

From source file:com.alibaba.simpleimage.render.FootnoteDrawTextItem.java

@Override
public void drawText(Graphics2D graphics, int width, int height) {
    if (StringUtils.isBlank(text) || StringUtils.isBlank(domainName)) {
        return;//  w  w w  .jav a2 s. c o  m
    }

    int x = 0, y = 0;
    int fontsize = ((int) (width * textWidthPercent)) / domainName.length();
    if (fontsize < minFontSize) {
        return;
    }

    float fsize = (float) fontsize;
    Font font = domainFont.deriveFont(fsize);
    graphics.setFont(font);
    FontRenderContext context = graphics.getFontRenderContext();
    int sw = (int) font.getStringBounds(domainName, context).getWidth();

    x = width - sw - fontsize;
    y = height - fontsize;
    if (x <= 0 || y <= 0) {
        return;
    }

    if (fontShadowColor != null) {
        graphics.setColor(fontShadowColor);
        graphics.drawString(domainName, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize));
    }
    graphics.setColor(fontColor);
    graphics.drawString(domainName, x, y);

    //draw company name
    fsize = (float) fontsize;
    font = defaultFont.deriveFont(fsize);
    graphics.setFont(font);
    context = graphics.getFontRenderContext();
    sw = (int) font.getStringBounds(text, context).getWidth();
    x = width - sw - fontsize;
    y = height - (int) (fontsize * 2.5);
    if (x <= 0 || y <= 0) {
        return;
    }

    if (fontShadowColor != null) {
        graphics.setColor(fontShadowColor);
        graphics.drawString(text, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize));
    }
    graphics.setColor(fontColor);
    graphics.drawString(text, x, y);
}

From source file:com.piketec.jenkins.plugins.tpt.publisher.PieChart.java

private void renderLegend(Graphics2D g2) {
    g2.setFont(NORMALFONT.deriveFont(32f).deriveFont(Font.BOLD));

    // erst die Breite der Zahlen fuer die Einrueckung berechnen
    Font font = g2.getFont();/*  w w w .j  a  v  a 2s.c  om*/
    FontRenderContext fontRenderContext = g2.getFontRenderContext();
    Map<Segment, Double> numberWidths = new HashMap<>();
    double maxNumberWidth = 0;
    for (Segment seg : segments) {
        double numberWidth = font
                .getStringBounds(legendPortionFormat.format(seg.getPortion()), fontRenderContext).getWidth();
        numberWidths.put(seg, numberWidth);
        maxNumberWidth = Math.max(maxNumberWidth, numberWidth);
    }
    if (showTotalInLegend) {
        double numberWidth = font.getStringBounds(legendPortionFormat.format(total), fontRenderContext)
                .getWidth();
        numberWidths.put(null, numberWidth);
        maxNumberWidth = Math.max(maxNumberWidth, numberWidth);
    }

    // jetzt die Zeilen in die Legende malen
    int verticalOffset = 0;
    for (int row = 0; row < segments.size(); ++row) {
        Segment seg = getLegendSegment(row);
        // Zahlen rechtsbuendig
        double indentation = maxNumberWidth - numberWidths.get(seg);
        String segmentPortionNumber = legendPortionFormat.format(seg.getPortion());
        String segmentText = seg.getText();
        Color segmentColor = seg.getColor();
        String subSegmentText = seg.getSubSegmentText();
        String subNumberText = legendPortionFormat.format(seg.subPortion);
        drawLegendLine(g2, verticalOffset, indentation, segmentColor, segmentText, segmentPortionNumber,
                seg.getPortion() != 1d, seg.getSubPortion() > 0, subSegmentText, subNumberText,
                seg.getSubPortion() != 1d);
        verticalOffset += 85;
    }
    if (showTotalInLegend) {
        double indentation = maxNumberWidth - numberWidths.get(null);
        String subNumberText = legendPortionFormat.format(subTotal);
        drawLegendLine(g2, verticalOffset, indentation, null, totalText, legendPortionFormat.format(total),
                total != 1, subTotalTextOrNull != null, subTotalTextOrNull, subNumberText, subTotal != 1);
    }
}

From source file:BookTest.java

public void drawPage(Graphics2D g2, PageFormat pf, int page) {
    if (message.equals(""))
        return;//from  ww w. jav a  2s . c  om
    page--; // account for cover page

    drawCropMarks(g2, pf);
    g2.clip(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf.getImageableHeight()));
    g2.translate(-page * pf.getImageableWidth(), 0);
    g2.scale(scale, scale);
    FontRenderContext context = g2.getFontRenderContext();
    Font f = new Font("Serif", Font.PLAIN, 72);
    TextLayout layout = new TextLayout(message, f, context);
    AffineTransform transform = AffineTransform.getTranslateInstance(0, layout.getAscent());
    Shape outline = layout.getOutline(transform);
    g2.draw(outline);
}

From source file:TextLayoutWithCarets.java

private void initialize(Graphics2D g2) {
    String s = "Java Source and Support.";
    // Create a plain and italic font.
    int fontSize = 32;
    Font font = new Font("Lucida Sans Regular", Font.PLAIN, fontSize);
    Font italicFont = new Font("Lucida Sans Oblique", Font.ITALIC, fontSize);
    // Create an Attributed String
    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, font);
    as.addAttribute(TextAttribute.FONT, italicFont, 2, 5);
    // Get the iterator.
    AttributedCharacterIterator iterator = as.getIterator();
    // Create a TextLayout.
    FontRenderContext frc = g2.getFontRenderContext();
    mLayout = new TextLayout(iterator, frc);

    mHit = mLayout.getNextLeftHit(1);//from  w  w w  . j av  a 2 s . com

    // Respond to left and right arrow keys.

    mInitialized = true;
}

From source file:com.alibaba.simpleimage.render.CornerDrawTextItem.java

@Override
public void drawText(Graphics2D graphics, int width, int height) {
    if (StringUtils.isBlank(text)) {
        return;//from w  ww  .jav a2s  . c  o  m
    }

    int x = 0, y = 0;
    // ?
    int textLength = (int) (width * textWidthPercent);
    // ??
    int fontsize = textLength / text.length();
    // ?.....?
    if (fontsize < minFontSize) {
        return;
    }

    float fsize = (float) fontsize;
    Font font = defaultFont.deriveFont(fsize);
    graphics.setFont(font);
    FontRenderContext context = graphics.getFontRenderContext();
    int sw = (int) font.getStringBounds(text, context).getWidth();

    // ??
    if (width > height) {
        y = height / 4;
    } else {
        y = width / 4;
    }

    int halflen = sw / 2;
    if (halflen <= (y - fontsize)) {
        x = y - halflen;
    } else {
        x = fontsize;
    }

    if (x <= 0 || y <= 0) {
        return;
    }

    if (fontShadowColor != null) {
        graphics.setColor(fontShadowColor);
        graphics.drawString(text, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize));
    }
    graphics.setColor(fontColor);
    graphics.drawString(text, x, y);

    if (width > height) {
        y = height - (height / 4);
    } else {
        y = height - (width / 4);
    }

    halflen = sw / 2;
    if (halflen <= (height - y - fontsize)) {
        x = width - (height - y) - halflen;
    } else {
        x = width - sw - fontsize;
    }

    if (x <= 0 || y <= 0) {
        return;
    }

    if (fontShadowColor != null) {
        graphics.setColor(fontShadowColor);
        graphics.drawString(text, x + getShadowTranslation(fontsize), y + getShadowTranslation(fontsize));
    }
    graphics.setColor(fontColor);
    graphics.drawString(text, x, y);

}

From source file:AnimatedMessagePanel.java

public void printMessages() {

    try {//  w  w w.  j a v a2 s. c o m
        Runnable runner = new Runnable() {
            public void run() {

                AnimatedMessagePanel.getInstance().setCurrentlyScrolling(true);
                Graphics2D g2 = (Graphics2D) og;

                int linecount = 1;
                StringTokenizer st1 = new StringTokenizer(messageQue[0]);
                String text1 = "";
                String testtext1 = "";
                String prodtext1 = "";
                while (st1.hasMoreTokens()) {
                    text1 = st1.nextToken();
                    testtext1 += text1 + " ";
                    FontRenderContext frc1 = g2.getFontRenderContext();
                    TextLayout t11 = new TextLayout(testtext1, font, frc1);
                    int sw1 = (int) t11.getBounds().getWidth();
                    if (sw1 > (getWidth() - 40)) {
                        linecount++;
                        testtext1 = "";
                        prodtext1 = text1;
                    } else
                        prodtext1 += text1 + " ";
                }

                for (int k = -(15) * (linecount - 1); k <= 15; k++) {
                    paintBG();
                    int y = k;
                    og.setColor(Color.black);
                    for (int j = 0; j < messageQue.length; j++) {
                        if (messageQue[j].length() != 0) {
                            StringTokenizer st = new StringTokenizer(messageQue[j]);
                            String text = "";
                            String testtext = "";
                            String prodtext = "";
                            while (st.hasMoreTokens()) {
                                text = st.nextToken();
                                testtext += text + " ";
                                FontRenderContext frc = g2.getFontRenderContext();
                                TextLayout t1 = new TextLayout(testtext, font, frc);
                                int sw = (int) t1.getBounds().getWidth();
                                if (sw > (getWidth() - 40)) {
                                    og.drawString(prodtext, 10, y);
                                    y += 12;
                                    testtext = "";
                                    prodtext = text;
                                } else
                                    prodtext += text + " ";
                            }
                            og.drawString(prodtext, 10, y);
                            y += 18;
                            if (y > getHeight())
                                break;
                        }
                    }
                    repaint();
                    try {
                        Thread.sleep(50);
                    } catch (Exception de) {
                    }
                }
                AnimatedMessagePanel.getInstance().setCurrentlyScrolling(false);
                AnimatedMessagePanel.getInstance().checkForMessagesWaiting();

            }
        };
        new Thread(runner, "printMessage.run").start();
    } catch (Exception e) {
    }
}