Example usage for java.awt Graphics setFont

List of usage examples for java.awt Graphics setFont

Introduction

In this page you can find the example usage for java.awt Graphics 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:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

    Graphics g = img.getGraphics();
    g.setColor(Color.red);/*from   ww w . j  a  v  a  2  s.co  m*/
    g.setFont(new Font("Arial", Font.BOLD, 14));
    g.drawString("Reference", 10, 80);

    int w = 100;
    int h = 100;
    int x = 1;
    int y = 1;

    int[] pixels = new int[w * h];
    PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);

    pg.grabPixels();
    BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
            bimg.setRGB(x + i, y + j, pixels[j * w + i]);
        }
    }

    FileOutputStream fout = new FileOutputStream("jpg.jpg");

    JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(fout);
    JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(bimg);

    enParam.setQuality(1.0F, true);
    jencoder.setJPEGEncodeParam(enParam);
    jencoder.encode(bimg);

    fout.close();

}

From source file:Main.java

public static void main(String[] args) {
    final int w = 20;
    final int side = 25;
    final int[][] grid = new int[50][w];

    JPanel panel = new JPanel() {
        public void paintComponent(Graphics g) {
            Font font = new Font("WingDings", Font.PLAIN, 14);
            g.setFont(font);
            int off = 0;
            for (int i = 0; i < 256 * 256; i++) {
                if (font.canDisplay((char) i)) {
                    off++;/*  ww  w.  j a  va 2 s  . co  m*/
                    grid[off / w][off % w] = i;
                    int x = off % w * side;
                    int y = (off / w) * side + side;
                    g.drawString("" + (char) i, x, y);
                }
            }
        }
    };
    JFrame frame = new JFrame();
    panel.setSize(300, 300);
    frame.getContentPane().add(panel);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final int columnCount = 10;
    final int side = 25;
    final int[][] grid = new int[50][columnCount];

    JPanel panel = new JPanel() {
        public void paintComponent(Graphics g) {
            Font font = new Font("WingDings", Font.PLAIN, 14);
            g.setFont(font);
            int off = 0;
            for (int i = 0; i < 256 * 256; i++) {
                if (font.canDisplay((char) i) == false) {
                    continue;
                }//from   w ww . ja  va  2s . com
                off++;
                grid[off / columnCount][off % columnCount] = i;
                int x = off % columnCount * side;
                int y = (off / columnCount) * side + side;
                g.drawString(Character.toString((char) i), x, y);

            }
        }
    };
    JFrame frame = new JFrame();
    panel.setSize(300, 300);
    frame.getContentPane().add(panel);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String arg[]) throws Exception {

    String yourText = "java2s.com";
    BufferedImage bufferedImage = new BufferedImage(170, 30, BufferedImage.TYPE_INT_RGB);

    Graphics graphics = bufferedImage.getGraphics();
    graphics.setColor(Color.LIGHT_GRAY);
    graphics.fillRect(0, 0, 200, 50);//from  w  w  w  .  java 2  s. c o  m
    graphics.setColor(Color.BLACK);
    graphics.setFont(new Font("Arial Black", Font.BOLD, 20));
    graphics.drawString(yourText, 10, 25);

    ImageIO.write(bufferedImage, "jpg", new File("C:/Users/image.jpg"));

    System.out.println("Image Created");
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel() {
        @Override/*from  www . ja va  2  s . c om*/
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int y = 0;
            for (int size = 4; size <= 24; size += 2) {
                g.setFont(new Font("Arial", Font.BOLD, size));
                g.drawString("Name", 0, y);
                int heightOfFont = g.getFontMetrics().getHeight();
                y += heightOfFont;
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    };
    frame.add(panel);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    frame.pack();

}

From source file:WaterMark.java

public static String execute(String src, String dest, String text, Color color, Font font) throws Exception {
    BufferedImage srcImage = ImageIO.read(new File(src));

    int width = srcImage.getWidth(null);
    int height = srcImage.getHeight(null);
    BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = destImage.getGraphics();

    g.drawImage(srcImage, 0, 0, width, height, null);
    g.setColor(color);/*from  w  w w. j av  a  2  s.  c  o  m*/
    g.setFont(font);
    g.fillRect(0, 0, 50, 50);
    g.drawString(text, width / 5, height - 10);
    g.dispose();

    ImageIO.write(destImage, DEFAULT_FORMAT, new File("dest.jpg"));
    return dest;
}

From source file:org.b3log.symphony.model.Character.java

/**
 * Creates an image with the specified content (a character).
 *
 * @param content the specified content//from   w  w w .java  2s .c o m
 * @return image
 */
public static BufferedImage createImage(final String content) {
    final BufferedImage ret = new BufferedImage(500, 500, Transparency.TRANSLUCENT);
    final Graphics g = ret.getGraphics();
    g.setClip(0, 0, 50, 50);
    g.fillRect(0, 0, 50, 50);
    g.setFont(new Font(null, Font.PLAIN, 40));
    g.setColor(Color.BLACK);
    g.drawString(content, 5, 40);
    g.dispose();

    return ret;
}

From source file:com.baidu.rigel.biplatform.ma.auth.resource.RandomValidateCode.java

private static String drowString(Graphics g, String randomString, int i) {
    g.setFont(getFont());
    g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
    String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
    randomString += rand;/*w  w w. j a v a2s  .  c om*/
    g.translate(random.nextInt(3), random.nextInt(3));
    g.drawString(rand, 13 * i, 16);
    return randomString;
}

From source file:GraphicsUtil.java

static public void drawText(Graphics g, Font font, String text, int x, int y, int halign, int valign) {
    Font oldfont = g.getFont();// www .  ja  va 2  s .c o m
    if (font != null)
        g.setFont(font);
    drawText(g, text, x, y, halign, valign);
    if (font != null)
        g.setFont(oldfont);
}

From source file:FontAlgo.java

private static TextualChar getTextualChar(char a_char) throws Throwable {
    BufferedImage bImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bImg.getGraphics();
    g.setColor(Color.green);//from  w ww .  j  a va2  s.c  o  m
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setFont(appliedFont);
    g.setColor(Color.black);
    g.drawString(new String(new char[] { a_char }), 10, g.getFontMetrics().getHeight());
    PixelGrabber p = new PixelGrabber(bImg, 0, 0, WIDTH, HEIGHT, true);

    if (p.grabPixels()) {
        char[][] pattern = new char[WIDTH][HEIGHT];
        int baseColourPixel = 0, contrastColourPixel = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0;
        int[] pixels = (int[]) p.getPixels();
        baseColourPixel = pixels[0];
        // System.out.println("base: " + base);
        int xCounter = 0, yCounter = 0;
        for (int iPixel : pixels) {
            // System.out.println(iX + " - " + iY);
            if (isReverse) {
                pattern[xCounter][yCounter] = iPixel == baseColourPixel ? CHAR_TO_PATTERN : ' ';
            } else {
                pattern[xCounter][yCounter] = iPixel != baseColourPixel ? CHAR_TO_PATTERN : ' ';
            }

            yCounter++;
            if (yCounter > 49) {
                xCounter++;
                yCounter = 0;
            }

            if (contrastColourPixel == 0 && iPixel != baseColourPixel) {
                contrastColourPixel = iPixel;
                x1 = xCounter - 2;
                y1 = yCounter - 3;
                y2 = yCounter + 3;
            }

            if (contrastColourPixel == iPixel) {
                x2 = xCounter + 3;

                if (y1 > (yCounter - 3)) {
                    y1 = yCounter - 3;
                }

                if (y2 < (yCounter + 3)) {
                    y2 = yCounter + 3;
                }
            }
        }
        return new TextualChar(x1, x2, y1, y2, pattern);
    }
    return null;
}