Example usage for java.awt Graphics2D setFont

List of usage examples for java.awt Graphics2D setFont

Introduction

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

Prototype

public abstract void setFont(Font font);

Source Link

Document

Sets this graphics context's font to the specified font.

Usage

From source file:Text.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2d.setRenderingHints(rh);//  ww  w .jav  a 2  s .c o m
    Font font = new Font("New Roma", Font.BOLD, 40);
    g2d.setFont(font);
    g2d.drawString("text", 20, 30);
}

From source file:FontDerivation.java

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

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

    // Create a 1-point font.
    Font font = new Font("Serif", Font.PLAIN, 1);
    float x = 20, y = 20;

    Font font24 = font.deriveFont(24.0f);
    g2.setFont(font24);
    g2.drawString("font.deriveFont(24.0f)", x, y += 30);

    Font font24italic = font24.deriveFont(Font.ITALIC);
    g2.setFont(font24italic);/*  w  w w.  j  a v a2s  . c om*/
    g2.drawString("font24.deriveFont(Font.ITALIC)", x, y += 30);

    AffineTransform at = new AffineTransform();
    at.shear(.2, 0);
    Font font24shear = font24.deriveFont(at);
    g2.setFont(font24shear);
    g2.drawString("font24.deriveFont(at)", x, y += 30);

    Hashtable attributes = new Hashtable();
    attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    Font font24bold = font24.deriveFont(attributes);
    g2.setFont(font24bold);
    g2.drawString("font24.deriveFont(attributes)", x, y += 30);
}

From source file:costumetrade.common.verify.ImageVerification.java

public Pair<String, BufferedImage> create() {
    int x = 0, fontHeight = 0, codeY = 0;
    int red = 0, green = 0, blue = 0;

    x = width / (codeCount + 2);//?
    fontHeight = height - 2;//
    codeY = height - 4;//from w w  w.j  ava2 s. c o  m

    // ?buffer
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = buffImg.createGraphics();
    // ?
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    // 
    g.setFont(new Font("Arial", Font.PLAIN, fontHeight));

    for (int i = 0; i < lineCount; i++) {

        int xs = RandomUtils.nextInt(0, width);
        int ys = RandomUtils.nextInt(0, height);
        int xe = xs + RandomUtils.nextInt(0, width / 8);
        int ye = ys + RandomUtils.nextInt(0, height / 8);
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawLine(xs, ys, xe, ye);
    }

    // randomCode???
    StringBuilder randomCode = new StringBuilder();
    // ?codeCount??
    for (int i = 0; i < codeCount; i++) {
        String strRand = String.valueOf(codeSequence[RandomUtils.nextInt(0, codeSequence.length)]);
        // ????
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawString(strRand, (i + 1) * x, codeY);
        // ??
        randomCode.append(strRand);
    }
    // ????Session
    return new Pair<String, BufferedImage>(randomCode.toString(), buffImg);
}

From source file:org.kuali.mobility.people.service.PeopleServiceImpl.java

@Override
public BufferedImage generateObfuscatedImage(String text) {
    int width = 250;
    int height = 25;

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = bufferedImage.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 14);
    g2d.setFont(font);

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g2d.setRenderingHints(rh);//from   www. j a v a2s  .  com

    Paint bg = new Color(255, 255, 255);
    g2d.setPaint(bg);
    g2d.fillRect(0, 0, width, height);

    int x = 0;
    int y = height - 7;

    Paint textPaint = new Color(0, 0, 0);
    g2d.setPaint(textPaint);
    g2d.drawString(text, x, y);

    g2d.dispose();
    return bufferedImage;
}

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);//from  w w w  .  ja  va2s.  c o  m

    // 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:TextRendering.java

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

    Dimension d = getSize();/*from w  ww .  j  ava  2  s.  co  m*/
    AffineTransform ct = AffineTransform.getTranslateInstance(d.width / 2, d.height * 3 / 4);
    g2.transform(ct);

    String s = "www.java2s.com";
    Font f = new Font("Serif", Font.PLAIN, 128);
    g2.setFont(f);

    int count = 6;
    for (int i = 1; i <= count; i++) {
        AffineTransform oldTransform = g2.getTransform();

        float ratio = (float) i / (float) count;
        g2.transform(AffineTransform.getRotateInstance(Math.PI * (ratio - 1.0f)));
        float alpha = ((i == count) ? 1.0f : ratio / 3);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2.drawString(s, 0, 0);

        g2.setTransform(oldTransform);
    }
}

From source file:MainClass.java

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

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

    Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
    g2.setPaint(Color.red);//from   w  w  w .j  a  v  a2 s .c  om
    g2.fill(r);

    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2.setComposite(c);

    g2.setPaint(Color.blue);
    g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    g2.drawString("www.java2s.com", 25, 130);
}

From source file:RenderQualityTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(hints);/*from  ww w  . j  a  v a  2 s . c o m*/

    g2.draw(new Ellipse2D.Double(10, 10, 60, 50));
    g2.setFont(new Font("Serif", Font.ITALIC, 40));
    g2.drawString("Hello", 75, 50);

    g2.draw(new Rectangle2D.Double(200, 10, 40, 40));
    g2.draw(new Line2D.Double(201, 11, 239, 49));

    g2.drawImage(image, 250, 10, 100, 100, null);
}

From source file:c.depthchart.ViewerPanel.java

private void writeStats(Graphics2D g2)
/* write statistics in bottom-left corner, or
   "Loading" at start time *//*from  www  .j  av a 2 s.  c  om*/
{
    g2.setColor(Color.BLUE);
    g2.setFont(msgFont);
    int panelHeight = getHeight();
    if (imageCount > 0) {
        double avgGrabTime = (double) totalTime / imageCount;
        g2.drawString("Pic " + imageCount + "  " + df.format(avgGrabTime) + " ms", 5, panelHeight - 10); // bottom left
    } else // no image yet
        g2.drawString("Loading...", 5, panelHeight - 10);
}

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);//from   w w  w  .  j  av  a 2 s  . c om
    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;
}