-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighScorePanel.java
executable file
·158 lines (134 loc) · 3.92 KB
/
HighScorePanel.java
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.*;
import javax.swing.*;
public class HighScorePanel extends JPanel implements MouseListener {
// Declaration and initialization
BufferedImage Background;
JLabel back = new JLabel();
ImageIcon backButton, backButtonhover;
JTextArea scoreBoard;
//setting x value of image to provode animation
int x = 1366;
int y = 0;
//Array list to store High scores from file
ArrayList<Score> data = new ArrayList<Score>();
{
// Setting layout null
setLayout(null);
try {
// Reading image from storage
Background = ImageIO.read(new File("resources\\HighScore.png"));
backButton = new ImageIcon("resources\\backLarge.png");
backButtonhover = new ImageIcon("resources\\backhLarge.png");
// input streams for Reading Score from file
FileInputStream input = new FileInputStream("score.txt");
ObjectInputStream player = new ObjectInputStream(input);
data = (ArrayList<Score>) player.readObject();
}
// Catching exception
catch (FileNotFoundException e) {
e.printStackTrace();
System.out.print("Image not found");
} catch (IOException e) {
System.out.println("Creating new file");
} catch (ClassNotFoundException e) {
System.out.println("There is a litle error . Please report so that we can fix it");
}
//Writing score to TextArea
scoreBoard = new JTextArea("");
//Setting bounds,color anf font of scoreBoard
scoreBoard.setBounds(400, 150, 550, 500);
scoreBoard.setBackground(new Color(0, 0, 0, 0));
Font myFont = new Font("Serif", Font.BOLD, 36);
scoreBoard.setForeground(Color.red);
scoreBoard.setFont(myFont);
add(scoreBoard);
//adding back button
back.setIcon(backButton);
add(back);
back.setBounds(0, 660, 100, 100);
back.addMouseListener(this);
}
// Paint Component method
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Color color = new Color(255, 255, 255, 0);
setBackground(color);
// Drawing image to panel in full height and width
g2d.drawImage(Background, x, y, getWidth(), getHeight(), null);
//animation
if (x > 0) {
x -= 100;
}
if (x <= 0) {
x = 0;
//Showing scores on scoreBoard
if (data != null) {
scoreBoard.setText("");
for (int i = 0; i < data.size(); i++) {
String name=data.get(i).getName();
//setting default name if name is null
scoreBoard.append((i + 1) + " " + name
+ "\t\t" + data.get(i).getScore() + "\n");
}
}
}
//sleeping
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}// Paint Component ends
@Override
public void mouseClicked(MouseEvent e) {
//exiting menu
if (e.getSource() == back) {
Menu.fm.dispose();
}
}
//hover effect
@Override
public void mouseEntered(MouseEvent e) {
if (e.getSource() == back) {
back.setIcon(backButtonhover);
}
}
//hover effect
@Override
public void mouseExited(MouseEvent e) {
if (e.getSource() == back) {
back.setIcon(backButton);
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
//Escape button to go back to menu
Action Escape = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Menu.fm.dispose();
}
};
//Adding key binding of Escape button
{
InputMap mp = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
mp.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
"keyEscape");
ActionMap act = this.getActionMap();
act.put("keyEscape", Escape);
}
}// Class Ends