0

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?

Sebastian A
  • 521
  • 1
  • 5
  • 9
  • For my money, I'd try to display my String in a JTextArea, one set with `setLineWrap(true)` and `setWrapStyleWord(true)` methods both called and set to true. I know that I can get rid of the text area's background so that it doesn't even look like a JTextArea if I wanted to. – Hovercraft Full Of Eels Nov 23 '14 at 18:22
  • @HovercraftFullOfEels Ah, I think I'm aware of what you mean. But I already have a JFrame with a functioning game, and I want this ranking system to be in the main menu, so I could put a JTextArea inside of it and remove the background(of the JTextArea)? – Sebastian A Nov 23 '14 at 18:31
  • `"...I want this ranking system to be in the main menu, so I could put a JTextArea inside of it and remove the background(of the JTextArea)?"` -- I cannot say for sure since I'm not sure what your main menu is comprised of. I assume that it's some type of Swing component since you're drawing in it, and since most all Swing components can act as containers and thus can accept a JTextArea, I will assume that it's possible. As usual though, the devil will lie in the details of your current code and problem. – Hovercraft Full Of Eels Nov 23 '14 at 18:35

0 Answers0