-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreDisplay.java
More file actions
135 lines (111 loc) · 3.04 KB
/
Copy pathScoreDisplay.java
File metadata and controls
135 lines (111 loc) · 3.04 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
import java.awt.*;
import java.util.Random;
import java.util.ArrayList;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
Draws the score in the game
@author Jack Humphries
@version 5/10/2014
*/
public class ScoreDisplay
{
/** the score to draw */
private int score;
/** the width of all of the score digits together */
private int scoreWidth;
/** the height of the score */
private int scoreHeight;
/** an array of the digits of the score (1, 2, 3, etc.) */
private ArrayList<BufferedImage> scoreDigitImages;
/**
draws the score at the given positon (x, y)
@param g2 the graphics object
@param x the x coordinate of the left of the first digit
@param y the y coordinate of the top of the first digit
*/
public void drawScore(Graphics2D g2, int x, int y)
{
int digitSpace = 0;
for (BufferedImage scoreDigit : scoreDigitImages)
{
g2.drawImage(scoreDigit, x + digitSpace, y, null);
digitSpace += scoreDigit.getWidth();
}
}
/**
sets the score to a new number
@param newScore the new score
*/
public void setScore(int newScore)
{
score = newScore;
generateScore();
}
/**
generates the score digit images
*/
private void generateScore()
{
scoreWidth = 0;
scoreHeight = 0;
scoreDigitImages = new ArrayList<BufferedImage>();
ArrayList<Integer> scoreDigits = new ArrayList<Integer>();
String tempScore = Integer.toString(score);
for (int count = 0; count < tempScore.length(); count++)
{
scoreDigits.add(tempScore.charAt(count) - '0');
}
scoreDigitImages = new ArrayList<BufferedImage>();
for (int count = 0; count < scoreDigits.size(); count++)
{
int digit = scoreDigits.get(count);
File digitPath;
if (digit == 1 && count == 0)
{
digitPath = new File("images/score_1_firstPosition.png");
}
else
{
digitPath = new File("images/score_" + digit + ".png");
}
BufferedImage digitImage = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
try
{
digitImage = ImageIO.read(digitPath);
}
catch (Exception exception)
{
}
scoreWidth += digitImage.getWidth();
scoreDigitImages.add(digitImage);
if (digitImage.getHeight() > scoreHeight)
{
scoreHeight = digitImage.getHeight();
}
}
}
/**
the width of all of the score digits together
@return the width of the score
*/
public int getWidth()
{
return scoreWidth;
}
/**
the height of the score
@return the height of the score
*/
public int getHeight()
{
return scoreHeight;
}
public ScoreDisplay(int theScore)
{
score = theScore;
generateScore();
}
}