Progress bar

so i needed a progress bar and I tried JProgressBar with JMEDesktop but it killed my fps. then i found one in batman.ac's tutorial "HUDs for the total noob" but the code was out of date so I changed it a bit to work for me.bar goes from left to right and you need the image from batmans tutorial(renamed to hudtutorial3.png and background made alpha).it can probably be improved, but it works and doesnt kill fps. : :slight_smile:




package main;

import java.net.URL;
import java.nio.FloatBuffer;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;

public class MyProgressBar {

   private Node hudNode;
   private Quad gauge, hudQuad;
   private int textureWidth;
    private int textureHeight;
   private int minimum = 0, maximum = 100;
   private int widthScale = 0, heightScale = 0;
   private int xpos = 0, ypos = 0;
    final private int width = 34, height = 10;
   
   public MyProgressBar(DisplaySystem display){
      
      hudNode = new Node("hudNode");
        hudQuad = new Quad("hud", 34f, 10f);
             
        hudNode.getLocalTranslation().x =xpos;
        hudNode.getLocalTranslation().y =ypos;
             
        gauge = new Quad("gauge", 32f, 8f);
        gauge.setRenderQueueMode(Renderer.QUEUE_ORTHO);
        LightState ls = display.getRenderer().createLightState();
        ls.setEnabled(false);
        hudNode.setRenderState(ls);
        hudNode.updateRenderState();
        hudNode.attachChild(hudQuad);
        hudNode.attachChild(gauge);
       
        TextureState ts = display.getRenderer().createTextureState();
        ts.setTexture(TextureManager.loadTexture(getClass().getClassLoader()
            .getResource("hudtutorial3.png"), Texture.MM_LINEAR,Texture.FM_LINEAR,0.0f, true));
        textureWidth = ts.getTexture().getImage().getWidth();
        textureHeight = ts.getTexture().getImage().getHeight();
        ts.setEnabled(true);
       
        FloatBuffer texCoords = BufferUtils.createVector2Buffer(4);
        texCoords.put(getUForPixel(0)).put(getVForPixel(0));
        texCoords.put(getUForPixel(0)).put(getVForPixel(10));
        texCoords.put(getUForPixel(34)).put(getVForPixel(10));
        texCoords.put(getUForPixel(34)).put(getVForPixel(0));
        hudQuad.setTextureBuffer(texCoords);
        hudNode.setRenderState(ts);
      
        AlphaState as = display.getRenderer().createAlphaState();
        as.setBlendEnabled(true);
 
        as.setSrcFunction(AlphaState.SB_SRC_ALPHA);
        as.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);
        as.setTestEnabled(false);
        as.setEnabled(true);
        hudNode.setRenderState(as);
       
   }
   
   public int getWidth(){
      return width * widthScale;
   }
   public int getHeight(){
      return height * heightScale;
   }
   
   private float getUForPixel(float xPixel) {
        return (float) xPixel / textureWidth;
    }

    private float getVForPixel(float yPixel) {
        return 1f - (float) yPixel / textureHeight;
    }
   
    public Node getNode(){
      return hudNode;
   }
   
    public void setPosition(int xpos, int ypos){
       hudNode.getLocalTranslation().x =xpos;
        hudNode.getLocalTranslation().y =ypos;
    }
   
   public void setGauge(int value) {
          
      float range = maximum - minimum;
      
      float adjustedRange = value - minimum;
      
      float percent = adjustedRange / range;
      
      float newX = (32 * percent);
         
        FloatBuffer texCoords3 = BufferUtils.createVector2Buffer(4);
       
        texCoords3.put(getUForPixel(32-newX)).put(getVForPixel(56));
        texCoords3.put(getUForPixel(32-newX)).put(getVForPixel(64));
        texCoords3.put(getUForPixel(64-newX)).put(getVForPixel(64));
        texCoords3.put(getUForPixel(64-newX)).put(getVForPixel(56));
       
        gauge.setTextureBuffer(texCoords3);
       
   }
   
   public void setMinimum(int minimum){
      this.minimum = minimum;
   }
   
   public void setMaximum(int maximum){
      this.maximum = maximum;
   }
   
   public void setScale(int width, int height){
      widthScale = width;
      heightScale = height;
      hudNode.setLocalScale(new Vector3f(widthScale,heightScale,1));
   }
   
   private int getScaleWidth(){
      return widthScale;
   }
}

Niceโ€ฆ

Maybe you could think about putting it in the code snippet wiki (and make sure to include your name in it):



http://www.jmonkeyengine.com/wiki/doku.php?id=code_snippets

done, the source on the wiki is more complete also (has JMEDeskop) :smiley: