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

public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("h.pdf"));
    document.open();/*  w w w.  j  av a 2  s  . co  m*/
    PdfContentByte cb = writer.getDirectContent();
    Graphics2D graphics2D = cb.createGraphics(PageSize.A4.width(), PageSize.A4.height());
    graphics2D.drawString("Hello World", 36, 54);
    graphics2D.dispose();
    document.close();
}

From source file:G2DDrawStringPDF.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*  w  w w  .j a  v a  2 s  . c om*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("G2DDrawStringPDF.pdf"));
        document.open();
        DefaultFontMapper mapper = new DefaultFontMapper();
        FontFactory.registerDirectories();
        mapper.insertDirectory("c:\\windows\\fonts");

        int w = 150;
        int h = 150;
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2 = tp.createGraphics(w, h, mapper);

        g2.drawString("text", 20, 20);

        g2.dispose();
        cb.addTemplate(tp, 50, 400);

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:ArabicTextPDF.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {//from  ww w  .  j a v  a2 s.  c o  m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ArabicTextPDF.pdf"));
        document.open();
        java.awt.Font font = new java.awt.Font("arial", 0, 18);
        PdfContentByte cb = writer.getDirectContent();
        java.awt.Graphics2D g2 = cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
        g2.setFont(font);
        g2.drawString("\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0645\u0646", 100, 100);
        g2.dispose();
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();//from ww  w . ja v a  2 s  .co  m
    String text = "\u5e73\u548C";
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 50);
    Graphics2D g2 = tp.createGraphicsShapes(100, 50);
    java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 12);
    g2.setFont(font);
    g2.drawString(text, 0, 40);
    g2.dispose();
    cb.addTemplate(tp, 36, 780);
    document.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();/*from   ww w . ja v a  2s .  c  om*/
    String text = "\u0936\u093e\u0902\u0924\u093f";

    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 50);
    Graphics2D g2 = tp.createGraphicsShapes(100, 50);
    java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 12);
    g2.setFont(font);
    g2.drawString("Graphics2D: " + text, 0, 40);
    g2.dispose();
    cb.addTemplate(tp, 36, 750);

    document.close();
}

From source file:UnicodeText.java

public static void main(String[] args) {
    JFrame f = new JFrame() {
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            Font font = new Font("Lucida Sans Regular", Font.PLAIN, 32);

            g2.setFont(font);//w w w.j a  va  2  s  .c  o m
            g2.drawString("\u032e\u0624\u0639", 40, 80);
        }
    };
    f.setSize(200, 200);
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document(new Rectangle(100, 100));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("sun_tutorial_with_text.pdf"));
    document.open();//  w ww .  j  a  v a 2  s. co m
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 100);
    DefaultFontMapper mapper = new DefaultFontMapper();
    mapper.insertDirectory("c:/windows/fonts");
    String name;
    Map map = mapper.getMapper();
    for (Iterator i = map.keySet().iterator(); i.hasNext();) {
        name = (String) i.next();
        System.out.println(name + ": " + ((DefaultFontMapper.BaseFontParameters) map.get(name)).fontName);
    }
    Graphics2D g2 = tp.createGraphics(100, 100, mapper);
    g2.setColor(Color.black);
    java.awt.Font thisFont = new java.awt.Font("Garamond", java.awt.Font.PLAIN, 18);
    g2.setFont(thisFont);
    String pear = "Pear";
    FontMetrics metrics = g2.getFontMetrics();
    int width = metrics.stringWidth(pear);
    g2.drawString(pear, (100 - width) / 2, 20);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
    document.close();
}

From source file:WriteImageType.java

static public void main(String args[]) throws Exception {
    try {/*from  ww  w .  ja v a2 s .  c o  m*/
        int width = 200, height = 200;

        // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
        // into integer pixels
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D ig2 = bi.createGraphics();

        Font font = new Font("TimesRoman", Font.BOLD, 20);
        ig2.setFont(font);
        String message = "www.java2s.com!";
        FontMetrics fontMetrics = ig2.getFontMetrics();
        int stringWidth = fontMetrics.stringWidth(message);
        int stringHeight = fontMetrics.getAscent();
        ig2.setPaint(Color.black);
        ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);

        ImageIO.write(bi, "PNG", new File("c:\\yourImageName.PNG"));
        ImageIO.write(bi, "JPEG", new File("c:\\yourImageName.JPG"));
        ImageIO.write(bi, "gif", new File("c:\\yourImageName.GIF"));
        ImageIO.write(bi, "BMP", new File("c:\\yourImageName.BMP"));

    } catch (IOException ie) {
        ie.printStackTrace();
    }

}

From source file:org.signserver.admin.gui.SignServerAdminGUIApplication.java

/**
 * Main method launching the application.
 */// ww  w . j a  va2  s  .  c  om
