eclipse - Filling a HUD in Java -
i've decided on making hud picture above, don't know command in java need use can fill top half , bottom half separately.
know how use g.fillrect();
command, , take around twenty of these commands fill said half.
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); } }
this code hud far.
in filling shape help.
removed idea #1! (it didn't seem work.)
okay, idea #2:
image1
image2
image3
so, there 3 .png images.
- draw image1 first, followed drawing image2 , image3 directly on top of it.
- to fill either red/blue bars, clip image2 , image3 accordingly (i.e. cut away left sides)
take @ this on clipping.
require minor calculations on how clip, based on hp/mana of player, should enough.
should (clipping , overlaying done in paint)
update (problem solved, using 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 { // 3 images same 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 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); } }
Comments
Post a Comment