How to input graphics on a console without JFrame?

Viewed 238

I use codiva to do my coding on a chromebook. I was just wondering if I could create graphics in a console output. For example, my text right now is plain and normal(it prints in the console as plain text). Is there a way to bold, emphasize, or even change the color of the text if I can only use the console for output(no canvas, Jframe, popup, etc.)?

I have tried "\u001B[1m (bold text)" and all I get is (see Output). Same goes for the italic one.

Output:

2 Answers

The interpretation of control sequences is subject of the specific console and it seems, codiva.io’s console doesn’t interpret any control sequences.

However, since it’s displayed in a browser and browsers usually have broad Unicode support, you can achieve a limited formatting using special codepoints. E.g.

class HelloCodiva {
    public static void main(String[] args) {
        System.out.println("Formatted: "
            + bold("bold") + " " + italic("italic") + " " + bold(italic("both")));
    }

    static CharSequence bold(CharSequence cs) {
        return trans(cs, 0x1D400, 0x1D41A);
    }

    static CharSequence italic(CharSequence cs) {
        return trans(cs, 0x1D434, 0x1D44e);
    }

    static CharSequence trans(CharSequence cs, int upper, int lower) {
        return cs.codePoints()
            .map(cp -> cp >= 'A' && cp <= 'Z'? cp + upper - 'A':
                       cp >= 'a' && cp <= 'z'? cp + lower - 'a':
                       cp >= 0x1D400 && cp <= 0x1D433? cp + 104:
                       cp >= 0x1D434 && cp <= 0x1D467? cp + 52:
                       cp)
            .collect(StringBuilder::new,
                     StringBuilder::appendCodePoint, StringBuilder::append);
    }
}

prints

Formatted:   

In codiva.io’s console.

The limitation is that it only works for letters. But for highlighting important words, it might be sufficient.

Use a java.awt.image.BufferedImage

BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();

//.. draw stuff ..

ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
writer.setOutput(ImageIO.createImageOutputStream(new File("myimage.png"));
writer.write(image);

This may not work with all terminals, because it is terminal-dependent. Here's a doc that I based my answer upon. (DOC)

Related