public static void main(String[] args) {
    LOG.debug("SignServer Administration GUI startup");

    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
        LOG.debug("No splash screen available.");
    } else {
        // Fill in version and copyright information
        final Graphics2D image = splash.createGraphics();
        image.setPaint(Color.BLACK);
        image.setFont(new Font("Arial", Font.BOLD, 14));
        image.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        final ResourceMap resourceMap = getApplication().getContext()
                .getResourceMap(SignServerAdminGUIApplicationAboutBox.class);
        final String version = "Version " + resourceMap.getString("appVendorLabel1.text");
        ;
        image.drawString(version, 512 - image.getFontMetrics().stringWidth(version), 215);
        image.setPaint(Color.DARK_GRAY);
        image.drawString(resourceMap.getString("appCopyright.text"), 12, 392);
        splash.update();
    }

    try {
        // Parse the command line
        final CommandLine line = new GnuParser().parse(OPTIONS, args);
        if (line.hasOption(OPTION_HELP)) {
            printUsage();
        } else {
            if (line.hasOption(OPTION_WS)) {
                protocol = Protocol.WS;
            } else {
                if (isNamingContextAvailable()) {
                    protocol = Protocol.EJB;
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Application server libraries not detected."
                                    + "\n\nTo connect to a locally running SignServer instance "
                                    + "\nplease append the appropriate application server "
                                    + "\nJAR-files and if needed a jndi.properties file."
                                    + "\n\nTo connect using web services invoke this command "
                                    + "\nwith the argument \"-ws\".");
                    protocol = Protocol.WS;
                }
            }
            if (line.hasOption(OPTION_CONNECTFILE)) {
                connectFile = new File(line.getOptionValue(OPTION_CONNECTFILE));
            }
            if (line.hasOption(OPTION_DEFAULTCONNECTFILE)) {
                defaultConnectFile = new File(line.getOptionValue(OPTION_DEFAULTCONNECTFILE));
            }
            if (line.hasOption(OPTION_BASEDIR)) {
                baseDir = new File(line.getOptionValue(OPTION_BASEDIR));
            }

            try {
                launch(SignServerAdminGUIApplication.class, args);
            } catch (Exception ex) {
                displayException(ex);
            }
        }
    } catch (ParseException ex) {
        throw new IllegalArgumentException(ex.getLocalizedMessage(), ex);
    }
}

From source file:com.vitco.Main.java

public static void main(String[] args) throws Exception {
    // display version number on splash screen
    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash != null) {
        Graphics2D g = splash.createGraphics();
        if (g != null) {
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            Font font = Font
                    .createFont(Font.TRUETYPE_FONT,
                            new SaveResourceLoader("resource/font/arcade.ttf").asInputStream())
                    .deriveFont(Font.PLAIN, 42f);
            g.setFont(font);/* w w w . j ava 2 s.c o  m*/
            //g.setFont(g.getFont().deriveFont(9f));
            g.setColor(VitcoSettings.SPLASH_SCREEN_OVERLAY_TEXT_COLOR);
            int width = g.getFontMetrics().stringWidth(VitcoSettings.VERSION_ID);
            g.drawString(VitcoSettings.VERSION_ID, 400 - 20 - width, 110);
            splash.update();
            g.dispose();
        }
    }

    // the JIDE license
    SaveResourceLoader saveResourceLoader = new SaveResourceLoader("resource/jidelicense.txt");
    if (!saveResourceLoader.error) {
        String[] jidelicense = saveResourceLoader.asLines();
        if (jidelicense.length == 3) {
            com.jidesoft.utils.Lm.verifyLicense(jidelicense[0], jidelicense[1], jidelicense[2]);
        }
    }

    // check if we are in debug mode
    if ((args.length > 0) && args[0].equals("debug")) {
        ErrorHandler.setDebugMode();
        debug = true;
    }

    // build the application
    final ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
            "com/vitco/glue/config.xml");

    // for debugging
    if (debug) {
        ((ActionManager) context.getBean("ActionManager")).performValidityCheck();
        ((ComplexActionManager) context.getBean("ComplexActionManager")).performValidityCheck();
    }

    // open vsd file when program is started with "open with"
    MainMenuLogic mainMenuLogic = ((MainMenuLogic) context.getBean("MainMenuLogic"));
    for (String arg : args) {
        if (arg.endsWith(".vsd")) {
            File file = new File(arg);
            if (file.exists() && !file.isDirectory()) {
                mainMenuLogic.openFile(file);
                break;
            }
        }
    }

    // perform shortcut check
    ((ShortcutManager) context.getBean("ShortcutManager")).doSanityCheck(debug);
    //        // test console
    //        final Console console = ((Console) context.getBean("Console"));
    //        new Thread() {
    //            public void run() {
    //                while (true) {
    //                    console.addLine("text");
    //                    try {
    //                        sleep(2000);
    //                    } catch (InterruptedException e) {
    //                       //e.printStackTrace();
    //                    }
    //                }
    //            }
    //        }.start();

    // add a shutdown hook
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            // make reference so Preferences object doesn't get destroyed
            Preferences pref = ((Preferences) context.getBean("Preferences"));
            // trigger @PreDestroy
            context.close();
            // store the preferences (this needs to be done here, b/c
            // some PreDestroys are used to store preferences!)
            pref.save();
        }
    });
}