How to print a Banner in Java easily

In this short wiki we will learn how to use the Figlet API to generate an AsciiArt Banners in Java applications. We will also show how to emed a sample example in a JBang Script.

Figlet is a command-line tool which helps you to create ascii banners like this:

 _   _        _  _         __        __              _      _  _ 
| | | |  ___ | || |  ___   \ \      / /  ___   _ __ | |  __| || |
| |_| | / _ \| || | / _ \   \ \ /\ / /  / _ \ | '__|| | / _` || |
|  _  ||  __/| || || (_) |   \ V  V /  | (_) || |   | || (_| ||_|
|_| |_| \___||_||_| \___/     \_/\_/    \___/ |_|   |_| \__,_|(_)
                                                                 

Let’s see how to create a sample banner like that in a minute!

Configuring and running Figlet

In order to use Figlet API, include the following dependency in your project:

<dependency>
    <groupId>com.github.lalyos</groupId>
    <artifactId>jfiglet</artifactId>
    <version>0.0.9</version>
</dependency>

On the other hand, if you want to generate Banners in a JBang script, all you need is to add the proper //DEPS to your script. For example:

//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.github.lalyos:jfiglet:0.0.9

import com.github.lalyos.jfiglet.FigletFont;
import java.io.IOException;

public class PrintBanner {

    public static void main(String[] args) throws IOException {
         String asciiArt = FigletFont.convertOneLine("Hello World!");
         System.out.println(asciiArt);
    }
}

Then, run the jbang script as follows:

jbang PrintBanner.java

Here’s your beautiful Banner:

create banner in java how to

Using a Custom Font in your Banners

You can also use custom Fonts to display your banners. Some example fonts are available in JFiglet repository ( look for .flf files ). Here is a sample script which uses a custom Font in the Banner:

//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.github.lalyos:jfiglet:0.0.9

import com.github.lalyos.jfiglet.FigletFont;
import java.io.IOException;

public class PrintBanner {

    public static void main(String[] args) throws IOException {
    
    String asciiArt = FigletFont.convertOneLine("classpath:/slant.flf", "Hello World!");     
    System.out.println(asciiArt);    
     
    }
}

Then, see the new Font in the output:

jbang PrintBanner.java
[jbang] Building jar for PrintBanner.java...
    __  __     ____         _       __           __    ____
   / / / /__  / / /___     | |     / /___  _____/ /___/ / /
  / /_/ / _ \/ / / __ \    | | /| / / __ \/ ___/ / __  / / 
 / __  /  __/ / / /_/ /    | |/ |/ / /_/ / /  / / /_/ /_/  
/_/ /_/\___/_/_/\____/     |__/|__/\____/_/  /_/\__,_(_)  

Conclusion

In this tutorial, we explored how to use the jfiglet library to create banners in Java. We started by adding the jfiglet dependency to our Maven project, which provides a convenient way to generate banners using ASCII art fonts. We then learned how to use the FigletFont class to load a Figlet font from the classpath and create a FigletFont object, which can be used to convert text into a banner using the convert method. We also learned how to customize the appearance of our banners by setting the banner width and selecting a different font.

Found the article helpful? if so please follow us on Socials