How do I write text on an image or the screen?

We will assume that you know how to import Minueto into your project and how to create a MinuetoWindow. If this is not the case, please read How to build your first game Window?.

Minueto allows you to build images of a string. You can then draw that string over any image surfaces. In fact, the image of the string is a regular MinuetoImage, so you can draw it, crop it, scale it, etc. If you have read the previous howtos, you should know the drill by now.

The first step in drawing text is initializing a Font object. A font object describes how text is drawn (square letters or hand-drawn, bold or italic, etc).

Loading a font in Minueto is a best effort task. You can try to load any font available on the computer. If the specified font is not available, Minueto will load a default font instead. Five special fonts are available on every JVM. More information on MinuetoFont is available in the API description.

MinuetoFont fontArial14; 
fontArial14 = new MinuetoFont("Arial",14,false, false);

This will create an Arial font object of size 14. If the Arial font is not available, then the default Java font will be loaded. The two boolean parameters indicate that the font should not be bold and should not be italic.

The second step is to build the image of the string.

MinuetoImage helloWorld;
helloWorld = new MinuetoText("HelloWorld",fontArial14,Minueto.Color.BLUE);

The following code would create an image containing the string "HelloWorld" in Arial 14.

window.draw(helloWorld, 10, 10);

You can draw this string over any image or window.

Example of drawing text

The following will draw the "HelloWorld" string in the top left corner of the window. Remember, you only need to build a string image once, even if you are going to draw that string multiple times.

import org.minueto.*; 
import org.minueto.handlers.*; 
import org.minueto.image.*; 
import org.minueto.window.*; 
        
public class Demo {
    
    public static void main(String[] args) {

        MinuetoWindow window; 
        MinuetoFont fontArial14;
        MinuetoImage helloWorld;

        window = new MinuetoFrame(640, 480, true); 

        fontArial14 = new MinuetoFont("Arial",14,false, false); 

        helloWorld = new MinuetoText("HelloWorld" ,fontArial14,Minueto.Color.BLUE); 

        window.setVisible(true);

        while(true) { 
            window.draw(helloWorld, 10, 10);
            window.render();
            Thread.yield();
        }
    }
}