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

public void paint(Graphics g) {
    BufferedImage bim;/*from  w  ww.j  a v  a2s  .c o  m*/

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    System.out.println(tp.getTransparency());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:Main.java

public void paint(Graphics g) {
    BufferedImage bim;/*from   www.  j a va 2s . c om*/

    TexturePaint tp;

    String mesg = "www.java2s.com";

    Font myFont = new Font("Lucida Regular", Font.BOLD, 72);

    Color[] colors = { Color.red, Color.blue, Color.yellow, };

    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
        g2.setPaint(colors[(i / 2) % colors.length]);
        g2.drawLine(0, i, i, 0);
        g2.drawLine(width - i, height, width, height - i);
    }
    Rectangle r = new Rectangle(0, 0, bim.getWidth(), bim.getHeight());
    tp = new TexturePaint(bim, r);
    tp.createContext(ColorModel.getRGBdefault(), new Rectangle(10, 10, 20, 20), null, null, null);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setPaint(tp);
    g2d.setFont(myFont);
    g2d.drawString(mesg, 20, 100);
}

From source file:org.eqaula.glue.faces.MethodPrinter.java

public StreamedContent print(Method method) {

    try {/*from   w  w w.  ja  va2  s. co  m*/

        if (Method.Type.SEMAPHORE.equals(method.getMethodType())) {

            // So, browser is requesting the image. Get ID value from actual request param.
            BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bufferedImg.createGraphics();
            g2.drawString(method.getTarget().getCurrentValue().toString(), 0, 10);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(bufferedImg, "png", os);
            graphicText = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
            System.out.println("---> image generated for method " + method);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return graphicText;
}

From source file:HorizontallyCenteredText.java

protected void paintHorizontallyCenteredText(Graphics2D g2, String s, float centerX, float baselineY) {
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:TextAttributesSize.java

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

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.SIZE, 40);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:SimpleAttributes.java

public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setBackground(Color.GRAY);
    g2d.clearRect(0, 0, getWidth(), getHeight());

    // String and line with default attributes
    g2d.drawString("Default Font", 10, 20);
    g2d.drawLine(10, 22, 80, 22);//w  w  w  .j a  v a 2  s  .c om

    // Change the font, foreground color, and Stroke
    g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC, 24f));
    g2d.setColor(Color.WHITE);
    g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));

    // String and line with new attributes
    g2d.drawString("New Font", 10, 50);
    g2d.drawLine(10, 57, 120, 57);
    g2d.dispose();
}

From source file:TextAttributesColor.java

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

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 2);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:TexturedText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(tp);//w  w  w  .  j a  va2s .  c om
    g2.setFont(myFont);
    g2.drawString(mesg, 20, 100);
}

From source file:TextAttributesBackground.java

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

    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.BACKGROUND, Color.LIGHT_GRAY, 2, 9);
    g2d.drawString(as1.getIterator(), 15, 60);
}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = getWidth() + "x" + getHeight();
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
    g2d.drawString(text, x, y);
    g2d.dispose();//w w w .  j  ava2  s .  c  o  m
}