为什么边界不画?

时间:2019-05-11 21:50:17

标签: java swing border

我正在尝试为游戏创建健康栏,但每个矩形周围的黑色边框未显示。我正在使用Java Swing / GUI。有人可以帮我弄清楚为什么没有显示边框吗?我正在使用Graphics.setStroke()来绘制较粗的笔画,但笔画没有显示。

我的Java版本是Java 8

我尝试了多种选择,但没有一个真正适用于绘制健康栏黑色边框。

供参考,我的问题在代码行中-         (((Graphics2D)g).setStroke(new BasicStroke(3));

    public static void drawHealthBar(int x, int y, Graphics g, String characterName, int currentHealth, int totalHealth, int direction)
    {
        //Draw Initial Bar
        final int HEIGHT_NAME_BOX = 40;
        int sizeFont = g.getFont().getSize();

        int totalWidth = direction * (sizeFont * characterName.length()); //20 pixels for additional 'middle' feeling
        g.setColor(Color.RED); //Red fill inside the box
        g.fillRect(x, y, totalWidth , HEIGHT_NAME_BOX);

        if(currentHealth > 0) //Draws actual health inside healthbar
            g.fillRect(x + totalWidth + 1 ,y + (HEIGHT_NAME_BOX / 4) , currentHealth * direction, (HEIGHT_NAME_BOX / 2));

        g.setColor(Color.BLACK); //Creates black outline around red name box PROBLEM LOCATED HERE
        ((Graphics2D)g).setStroke(new BasicStroke(3));      

        g.drawRect(x, y, totalWidth, HEIGHT_NAME_BOX);
        g.drawRect(x + totalWidth + 1 ,y + (HEIGHT_NAME_BOX / 4), totalHealth * direction, (HEIGHT_NAME_BOX / 2));

        //Drawing String inside box

        g.setColor(Color.WHITE);
        int xCoord = direction > -1 ? (int) (x + (totalWidth / 4)) : (int) (x + totalWidth * 0.75);
        g.drawString(characterName, xCoord, y + (HEIGHT_NAME_BOX / 2));
    }

0 个答案:

没有答案