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?
drawString
and is called on the objectg
(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 theg
in your code and how the create it \$\endgroup\$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\$