-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHealthBar.java
More file actions
89 lines (72 loc) · 3.35 KB
/
Copy pathHealthBar.java
File metadata and controls
89 lines (72 loc) · 3.35 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class draws the player health bar
*/
import java.awt.*;
import java.awt.geom.*;
public class HealthBar {
// Constants for the dimensions and positioning of the health bar
public static final int BAR_WIDTH = 80; // Width of the health bar
public static final int BAR_HEIGHT = 10; // Height of the health bar
public static final int Y_OFFSET = 15; // Distance below the player
// Reference to the player whose health is being displayed
public final Player PLAYER;
// Colors used for the health bar
public final Color BORDER_COLOR = Color.WHITE; // Color of the border
public final Color BACKGROUND_COLOR = new Color(60, 60, 60, 200); // Background color of the bar
public final Color HEALTH_COLOR = new Color(200, 150, 255); // Color representing the health
// Constructor to initialize the health bar with the player
public HealthBar(Player player) {
this.PLAYER = player;
}
// Method to draw the health bar
public void draw(Graphics2D g2d) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Compute the position of the health bar centered below the player
int barX = (int) (PLAYER.getX() + PLAYER.getWidth() / 2.0 - BAR_WIDTH / 2.0);
int barY = (int) (PLAYER.getY() + PLAYER.getHeight() + Y_OFFSET);
// Calculate the fraction of health remaining (clamped between 0 and 1)
float fraction = (float) PLAYER.getHp() / PLAYER.getMaxHp();
fraction = Math.max(0f, Math.min(1f, fraction));
// Rounded corner radius and shadow offset
int arc = 10;
int shadowOffset = 3;
Color shadowColor = new Color(0, 0, 0, 80); // Shadow color
// Draw shadow for the health bar
g2d.setColor(shadowColor);
g2d.fillRoundRect(barX + shadowOffset,
barY + shadowOffset,
BAR_WIDTH,
BAR_HEIGHT,
arc, arc);
// Draw the background track of the health bar
RoundRectangle2D track = new RoundRectangle2D.Float(
barX, barY, BAR_WIDTH, BAR_HEIGHT, arc, arc);
g2d.setColor(BACKGROUND_COLOR.darker());
g2d.fill(track);
// Calculate the width of the filled portion based on health fraction
int fillWidth = (int) (BAR_WIDTH * fraction);
if (fillWidth > 0) {
// Create a gradient for the health fill
GradientPaint grad = new GradientPaint(
barX, barY, HEALTH_COLOR.brighter(),
barX, barY + BAR_HEIGHT, HEALTH_COLOR.darker());
g2d.setPaint(grad);
g2d.fillRoundRect(barX, barY, fillWidth, BAR_HEIGHT, arc, arc);
}
// Add a glossy effect to the health bar
int glossHeight = BAR_HEIGHT / 3;
GradientPaint gloss = new GradientPaint(
barX, barY, new Color(255, 255, 255, 180),
barX, barY + glossHeight, new Color(255, 255, 255, 30));
g2d.setPaint(gloss);
g2d.fillRoundRect(barX, barY, BAR_WIDTH, glossHeight + arc / 2, arc, arc);
// Draw the border of the health bar
g2d.setColor(BORDER_COLOR);
g2d.setStroke(new BasicStroke(2f));
g2d.draw(track);
// Reset the paint to default
g2d.setPaint(null);
}
}