在 Java 中填充 HUD

Filling a HUD in Java

我决定用上图制作一个HUD,但我不知道Java中需要使用什么命令才能分别填充上半部分和下半部分。

我只知道如何使用 g.fillRect(); 命令,大约需要 20 个这样的命令才能填满上述一半。

public class HUD {

    private Player player;
    private BufferedImage image;
    private Font font;
    private Font font2;
    private  int Phealth = Player.getHealth();

    public HUD(Player p) {
        player = p;
        try {
            image = ImageIO.read(getClass().getResourceAsStream("/HUD/HUD_TEST.gif"));
            font = new Font("Arial", Font.PLAIN, 10);
            font2 = new Font("SANS_SERIF", Font.BOLD, 10);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void draw(Graphics2D g) {

        g.drawImage(image, 0,  10,  null);
        g.setFont(font2);
        g.setColor(Color.black);
        g.drawString("Health:", 30, 22);
        g.drawString("Mana:", 25, 47);
        g.setFont(font);
        g.drawString(Player.getHealth() + "/" + player.getMaxHealth(), 64, 22);
        g.drawString(player.getCubes() / 100 + "/" + player.getMaxCubes() / 100, 55, 47);
        g.setColor(Color.red);
        g.fillRect(1, 25, Phealth * 25, 4);
        g.setColor(Color.blue);
        g.fillRect(1, 31, player.getCubes() / 33, 4);
    }
}

这是目前 HUD 的代码。
任何有助于填充形状的帮助都会有所帮助。

删除了想法 #1!(它似乎没有用。)


好的,想法 #2:

Image1

Image2


Image3

所以,有 3 张 .png 图片。

  • 先画Image1,然后直接在上面画Image2Image3 .
  • 要填充 red/blue 条,clip Image2Image3相应地(即切掉他们的左边)

看看 this 剪辑。
这将需要根据播放器的 HP/Mana 对剪辑多少进行一些小的计算,但它应该足够好。


这是它应该看起来的样子(在 Paint 中完成剪裁和叠加)




更新(问题已解决,使用想法 #2!):

代码:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

@SuppressWarnings("serial")
public class TestGraphics extends JFrame implements ActionListener
{
    JPanel utilBar = new JPanel();

    JButton hpUpBtn = new JButton("HP++");
    JButton hpDownBtn = new JButton("HP--");
    JButton mpUpBtn = new JButton("MP++");
    JButton mpDownBtn = new JButton("MP--");

    GraphicsPanel drawingArea = new GraphicsPanel();

    TestGraphics()
    {   
        setSize(600, 500);
        setLayout(new BorderLayout());

        add(utilBar, BorderLayout.NORTH);
        utilBar.setLayout(new GridLayout(1, 4));

        utilBar.add(hpUpBtn);
        utilBar.add(hpDownBtn);
        utilBar.add(mpUpBtn);
        utilBar.add(mpDownBtn);

        add(drawingArea, BorderLayout.CENTER);

        hpUpBtn.addActionListener(this);
        hpDownBtn.addActionListener(this);
        mpUpBtn.addActionListener(this);
        mpDownBtn.addActionListener(this);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == hpUpBtn) {
            drawingArea.incHp();
        }
        else if (e.getSource() == hpDownBtn) {
            drawingArea.decHp();
        }
        else if (e.getSource() == mpUpBtn) {
            drawingArea.incMp();
        }
        else if (e.getSource() == mpDownBtn) {
            drawingArea.decMp();
        }

        System.out.println("Player HP: " + drawingArea.getHp() +
                " Player MP: " + drawingArea.getMp());

        drawingArea.revalidate();
        drawingArea.repaint();
    }

    public static void main(String[]agrs)
    {
        new TestGraphics();
    }
}

@SuppressWarnings("serial")
class GraphicsPanel extends JPanel {

    private static int baseX = 150;
    private static int baseY = 150;

    private static final int BAR_FULL = 287;
    private static final int BAR_EMPTY = 8;

    private BufferedImage image1 = null;
    private BufferedImage image2 = null;
    private BufferedImage image3 = null;

    private int playerHp = 100;
    private int playerMp = 100;

    public GraphicsPanel() {
        try {
            // All 3 images are the same as those posted in answer
            image1 = ImageIO.read(
                    getClass().getResourceAsStream("/Image1.png"));
            image2 = ImageIO.read(
                    getClass().getResourceAsStream("/Image2.png"));
            image3 = ImageIO.read(
                    getClass().getResourceAsStream("/Image3.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void incHp() { playerHp += (playerHp < 100) ? 5 : 0; }
    public void decHp() { playerHp -= (playerHp > 0) ? 5 : 0; }
    public void incMp() { playerMp += (playerMp < 100) ? 5 : 0; }
    public void decMp() { playerMp -= (playerMp > 0) ? 5 : 0; }

    public int getHp() { return playerHp; }
    public int getMp() { return playerMp; }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Clear the graphics
        g.setClip(null);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 600, 600);

        g.drawImage(image1, baseX, baseY, null);

        int hpPerc = (int) ((BAR_FULL - BAR_EMPTY) * (playerHp / 100.0));
        g.setClip(baseX + BAR_EMPTY + hpPerc, 0, 600, 500);
        g.drawImage(image2, baseX, baseY, null);
        g.setClip(null);

        int mpPerc = (int) ((BAR_FULL - BAR_EMPTY) * (playerMp / 100.0));
        g.setClip(baseX + BAR_EMPTY + mpPerc, 0, 600, 500);
        g.drawImage(image3, baseX, baseY + 78, null);
        g.setClip(null);
    }
}