Example usage for java.awt.font TextLayout TextLayout

List of usage examples for java.awt.font TextLayout TextLayout

Introduction

In this page you can find the example usage for java.awt.font TextLayout TextLayout.

Prototype

public TextLayout(String string, Map<? extends Attribute, ?> attributes, FontRenderContext frc) 

Source Link

Document

Constructs a TextLayout from a String and an attribute set.

Usage

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

/**
 * This method draws the page both on the screen and the printer graphics context.
 * @param g2 the graphics context/* www.j  a  v  a  2  s . co  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:com.neophob.sematrix.core.generator.Textwriter.java

/**
 * create image./*from  www.  ja  v a2 s  .  c  om*/
 *
 * @param text the text
 */
public void createTextImage(String text) {
    //only load if needed
    if (StringUtils.equals(text, this.text)) {
        return;
    }

    this.text = text;

    BufferedImage img = new BufferedImage(TEXT_BUFFER_X_SIZE, internalBufferYSize, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = img.createGraphics();
    FontRenderContext frc = g2.getFontRenderContext();
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D rect = layout.getBounds();

    int h = (int) (0.5f + rect.getHeight());
    //head and tailing space
    maxXPos = (int) (0.5f + rect.getWidth()) + 2 * internalBufferXSize;
    int ypos = internalBufferYSize - (internalBufferYSize - h) / 2;

    img = new BufferedImage(maxXPos, internalBufferYSize, BufferedImage.TYPE_BYTE_GRAY);
    g2 = img.createGraphics();

    g2.setColor(new Color(128));
    g2.setFont(font);
    g2.setClip(0, 0, maxXPos, internalBufferYSize);

    g2.drawString(text, internalBufferXSize, ypos);
    DataBufferByte dbi = (DataBufferByte) img.getRaster().getDataBuffer();
    byte[] textBuffer = dbi.getData();
    g2.dispose();

    xofs = 0;

    textAsImage = new int[maxXPos * internalBufferYSize];
    for (int i = 0; i < textAsImage.length; i++) {
        if (textBuffer[i] > 10) {
            textAsImage[i] = 127;
        } else {
            textAsImage[i] = 0;
        }
    }

    //clear internalbuffer
    Arrays.fill(this.internalBuffer, 0);
}

From source file:ImageOps.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    int w = getSize().width;
    int h = getSize().height;

    g2.setColor(Color.black);/*from www .ja v a 2 s  . c  om*/
    float[][] data = { { 0.1f, 0.1f, 0.1f, // low-pass filter
            0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f }, SHARPEN3x3_3 };

    String theDesc[] = { "Convolve LowPass", "Convolve Sharpen", "LookupOp", "RescaleOp" };
    for (int i = 0; i < bi.length; i++) {
        int iw = bi[i].getWidth(this);
        int ih = bi[i].getHeight(this);
        int x = 0, y = 0;

        AffineTransform at = new AffineTransform();
        at.scale((w - 14) / 2.0 / iw, (h - 34) / 2.0 / ih);

        BufferedImageOp biop = null;
        BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);

        switch (i) {
        case 0:
        case 1:
            x = i == 0 ? 5 : w / 2 + 3;
            y = 15;
            Kernel kernel = new Kernel(3, 3, data[i]);
            ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            cop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            break;
        case 2:
            x = 5;
            y = h / 2 + 15;
            byte chlut[] = new byte[256];
            for (int j = 0; j < 200; j++)
                chlut[j] = (byte) (256 - j);
            ByteLookupTable blut = new ByteLookupTable(0, chlut);
            LookupOp lop = new LookupOp(blut, null);
            lop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
            break;
        case 3:
            x = w / 2 + 3;
            y = h / 2 + 15;
            RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
            rop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        }
        g2.drawImage(bimg, biop, x, y);
        TextLayout tl = new TextLayout(theDesc[i], g2.getFont(), g2.getFontRenderContext());
        tl.draw(g2, (float) x, (float) y - 4);
    }
}

