gpt4 book ai didi

java - 光滑 - 填充矩形

转载 作者:行者123 更新时间:2023-11-30 04:43:40 25 4
gpt4 key购买 nike

我正在尝试使用 Slick 渲染健康栏。我想要一个红色矩形,前面有一个绿色矩形来显示剩余的生命值。这是我到目前为止所拥有的:

        Rectangle healthBG = new Rectangle(healthX, healthY, healthWidth, healthHeight);
ShapeFill healthFill = new GradientFill(0, healthHeight / 2, Color.red, healthWidth, healthHeight - 1, Color.orange, true);

float healthGAWidth = ((float) health / (float) maxHealth) * (float) healthWidth;
Rectangle healthGA = new Rectangle(healthX, healthY, healthGAWidth, healthHeight);
ShapeFill healthGAFill = new GradientFill(0, healthHeight / 2, Color.green, healthGAWidth, healthHeight - 1, Color.green, true);

//g.setColor(Color.red);
g.draw(healthBG, healthFill);
//g.setColor(Color.green);
g.draw(healthGA, healthGAFill);

这是屏幕上呈现的内容:

Screenshot of health bar in action

最佳答案

问题是您使用了错误的渲染方法。在图形中有两种类型的方法:draw() 和 fill()。你的渲染线应该是:

g.fill(healthBG, healthFill);

或者,您可以跳过制作矩形或 ShapeFill,因为 Graphics 有一个填充矩形的方法:

float healthGAWidth = ((float) health / (float) maxHealth) * (float) healthWidth;
g.fillRect(healthX, healthY, healthWidth, healthHeight);
g.fillRect(healthX, healthY, healthGAWidth, healthHeight);

关于java - 光滑 - 填充矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com