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:ShowOff.java

protected void drawText(Graphics2D g2) {
    FontRenderContext frc = g2.getFontRenderContext();
    mLayout = new TextLayout(mMessage, mFont, frc);
    int width = getSize().width;
    int height = getSize().height;
    Rectangle2D bounds = mLayout.getBounds();
    double x = (width - bounds.getWidth()) / 2;
    double y = height - bounds.getHeight();
    drawString(g2, x, y, 0);/*from   www  .  ja v a2s.c o m*/
    drawString(g2, width - bounds.getHeight(), y, -Math.PI / 2);
}

From source file:BasicShapes.java

License:asdf

void drawParagraph(Graphics2D g, String paragraph, float width) {
    LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph).getIterator(),
            g.getFontRenderContext());

    float y = 0.0f;
    while (linebreaker.getPosition() < paragraph.length()) {
        TextLayout tl = linebreaker.nextLayout(width);

        y += tl.getAscent();/*from  w ww.  j a  v  a 2  s.  c  o m*/
        tl.draw(g, 0, y);
        y += tl.getDescent() + tl.getLeading();
    }
}

From source file:Main.java

License:asdf

void drawParagraph(Graphics2D g, String paragraph, float width) {
    LineBreakMeasurer linebreaker = new LineBreakMeasurer(new AttributedString(paragraph).getIterator(),
            g.getFontRenderContext());

    int y = 0;/*from   ww w.  j  a va2 s  .c o m*/
    while (linebreaker.getPosition() < paragraph.length()) {
        TextLayout textLayout = linebreaker.nextLayout(width);

        y += textLayout.getAscent();
        textLayout.draw(g, 0, y);
        y += textLayout.getDescent() + textLayout.getLeading();
    }
}

From source file:TextFormat.java

/**
 * Lazy evaluation of the List of TextLayout objects corresponding to this
 * MText. Some things are approximations!
 *///w  ww  .  ja  v  a  2s.c  om
private void getLayouts(Graphics g) {
    layouts = new ArrayList();

    Point pen = new Point(10, 20);
    Graphics2D g2d = (Graphics2D) g;
    FontRenderContext frc = g2d.getFontRenderContext();

    AttributedString attrStr = new AttributedString(text);
    attrStr.addAttribute(TextAttribute.FONT, font, 0, text.length());
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
    float wrappingWidth;

    wrappingWidth = getSize().width - 15;

    while (measurer.getPosition() < text.length()) {
        TextLayout layout = measurer.nextLayout(wrappingWidth);
        layouts.add(layout);
    }
}

From source file:PrintTest.java

/**
 * This method draws the page both on the screen and the printer graphics context.
 * @param g2 the graphics context/*from  w ww  . j  av a2s  .  c  o  m*/
 */
public void drawPage(Graphics2D g2) {
    FontRenderContext context = g2.getFontRenderContext();
    Font f = new Font("Serif", Font.PLAIN, 72);
    GeneralPath clipShape = new GeneralPath();

    TextLayout layout = new TextLayout("Hello", f, context);
    AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
    Shape outline = layout.getOutline(transform);
    clipShape.append(outline, false);

    layout = new TextLayout("World", f, context);
    transform = AffineTransform.getTranslateInstance(0, 144);
    outline = layout.getOutline(transform);
    clipShape.append(outline, false);

    g2.draw(clipShape);
    g2.clip(clipShape);

    final int NLINES = 50;
    Point2D p = new Point2D.Double(0, 0);
    for (int i = 0; i < NLINES; i++) {
        double x = (2 * getWidth() * i) / NLINES;
        double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
        Point2D q = new Point2D.Double(x, y);
        g2.draw(new Line2D.Double(p, q));
    }
}

From source file:ShowOff.java

protected float drawBoxedString(Graphics2D g2, String s, Color c1, Color c2, double x) {
    FontRenderContext frc = g2.getFontRenderContext();
    TextLayout subLayout = new TextLayout(s, mFont, frc);
    float advance = subLayout.getAdvance();

    GradientPaint gradient = new GradientPaint((float) x, 0, c1, (float) (x + advance), 0, c2);
    g2.setPaint(gradient);/*from   ww  w  .  j  av a2s.com*/
    Rectangle2D bounds = mLayout.getBounds();
    Rectangle2D back = new Rectangle2D.Double(x, 0, advance, bounds.getHeight());
    g2.fill(back);

    g2.setPaint(Color.white);
    g2.setFont(mFont);
    g2.drawString(s, (float) x, (float) -bounds.getY());
    return advance;
}

From source file:MainClass.java

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

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

    g2.setFont(new Font("Serif", Font.PLAIN, 48));

    FontRenderContext frc = g2.getFontRenderContext();
    String s = "www.java2s.com";
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    int centerX = 100;
    int baselineY = 70;
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);/*from  w w w. j av  a  2  s.c  o m*/
}

From source file:MainClass.java

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

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

    String s = "www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);/*  w w  w.j  av  a 2 s.c  o m*/
}

From source file:SimpleFont.java

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

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

    String s = "Java Source and Support";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    g2.drawGlyphVector(gv, 40, 60);//from w  w  w  .j  av  a  2  s  . co  m
}