javajava-3dhud

Java3d: How to edit a drawn 2d overlay on a Java 3d scene?


I used the technique answered this question: Java3D: Painting 2D HUD over a Canvas3D

And my code is something like this:

...
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D(config){
        public void postRender()
        {
            this.getGraphics2D().setColor(Color.white);
            this.getGraphics2D().drawString("Heads Up Display (HUD) Works!",100,100);
            this.getGraphics2D().flush(false);
        }

    };
...

How could I later remove this 2D HUD or edit this 2D HUD?


Solution

  • If the render renders each frame completely from scratch, you can add, for example, a static boolean in the same class with your postRender() method. Then inside the method use an if statment to check if the boolean if true, otherwise don't do anything, if you then set the boolean to false via

    ClassName.boolean_name=false;
    

    The HUD should not show. As for changing it, that's possible, and would really depend on what type of information you want to show. Here is an example I'd use for games, but it can fit several subjects.

    I would want to store FPS, x, y, and z positions If I was working on a game, so what I would do is create a variable inside the class for each attribute I needed to display, then something like this:

    Graphics2D G2D = this.getGraphics2D()
    G2D.setColor(Color.white);
    G2D.drawString("FPS: "+fps, 100, 100);
    G2D.drawString("X: "+x_pos, 100, 80);
    G2D.drawString("Y: "+y_pos, 100, 60);
    G2D.drawString("Z: "+z_pos, 100, 40);
    G2D.flush(false);
    

    Also if you notice I set G2D equal to this.getGraphics2D(), that way I didn't need to call an external method each time I wanted to use the Graphics2D object, each time you call getGraphics2D() it has to go to that class, call that method, and return the value, as compared to just getting it once, and using it, faster type type, and in the long run creates faster speeds.

    What I would do after all this is I could just set each variable to the correct value, and it would display, that's one way of updating it, you could do similar make a variable for each line, and go:

    line1="this is some text";
    list2="this is some more text";
    list3="etc...";