'Type Writer Effect in JTextPane

i'm pretty new to coding (about 5 months) and i've been trying to make a custom terminal for a text based RPG. so far i have the terminal working the way it should but i'm looking for ways to rewrite the code to come up with a typewriter effect. here's the code i have for writing the text onto a JTextPane but the code i'm trying to merge doesn't work all too well since i do feel like i am missing something. i've tried searching for what but couldn't find too many answers.

public void print(String s, boolean trace) {
        print(s, trace, new Color(255,255,255), startFont);
    }

    public void print(String s, boolean trace, Color c, boolean startFont) {
        Style style = console.addStyle("Style", null);
        StyleConstants.setForeground(style, c);
        
        if (trace) {
            Throwable t = new Throwable();
            StackTraceElement[] elements = t.getStackTrace();
            String caller = elements[0].getClassName();
            
            s = caller + " -> " + s;
            
        }
        
        if (startFont) {
            console.setFont(new Font("Bookman Old Style", Font.ITALIC, 87));
        }
        
        try {
            document.insertString(document.getLength(), s, style);
        } catch(Exception ex) {}
        
    }

    public void println(String s, boolean trace) {
        println(s, trace, new Color(255, 255, 255));
    }
    
    public void println(String s, boolean trace, Color c) {
        print(s + "\n", trace, c, startFont);
    }

here's the code that i'm trying to implement: Java typewriter effect

when i do use the "g.drawString" method i ususally get an error saying that "g" is null which makes me think that i am missing something and that "g.drawString" would work but i'm very unsure.

i have some ideas on what i should do like maybe try to make the main string public without anything to it/making it into an array or trying to make a whole new render itself for the JTextPane to get the effect but i've had no luck so far. anyone's got any ideas if it's even possible?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source