How do I get the available font names?

package org.kodejava.awt;

import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class FontNameList {
    public static void main(String[] args) {
        // Get all available fonts from GraphicsEnvironment
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();

        // Iterates all available fonts and get their name and family name
        for (Font font : fonts) {
            String fontName = font.getName();
            String familyName = font.getFamily();

            System.out.println("Font: " + fontName + "; family: " + familyName);
        }
    }
}

Here are some fonts name printed from the code snippet above:

Font: Agency FB; family: Agency FB
Font: Agency FB Bold; family: Agency FB
Font: Algerian; family: Algerian
Font: Arial; family: Arial
Font: Arial Black; family: Arial Black
Font: Arial Bold; family: Arial
Font: Arial Bold Italic; family: Arial
Font: Arial Italic; family: Arial
Font: Arial Narrow; family: Arial Narrow
Font: Arial Narrow Bold; family: Arial Narrow
...

How do I get my screen size?

package org.kodejava.awt;

import java.awt.*;

public class ScreenSizeExample {
    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        System.out.println("Screen Width: " + screenSize.getWidth());
        System.out.println("Screen Height: " + screenSize.getHeight());
    }
}

The output of the code snippet above:

Screen Width: 2560.0
Screen Height: 1080.0

How do I read image file?

Here you see a code sample to read an image file. This code will work either the image file is located in a file folder or inside a jar file. You can use javax.imageio.ImageIO class to read the image file.

package org.kodejava.awt;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class ReadingImage {
    public static void main(String[] args) {
        ReadingImage demo = new ReadingImage();
        demo.getImage();
    }

    public void getImage() {
        try {
            InputStream is = getClass().getResourceAsStream("/kodejava.png");
            BufferedImage image = ImageIO.read(is);

            // Do something with the image.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I create a beep sound?

package org.kodejava.awt;

import java.awt.*;

public class BeepExample {
    public static void main(String[] args) {
        // This is the way we can send a beep audio out.
        Toolkit.getDefaultToolkit().beep();
    }
}

Using Runtime.exec() method we can do something like the code snippet below to produce beep sound using powershell command.

try {
    Process process = Runtime.getRuntime().exec(
            new String[]{"powershell", "[console]::beep()"});

    int errorCode = process.waitFor();
    int exitValue = process.exitValue();
    System.out.println("errorCode = " + errorCode);
    System.out.println("exitValue = " + exitValue);
} catch (Exception e) {
    throw new RuntimeException(e);
}

We can also execute external command using ProcessBuilder class like the following code snippet:

try {
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("powershell", "[console]::beep()");
    Process process = processBuilder.start();

    int errorCode = process.waitFor();
    int exitValue = process.exitValue();
    System.out.println("errorCode = " + errorCode);
    System.out.println("exitValue = " + exitValue);
} catch (Exception e) {
    throw new RuntimeException(e);
}