Example usage for java.awt Font createGlyphVector

List of usage examples for java.awt Font createGlyphVector

Introduction

In this page you can find the example usage for java.awt Font createGlyphVector.

Prototype

public GlyphVector createGlyphVector(FontRenderContext frc, int[] glyphCodes) 

Source Link

Document

Creates a java.awt.font.GlyphVector GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);/*  w w w  .  j  av  a  2  s . c o  m*/
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}

From source file:Main.java

public static boolean isVectorFont(Font font) {
    GlyphVector gv = font.createGlyphVector(new FontRenderContext(null, true, false), "Test");
    Shape fontShape = gv.getOutline();
    Rectangle2D bounds = fontShape.getBounds2D();
    return !(bounds.getWidth() == 0 && bounds.getHeight() == 0);
}

From source file:Utils.java

public static Shape generateShapeFromText(Font font, String string) {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();

    try {/*from  w  w w . j a  va  2 s.c  o  m*/
        GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
        Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());

        return shape;
    } finally {
        g2.dispose();
    }
}

From source file:Hexagon.java

public static BufferedImage drawCodePoint(char codePoint, int width, int height, Font font, Color color) {
    BufferedImage img = createImage(width, height);
    Graphics2D g2 = img.createGraphics();
    String text = "" + codePoint;
    g2.setColor(color);/*from   w  w w . j a  v  a 2 s  .c  o m*/
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    GlyphVector gv = font.createGlyphVector(g2.getFontRenderContext(), text);
    g2.drawGlyphVector(gv, 0f, (float) gv.getGlyphMetrics(0).getBounds2D().getHeight());
    return img;
}

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 ava  2s . c om*/
}

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 www .j a v a2 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);/*from  w w  w .  j av  a2  s.c o m*/
}

From source file:RotatedText.java

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

    String s = "111111111111111111111111111111";

    Font font = new Font("Courier", Font.PLAIN, 12);
    g2d.translate(20, 20);//from w  w w .  j av  a 2 s.  c o  m

    FontRenderContext frc = g2d.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    System.out.println(length);

}

From source file:RollingText.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();
    g2.translate(40, 80);//from w ww. ja  va  2s.c om

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}

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 www.java2s.com";
    Font font = new Font("Serif", Font.PLAIN, 24);
    FontRenderContext frc = g2.getFontRenderContext();
    g2.translate(40, 80);/*from  ww w  . j  av a 2  s .  c o m*/

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    for (int i = 0; i < length; i++) {
        Point2D p = gv.getGlyphPosition(i);
        double theta = (double) i / (double) (length - 1) * Math.PI / 4;
        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
        at.rotate(theta);
        Shape glyph = gv.getGlyphOutline(i);
        Shape transformedGlyph = at.createTransformedShape(glyph);
        g2.fill(transformedGlyph);
    }
}