-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighScore.java
More file actions
141 lines (114 loc) · 3.55 KB
/
HighScore.java
File metadata and controls
141 lines (114 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// HighScore.java
// David Wang & Wendy Xu
// Organizes the highscores from a text file into the game
// displays the top 5 high scores
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
class HighScore extends JFrame implements ActionListener{
MainMenu menu;
HighScorePanel highscorepanel;
javax.swing.Timer myTimer;
public HighScore(MainMenu menu){
super();
this.menu = menu;
setLayout(null);
highscorepanel = new HighScorePanel(this);
highscorepanel.setSize(818,647);
highscorepanel.setLocation(0,0);
add(highscorepanel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(818,647);
myTimer = new javax.swing.Timer(100,this);
myTimer.start();
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
highscorepanel.draw();
}
public void mainMenu(){
menu.playMusic();
menu.setVisible(true);
setVisible(false);
}
public static void main(String[]args){
HighScore highscore = new HighScore(null);
}
}
class HighScorePanel extends JPanel implements MouseListener,MouseMotionListener{
HighScore highscoreframe;
int[] pos;
private Rectangle backButton;
private Image highscorePic;
private Image highScoreHover;
boolean clicked;
private ArrayList<Image> scoreNums = new ArrayList<Image>(); // image of numbers (to print scores)
public void mouseClicked(MouseEvent evt){
clicked = true;
}
public void mouseExited(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){
clicked = true;
}
public void mousePressed(MouseEvent evt){
clicked = false;
}
public void mouseMoved(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public void mouseDragged(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public HighScorePanel(HighScore highscoreframe){
super();
setFocusable(true);
grabFocus();
addMouseListener(this);
addMouseMotionListener(this);
this.highscoreframe = highscoreframe;
highscorePic = new ImageIcon("Images/Game/Highscores/Highscores.png").getImage();
highScoreHover = new ImageIcon("Images/Game/Highscores/HighscoresBack.png").getImage();
backButton = new Rectangle(95,17,144,112);
pos = new int[]{0,0};
clicked = false;
for(int i = 0; i < 10; i ++){
scoreNums.add(new ImageIcon(String.format("Images/Game/Highscores/%s.png",i)).getImage());
}
}
public void draw(){
repaint();
}
public void paintComponent(Graphics g){
g.drawImage(highscorePic,0,0,this);
if (backButton.contains(pos[0],pos[1])){
g.drawImage(highScoreHover,0,0,this);
if(clicked){
highscoreframe.mainMenu();
}
}
clicked = false;
ArrayList<Integer> scores = new ArrayList<Integer>();
try{
Scanner inFile = new Scanner(new BufferedReader(new FileReader("highscores.txt")));
while(inFile.hasNextInt()){ // takes all scores saved in textfile
scores.add(inFile.nextInt());
}
inFile.close();
}
catch(Exception excep){
System.out.println("Oh no! "+excep);
}
for(int i = 0; i < Math.min(scores.size(),5); i++){ // only display top 5 high scores
String scoreDigits = ""+scores.get(i); // use to get number of digits
int temp = scores.get(i); // use temp score to draw single digits
for(int j = 0; j < scoreDigits.length(); j ++){
g.drawImage(scoreNums.get(temp%10),630-50*j,248+i*60,this);
temp /= 10; // take away a single digit every time
}
}
}
}