[英]How can I create a health bar in the compiler relative to the max health and length of the bar?
[英]Java Game Dev - How do I create a vertical health bar?
我需要一些幫助來修復我的垂直健康欄。 它正確跟蹤健康並更新,但由於 Java 的繪制方式(從左上角,即 (0,0)),條形圖似乎是顛倒的。 我想翻轉健康欄,使其正確顯示,但我不確定如何操作。
代碼 x 和 y 中的划分只是為了將條形放置在畫布上的正確位置。 乘數是擴大健康欄以適合我為它制作的圖形疊加層。
private void drawHealthBar(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(WIDTH - (WIDTH / 50), HEIGHT / 2, 10, ships.get(0).getHealth() * 6);
}
使用 transform() 和 translate() 設置您想要的坐標系。 所以:
您希望原點位於 (0, height); 左下方。 那么你想翻轉 Y 軸。 示例代碼:
AffineTransform tform = AffineTransform.getTranslateInstance( 0, height);
tform.scale( 1, -1);
g2.setTransform( tform);
您需要根據剩余的生命值移動條,如下所示:
private void drawHealthBar(Graphics g) {
final Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
final int w = 10;
final int h = ships.get(0).getHealth() * 6; // calculate height first
final int x = WIDTH - (WIDTH / 50);
final int y = HEIGHT / 2 + (MAX_HEIGHT - h); // shift by height of whitespace
g2.fillRect(x, y, w, h);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.