How to get drawStrings on new lines

455 views Asked by At

So, I have this String(snippet below) which has a fair amount of information

highscoreString += (i + 1) + ".\t\n" + scores.get(i).getName() + "\t\t\n" + scores.get(i).getScore() + "\n";

which is inside this method

public String getHighscoreString() {
    String highscoreString = "";
    int max = 10;

    ArrayList<Score> scores;
    scores = getScores();

    int i = 0;
    int x = scores.size();
    if (x > max) {
        x = max;
    }
    while (i < x ) {
        highscoreString += (i + 1) + ".\t\n" + scores.get(i).getName() + "\t\t\n" + scores.get(i).getScore() + "\n";
        i++;
    }
    return highscoreString;
}

And what I am trying to do, is put that information in a g.drawString(); which I can do easily, but what I cannot do is, to go onto a new line once it gets to the end of that highscoreString(first snippet shown).

This is what I've done so far using the g.drawString() method

g.setFont(new Font("Century Gothic", Font.BOLD, 10));
        g.drawString("" + hsm.getHighscoreString(), 40, 310);

Even if I put \n at the end of the highscoreString, it wont end the line...

I'm aware of the fact that I can use this method to make new lines

void drawString(Graphics g, String text, int x, int y) {
for (String line : text.split("\n"))
    g.drawString(line, x, y += g.getFontMetrics().getHeight());
}

But, how would I be able to split the highscoreString?

0

There are 0 answers