Example usage for java.awt Graphics drawString

List of usage examples for java.awt Graphics drawString

Introduction

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

Prototype

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

Source Link

Document

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

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);// w  ww  .j a  va  2s  .  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:MainClass.java

public static void main(String[] args) {
    try {//w  w  w  .  ja  v  a 2  s  .  co m
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setJobName("Graphics Demo Printout");
        pjob.setCopies(1);
        pjob.setPrintable(new Printable() {
            public int print(Graphics pg, PageFormat pf, int pageNum) {
                if (pageNum > 0) // we only print one page
                    return Printable.NO_SUCH_PAGE; // ie., end of job

                pg.drawString("www.java2s.com", 10, 10);

                return Printable.PAGE_EXISTS;
            }
        });

        if (pjob.printDialog() == false) // choose printer
            return;
        pjob.print();
    } catch (PrinterException pe) {
        pe.printStackTrace();
    }
}

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);/*w  ww.  ja  v  a  2 s  .  co  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//  w  w w  . j a  v a2 s  .co  m
        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:Main.java

public static void main(String[] args) {
    String s = "The quick brown fox jumps over the lazy dog!";
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D b = fm.getStringBounds(s, g);
    System.out.println(b);//from www  . j a  v a2 s.co m
    bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()),
            BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.drawString(s, 0, (int) b.getHeight());

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

    // Technique 3 - JLabel
    JLabel l = new JLabel(s);
    l.setSize(l.getPreferredSize());
    bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 100);
    l.paint(g);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}

From source file:SplashScreenDemo.java

public static void main(String[] args) {
    SplashScreen splashScreen = SplashScreen.getSplashScreen();
    Dimension size = splashScreen.getSize();
    int borderDim = (int) (size.height * 0.05);
    Graphics g = splashScreen.createGraphics();
    g.setColor(Color.blue);/*from   w w w  .jav a  2  s . c o  m*/
    for (int i = 0; i < borderDim; i++)
        g.drawRect(i, i, size.width - 1 - i * 2, size.height - 1 - i * 2);

    FontMetrics fm = g.getFontMetrics();
    int sWidth = fm.stringWidth("Initializing...");
    int sHeight = fm.getHeight();
    if (sWidth < size.width && 2 * sHeight < size.height) {
        g.setColor(Color.blue);
        g.drawString("Initializing...", (size.width - sWidth) / 2, size.height - 2 * sHeight);
    }

    splashScreen.update();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
}

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);//from w w w  . j  a v a 2 s. co  m
            int off = 0;
            for (int i = 0; i < 256 * 256; i++) {
                if (font.canDisplay((char) i)) {
                    off++;
                    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);/* w w  w .  j a  v a 2 s .c  o  m*/
            int off = 0;
            for (int i = 0; i < 256 * 256; i++) {
                if (font.canDisplay((char) i) == false) {
                    continue;
                }
                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 args[]) {
    JFrame f = new JFrame();
    f.add(new JComponent() {
        public void paintComponent(Graphics g) {

            // Some parameters.
            String text = "Some Label";
            int centerX = 150, centerY = 100;
            int ovalWidth = 200, ovalHeight = 100;

            // Draw oval
            g.setColor(Color.BLUE);
            g.fillOval(centerX - ovalWidth / 2, centerY - ovalHeight / 2, ovalWidth, ovalHeight);

            // Draw centered text
            FontMetrics fm = g.getFontMetrics();
            double textWidth = fm.getStringBounds(text, g).getWidth();
            g.setColor(Color.WHITE);
            g.drawString(text, (int) (centerX - textWidth / 2), (int) (centerY + fm.getMaxAscent() / 2));

        }/*from  w  w  w.j av  a2 s .  c  o m*/
    });

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Paints string.//from  w  w w  .j  a va 2s  .  co  m
 *
 * @param g
 *            graphics context
 * @param text
 *            painted text
 * @param x
 *            text X coordinate
 * @param y
 *            text Y coordinate
 */
public static void drawString(final Graphics g, final String text, final int x, final int y) {
    g.drawString(text, x, y);
}