Example usage for java.awt Graphics2D drawString

List of usage examples for java.awt Graphics2D drawString

Introduction

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

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, float x, float y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

Usage

From source file:PrinterSettingUpDialogPrint.java

public void paintContent(Graphics2D g2D, int w, int h) {
    g2D.setFont(font);//ww  w . j  av  a  2s .  c o  m

    g2D.drawString("Java 2D", (float) (0.5 * (w - fontMetrics.stringWidth("Java 2D"))),
            (float) (0.5 * h - 1.25 * fontMetrics.getHeight()));
}

From source file:org.fife.ui.rsyntaxtextarea.SyntaxViewUtil.java

/**
 * Draws custom end-of-line markers based on whether a line ends with a CR, LF, or CRLF.
 *///  ww w  .j a v a 2s . co  m
public static void drawEOL(RSyntaxTextArea textArea, Graphics2D g, float x, float y) {
    if (textArea.getEOLMarkersVisible()) {
        g.setColor(textArea.getForegroundForTokenType(Token.WHITESPACE));
        g.setFont(textArea.getFontForTokenType(Token.WHITESPACE));

        if (textArea.getDocument() instanceof EOLPreservingRSyntaxDocument) {
            try {
                int line = textArea.getLineOfOffset(textArea.viewToModel(new Point((int) x, (int) y)));
                char[] eol = ((EOLPreservingRSyntaxDocument) textArea.getDocument()).getEOL(line);

                if (ArrayUtils.isNotEmpty(eol)) {
                    String display = "";
                    for (char c : eol) {
                        switch (c) {
                        case '\r':
                            display += "\\r";
                            break;
                        case '\n':
                            display += "\\n";
                            break;
                        }
                    }

                    g.drawString(display, x, y);
                }
            } catch (BadLocationException e) {
            }
        } else {
            g.drawString("\\n", x, y);
        }
    }
}

From source file:org.primefaces.showcase.view.multimedia.GraphicImageView.java

@PostConstruct
public void init() {
    try {//from  w w w . ja  v a2s.  c om
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.sf.webphotos.tools.Thumbnail.java

private static Image estampar(Image im) {
    try {/*ww  w  .  ja va2 s .c om*/
        Image temp = new ImageIcon(im).getImage();

        BufferedImage buf = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g2 = (Graphics2D) buf.getGraphics();

        g2.drawImage(temp, 0, 0, null);
        g2.setBackground(Color.BLUE);

        Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null));

        // aplicar texto
        if (texto != null) {
            Util.out.println("aplicando texto " + texto);

            Font fonte = new Font(txFamilia, txEstilo, txTamanho);
            g2.setFont(fonte);
            FontMetrics fm = g2.getFontMetrics(fonte);
            Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight());
            Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao);

            g2.setPaint(txCorFundo);
            g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight());
            g2.setPaint(txCorFrente);
            g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight());
        }

        // aplicar marca dagua
        if (marcadagua != null) {
            Image marca = new ImageIcon(marcadagua).getImage();
            int rule = AlphaComposite.SRC_OVER;
            float alpha = (float) mdTransparencia / 100;
            Point pos = calculaPosicao(dimensaoFoto, new Dimension(marca.getWidth(null), marca.getHeight(null)),
                    mdMargem, mdPosicao);

            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null);
        }

        g2.dispose();
        //return (Image) buf;
        return Toolkit.getDefaultToolkit().createImage(buf.getSource());
    } catch (Exception e) {
        Util.err.println("[Thumbnail.estampar]/ERRO: Inesperado - " + e.getMessage());
        e.printStackTrace(Util.err);
        return im;
    }
}

From source file:org.primefaces.examples.view.DynamicImageController.java

public DynamicImageController() {
    try {/*from  ww  w.  j a v a 2s .c  o  m*/
        //Graphic Text
        BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImg.createGraphics();
        g2.drawString("This is a text", 0, 10);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImg, "png", os);
        graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");

        //Chart
        JFreeChart jfreechart = ChartFactory.createPieChart("Turkish Cities", createDataset(), true, true,
                false);
        File chartFile = new File("dynamichart");
        ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
        chart = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");

        //Barcode
        File barcodeFile = new File("dynamicbarcode");
        BarcodeImageHandler.saveJPEG(BarcodeFactory.createCode128("PRIMEFACES"), barcodeFile);
        barcode = new DefaultStreamedContent(new FileInputStream(barcodeFile), "image/jpeg");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform at = new AffineTransform();
    at.setToRotation(-Math.PI / 2.0, getWidth() / 2.0, getHeight() / 2.0);
    g2d.setTransform(at);//from w  w w  .  java 2s . com
    g2d.drawString("Vertical text", 10, 10);
}

From source file:DrawStringDemo.java

public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 40);
    graphics2D.setFont(font);/*w  ww . j av a  2 s  . c o  m*/
    graphics2D.drawString(message, 50, 75);
}

From source file:AlphaCompositeSRCOUT.java

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

    int w = getSize().width;
    int h = getSize().height;

    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();

    ac = AlphaComposite.getInstance(compositeRule, alphaValue);

    big.setColor(Color.red);//from  w  ww .  j  a  v a 2 s.c  o  m
    big.drawString("Destination", w / 4, h / 4);
    big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));

    big.setColor(Color.blue);
    big.drawString("Source", 3 * w / 4, h / 4);

    big.setComposite(ac);
    big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));

    g2D.drawImage(bi, null, 0, 0);
}

From source file:juicebox.tools.utils.juicer.apa.APAPlotter.java

/**
 * Plot number value axis for color scale bar.
 *
 * @param g2      graphics2D object//from  w  w w.j  a v a 2s  . c o  m
 * @param heatMap object
 */
private static void plotColorScaleValues(Graphics2D g2, HeatChart heatMap) {
    // size, increment calculations
    double valIncrement = Math.max(heatMap.getDataRange() / ((double) numDivisions), epsilon);
    double depthIncrement = ((double) (fullHeight - 2 * colorScaleVerticalMargin)) / ((double) numDivisions);
    int verticalDepth = fullHeight - colorScaleVerticalMargin;
    int csBarRightEdgeX = fullWidth - colorScaleHorizontalMargin - extraWidthBuffer;

    // formatting
    g2.setFont(heatMap.getAxisValuesFont());
    DecimalFormat df = new DecimalFormat("0.#");

    // draw each tick mark and its value
    for (double i = heatMap.getLowValue(); i <= heatMap
            .getHighValue(); i += valIncrement, verticalDepth -= depthIncrement) {

        if (i > heatMap.getHighValue() - epsilon)
            verticalDepth = colorScaleVerticalMargin;
        g2.drawString(df.format(i), csBarRightEdgeX + 5, verticalDepth); // value
        g2.drawLine(csBarRightEdgeX - 5, verticalDepth, csBarRightEdgeX, verticalDepth); // tick mark
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 96);
    g2.setFont(font);/*from  w ww .j a  va 2  s . c  om*/

    g2.drawString("java2s.com", 40, 120);
}