Fill HUD with Java

I decided to use the above image to make HUD, but I don't know what command to use in Java, because I can fill the upper and lower parts respectively

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,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,Phealth * 25,4);
        g.setColor(Color.blue);
        g.fillRect(1,31,player.getCubes() / 33,4);
    }
}

This is the HUD code so far Any help with filling shapes will help

Solution

Deleted #1 the idea! It seems useless

OK, idea #2:

This search mirrors 2 images 3

So, there are three Png image

>First draw image1, then draw image2 and image3 directly on it. > To fill the red / blue bars, cut image2 and image3 accordingly (i.e. cut off their left side)

Look at this According to the player's HP / Mana, this requires some small calculation of the amount of clips, but it should be good enough

It should be like this (cut and overlay in paint)

Update (problem solved, use idea #2!):

Code:

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,600,600);

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

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

        int mpPerc = (int) ((BAR_FULL - BAR_EMPTY) * (playerMp / 100.0));
        g.setClip(baseX + BAR_EMPTY + mpPerc,500);
        g.drawImage(image3,baseY + 78,null);
        g.setClip(null);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>