-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthBar.java
94 lines (79 loc) · 2.21 KB
/
HealthBar.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
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.Image;
import javax.swing.*;
import java.awt.event.*;
public class HealthBar
{
private Image [] healthBar;
private int health, width, height;
private Image heart0, heart1, heart2;
public HealthBar(int life, int w, int h)
{
width = w;
height = h;
// gets the images
heart0 = pic (w, h, "Heart 0.png");
heart1 = pic (w, h, "Heart 1.png");
heart2 = pic (w, h, "Heart 2.png");
// initializes health and hp
health = life;
int hp = health/10;
if (health%10 != 0)
hp ++;
// makes the health bar and updates it
healthBar = new Image [hp];
updateBar();
}
public void setHealthBar (Image[] img) // sets health bar to image array
{
healthBar = img;
}
public Image [] getHealthBar () // gets health bar
{
return healthBar;
}
public void changeHealth (int h) // changes player's health then updates the health bar
{
health = h;
updateBar();
}
public int getLength () // gets the length of the health bar
{
return healthBar.length;
}
public Image getHpImg (int i) // gets the HP Image at index i
{
return healthBar [i];
}
public void updateBar() // updates the health bar
{
for (int i = 0; i < healthBar.length; i++) // loop through all elemnts
{
// draws the image based on how much health you have
if ((i+1)*10 <= health)
healthBar [i] = heart0;
else if ((i+1)*10-5 <= health)
healthBar [i] = heart1;
else
healthBar [i] = heart2;
}
}
public Image pic (int width, int height, String file)
{
Image img = null;
try
{
img = ImageIO.read (new File (file));
}
catch (IOException e)
{
}
return img.getScaledInstance (width, height, Image.SCALE_SMOOTH);
}
public Image reSize (int width, int height, Image img) // resizes images
{
return img.getScaledInstance (width, height, Image.SCALE_SMOOTH);
}
}