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

public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {//from   w  ww  .  ja  v a 2s  .  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:Main.java

public static void main(String[] args) throws Exception {
    String text = "java2s.com";

    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 48);
    g2d.setFont(font);
    FontMetrics fm = g2d.getFontMetrics();
    int width = fm.stringWidth(text);
    int height = fm.getHeight();
    g2d.dispose();/*  w ww  .ja  va  2s . co m*/

    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setFont(font);
    fm = g2d.getFontMetrics();
    g2d.setColor(Color.BLACK);
    g2d.drawString(text, 0, fm.getAscent());
    g2d.dispose();

    ImageIO.write(img, "png", new File("Text.png"));

}

From source file:WriteImageType.java

static public void main(String args[]) throws Exception {
    try {/*from w ww  .  ja  v a 2  s  . com*/
        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: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   w  w w.  ja va2s . 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   w ww .j a v  a 2s. c o  m
    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: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  w  w . ja  va  2s.c o 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: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);
            g2.drawString("\u032e\u0624\u0639", 40, 80);
        }/*from w  w  w  . j  a  v  a 2  s  .co m*/
    };
    f.setSize(200, 200);
    f.setVisible(true);
}

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

/**
 * Main method launching the application.
 *//*from w w w. j  av a  2 s  .c  o  m*/
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);
            //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();/*from w  w  w .  j a va2  s.  c  om*/
            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();
        }
    });
}

From source file:Draw2DTest.java

public static void main(String[] args) {
    final Graphics2DRenderer renderer = new Graphics2DRenderer();
    Shell shell = new Shell();
    shell.setSize(350, 350);/*from w  w  w.  j  av  a2  s .  c om*/

    shell.open();
    shell.setText("Draw2d Hello World");
    LightweightSystem lws = new LightweightSystem(shell);
    IFigure figure = new Figure() {
        protected void paintClientArea(org.eclipse.draw2d.Graphics graphics) {
            Dimension controlSize = getSize();

            renderer.prepareRendering(graphics);
            // prepares the Graphics2D renderer

            // gets the Graphics2D context and switch on the antialiasing
            Graphics2D g2d = renderer.getGraphics2D();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // paints the background with a color gradient
            g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.width,
                    (float) controlSize.width, java.awt.Color.white));
            g2d.fillRect(0, 0, controlSize.width, controlSize.width);

            // draws rotated text
            g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16));
            g2d.setColor(java.awt.Color.blue);

            g2d.translate(controlSize.width / 2, controlSize.width / 2);
            int nbOfSlices = 18;
            for (int i = 0; i < nbOfSlices; i++) {
                g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0);
                g2d.rotate(-2 * Math.PI / nbOfSlices);
            }

            // now that we are done with Java2D, renders Graphics2D
            // operation
            // on the SWT graphics context
            renderer.render(graphics);

            // now we can continue with pure SWT paint operations
            graphics.drawOval(0, 0, controlSize.width, controlSize.width);
        }
    };
    lws.setContents(figure);
    Display display = Display.getDefault();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    renderer.dispose();
}