I'm getting into graphical stuff in Java and want to display text. As I've read, drawString() is the standard method for this. My code to draw the string is:
import java.awt.*;
import javax.swing.*;
public class TextDisplay extends JPanel{
public void paint(Graphics g) {
g.drawString("My Text", 10, 20);
}
}
The class executing this is:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
TextDisplay d = new TextDisplay();
JFrame f = new JFrame();
f.getContentPane().add(d);
f.setSize(200,200);
f.setVisible(true);
}
}
This produces a window with the text "My Text" in it as expected. The question now is: How can I draw any String? With this I have to write the String into the paint() method, but I want to input it from somewhere else as a variable.
paint(Graphics g, String s)? If so, where would this String come from? My main program never actually calls this method.