I'm trying to make a method which will split a long text into lines and draw them on documents using Graphics. I managed to figure out how to split the lines that I get from a JTextArea component but don't know how to make them wrap/break when a line gets too long.
Here's my code so far:
void drawString(Graphics g, String text, int x, int y, Font w) {
g.setFont(w);
for (String line : text.split("\n"))
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
Any help is appreciated.
Edit:
My thoughts on a fix about this is to calculate the char position of the string and if it reaches a selected position then I add a line break ("\n")
there. Any other suggestions or should I go for this one?