From source file:AnimatedMessagePanel.java

public void printMessages() {

    try {//from w  w  w  .j  a  v a2s .  c  om
        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) {
    }
}

From source file:BookTest.java

public void drawPage(Graphics2D g2, PageFormat pf, int page) {
    if (message.equals(""))
        return;// w  ww  .j av a  2s . co  m
    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:net.fenyo.gnetwatch.GUI.BasicComponent.java

/**
 * Computes new margins.//from ww w .  j ava 2  s  .  c  om
 * @param none.
 * @return void.
 */
// AWT thread
// lock survey: sync_update << sync_value_per_vinterval << HERE
//              synchro << sync_tree << sync_update << sync_value_per_vinterval << events << HERE
//              sync_value_per_vinterval << HERE
private void setMargin() {
    /*
    final String left_values_str = "" + (int) (value_per_vinterval *
        (1 + (dimension.height - axis_margin_top - axis_margin_bottom) / pixels_per_vinterval));
    */
    final String left_values_str = "1000M";
    final TextLayout layout = new TextLayout(left_values_str, backing_g.getFont(),
            backing_g.getFontRenderContext());
    final Rectangle2D bounds = layout.getBounds();
    axis_margin_left = (int) bounds.getWidth() + 5 + 10;
}

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);// w ww.  ja  v a2s.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:piramide.interaction.reasoner.FuzzyReasonerWizardFacade.java

private BufferedImage createErrorMessagesImage(String text) {
    final Font font = TextTitle.DEFAULT_FONT;
    final Font bigBold = new Font(font.getName(), Font.BOLD, 24);
    final FontRenderContext frc = new FontRenderContext(null, true, false);
    final TextLayout layout = new TextLayout(text, bigBold, frc);
    final Rectangle2D bounds = layout.getBounds();
    final int w = (int) Math.ceil(bounds.getWidth());
    final int h = (int) Math.ceil(bounds.getHeight());
    final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    final Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);/* w  ww.ja  va2s  .c  o  m*/
    g.fillRect(0, 0, w, h);
    g.setColor(Color.RED);
    g.setFont(bigBold);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();
    return image;
}

From source file:com.atlassian.theplugin.idea.bamboo.tree.BuildTreeNode.java

private void recalculateColumnWidths(final BuildListModel buildModel) {
    JLabel l = new JLabel();

    reasonWidth = 0.0;/*from   www  . j a v a2s. co  m*/
    serverWidth = 0.0;
    dateWidth = 0.0;
    planInProgressWidth = 0.0;

    for (BambooBuildAdapter b : buildModel.getBuilds()) {
        // PL-1202 - argument to TextLayout must be a non-empty string
        String reason = getBuildReasonString(b);
        TextLayout layoutStatus = new TextLayout(reason.length() > 0 ? reason : ".", l.getFont(),
                new FontRenderContext(null, true, true));
        reasonWidth = Math.max(layoutStatus.getBounds().getWidth(), reasonWidth);
        String server = getBuildServerString(b);
        TextLayout layoutName = new TextLayout(server.length() > 0 ? server : ".", l.getFont(),
                new FontRenderContext(null, true, true));
        serverWidth = Math.max(layoutName.getBounds().getWidth(), serverWidth);
        String date = getRelativeBuildTimeString(b);
        TextLayout layoutDate = new TextLayout(date.length() > 0 ? date : ".", l.getFont(),
                new FontRenderContext(null, true, true));
        dateWidth = Math.max(layoutDate.getBounds().getWidth(), dateWidth);

        TextLayout planStatus = new TextLayout(
                build.getPlanStateString().length() > 0 ? build.getPlanStateString() : " ", l.getFont(),
                new FontRenderContext(null, true, true));
        planInProgressWidth = Math.max(planStatus.getBounds().getWidth(), planInProgressWidth);
    }
}