1
\$\begingroup\$

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.

package bp.display;

import java.awt.Color;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

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

    public static void print(String s, boolean trace, Color c) {
        Style style = Console.console.addStyle("Style", null);
        StyleConstants.setForeground(style, c);
        
        for(int i = 0; i < Console.doc.getLength(); i++) {
            
        }
        
        if (trace) {
            Throwable t = new Throwable();
            StackTraceElement[] elements = t.getStackTrace();
            String caller = elements[0].getClassName();
            
            s = caller + " -> " + s;
            
        }
        
        try {
            Console.doc.insertString(Console.doc.getLength(), s, style);
        } catch(Exception ex) {}
        
    }

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

here's the code that i'm trying to implement: https://stackoverflow.com/questions/35673302/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?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ the method is called drawString and is called on the object g (at least in the linked example). There is even a comment telling you //Where g is your Graphics object and x and y are the coordinates you want to draw at. If the error tells you its null, you would need to show us the g in your code and how the create it \$\endgroup\$
    – Zibelas
    Apr 28 at 21:54
  • \$\begingroup\$ In Java, variables like your g are "references" (a number representing the address of a bit of memory containing the object, not the object itself). They tell the program where to look for the object in memory when needed. If Java tells you a variable is null, it's saying the address is blank. Either you haven't created/assigned an object to initialise the variable, or you've unset it elsewhere in your code. \$\endgroup\$
    – Basic
    Apr 28 at 22:28
  • \$\begingroup\$ Welcome to GDSE. I'm voting to close this because cross-posting the same question across multiple Stack Exchange sites is not allowed. This seems to be a question about general debugging more so than game development & Stack Overflow is the more appropriate community for that sort of question. \$\endgroup\$
    – Pikalek
    Apr 29 at 3:07
  • 3
    \$\begingroup\$ I’m voting to close this question because it is cross-posted on Stack Overflow. \$\endgroup\$
    – Pikalek
    Apr 29 at 3:10

0

Browse other questions tagged .