Добавление Highscores в игру Java - от консоли до JPanel - Сохранение HighScore в зашифрованном текстовом файлеJAVA

Программисты JAVA общаются здесь
Anonymous
Добавление Highscores в игру Java - от консоли до JPanel - Сохранение HighScore в зашифрованном текстовом файле

Сообщение Anonymous »

Я новичок в Java и каждый день изучаю новые вещи. Английский не мой родной язык, извините. И время, которое у него есть, нужно сохранить в зашифрованном текстовом файле.
Итак, у меня есть пакет Highscores, объединяющий несколько классов. Код работает более или менее, он выводит в консоли. Теперь мне нужно, чтобы этот выход выводится на JPanel рядом с моим лабиринтом. Я добавил некоторую дополнительную информацию в коде
вот мой класс Highscore: < /p>

public class Highscore {
// An arraylist of the type "score" we will use to work with the scores inside the class
private ArrayList scores;

// The name of the file where the highscores will be saved
private static final String highscorefile = "Resources/scores.dat";

//Initialising an in and outputStream for working with the file
ObjectOutputStream output = null;
ObjectInputStream input = null;

public Highscore() {
//initialising the scores-arraylist
scores = new ArrayList();
}
public ArrayList getScores() {
loadScoreFile();
sort();
return scores;
}
private void sort() {
ScoreVergelijken comparator = new ScoreVergelijken();
Collections.sort(scores, comparator);
}
public void addScore(String name, int score) {
loadScoreFile();
scores.add(new Score(name, score));
updateScoreFile();
}
public void loadScoreFile() {
try {
input = new ObjectInputStream(new FileInputStream(highscorefile));
scores = (ArrayList) input.readObject();
} catch (FileNotFoundException e) {
System.out.println("[Laad] FNF Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("[Laad] IO Error: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("[Laad] CNF Error: " + e.getMessage());
} finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException e) {
System.out.println("[Laad] IO Error: " + e.getMessage());
}
}
}
public void updateScoreFile() {
try {
output = new ObjectOutputStream(new FileOutputStream(highscorefile));
output.writeObject(scores);
} catch (FileNotFoundException e) {
System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file");
} catch (IOException e) {
System.out.println("[Update] IO Error: " + e.getMessage());
} finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException e) {
System.out.println("[Update] Error: " + e.getMessage());
}
}
}
public String getHighscoreString() {
String highscoreString = "";
int max = 10;

ArrayList scores;
scores = getScores();

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

} < /p>

Вот мой основной класс: < /p>

public class Main {
public static void main(String[] args) {
Highscore hm = new Highscore();
hm.addScore("Bart",240);
hm.addScore("Marge",300);
hm.addScore("Maggie",220);
hm.addScore("Homer",100);
hm.addScore("Lisa",270);
hm.addScore(LabyrinthProject.View.MainMenu.username,290);

System.out.print(hm.getHighscoreString());
} }
< /code>

класс оценки: < /p>

public class Score implements Serializable {
private int score;
private String naam;

public Score() {

}

public int getScore() {
return score;
}

public String getNaam() {
return naam;
}

public Score(String naam, int score) {
this.score = score;
this.naam = naam;
}
< /code>

} < /p>

countvergelijken class (что означает сравнение) < /p>

public class ScoreVergelijken implements Comparator {
public int compare(Score score1, Score score2) {

int sc1 = score1.getScore();
int sc2 = score2.getScore();

if (sc1 > sc2){
return -1; // -1 means first score is bigger then second score
}else if (sc1 < sc2){
return +1; // +1 means that score is lower
}else{
return 0; // 0 means score is equal
}
} }
< /code>

Если кто -то мог бы объяснить мне, что использовать, это было бы очень оценено! Большое спасибо! Как я могу этого добиться? < /P>

Искри>

Подробнее здесь: https://stackoverflow.com/questions/223 ... core-in-en

Вернуться в «JAVA»