Here is an example of Java code for a JPanel that implements a fully functional status screen with health bar, mana bar, energy bar, strength, dexterity, intelligence, and luck:
import javax.swing.*;
import java.awt.*;
public class StatusScreen extends JPanel {
private int health;
private int maxHealth;
private int mana;
private int maxMana;
private int energy;
private int maxEnergy;
private int strength;
private int dexterity;
private int intelligence;
private int luck;
public StatusScreen(int health, int maxHealth, int mana, int maxMana, int energy, int maxEnergy,
int strength, int dexterity, int intelligence, int luck) {
this.health = health;
this.maxHealth = maxHealth;
this.mana = mana;
this.maxMana = maxMana;
this.energy = energy;
this.maxEnergy = maxEnergy;
this.strength = strength;
this.dexterity = dexterity;
this.intelligence = intelligence;
this.luck = luck;
setPreferredSize(new Dimension(400, 250));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Draw a black background
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
// Draw health bar
drawBar(g2d, "Health", health, maxHealth, 20);
// Draw mana bar
drawBar(g2d, "Mana", mana, maxMana, 60);
// Draw energy bar
drawBar(g2d, "Energy", energy, maxEnergy, 100);
// Draw strength
drawStat(g2d, "Strength", strength, 20, 140);
// Draw dexterity
drawStat(g2d, "Dexterity", dexterity, 20, 160);
// Draw intelligence
drawStat(g2d, "Intelligence", intelligence, 180, 140);
// Draw luck
drawStat(g2d, "Luck", luck, 180, 160);
}
private void drawBar(Graphics2D g2d, String text, int value, int maxValue, int y) {
// Calculate the percentage of the value relative to the maximum value
int fillWidth = (int) ((double) value / maxValue * getWidth() * 0.8);
// Draw the text
g2d.setColor(Color.WHITE);
g2d.drawString(text, 20, y + 10);
// Draw the empty bar
g2d.setColor(Color.GRAY);
g2d.fillRect(120, y, getWidth() - 140, 20);
// Draw the filled bar
g2d.setColor(Color.GREEN);
g2d.fillRect(120, y, fillWidth, 20);
}
private void drawStat(Graphics2D g2d, String text, int value, int x, int y) {
// Draw the text
g2d.setColor(Color.WHITE);
g2d.drawString(text + ": " + value, x, y);
}
public static void main(String[] args) {
// Example usage
JFrame frame = new JFrame("Status Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StatusScreen statusScreen = new StatusScreen(80, 100, 50, 100, 75, 100, 15, 20, 10, 5);
frame.add(statusScreen);
frame.pack();
frame.setVisible(true);
}
}
This code creates a StatusScreen JPanel that receives the status values as constructor arguments and draws the status bars and statistics on it. The paintComponent method is overridden to paint the background, status bars, and statistics. The drawBar method draws a bar with a text indicating the value and the drawStat method draws a statistic with its corresponding value. In the main method, you can test the StatusScreen by adding it to a JFrame.