Skip to main content

2D HUD on top of 3D Canvas

8 replies [Last post]
y3m5
Offline
Joined: 2007-06-03

Hi, I'm trying to make a simple 3d action game.

I need to know how to draw 2d graphics and text (such as health, pictures of the weapon, etc) on top of the Canvas3D.

Is there a specific known best way to do this?

Thanks,
y3m5

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
endolf
Offline
Joined: 2003-06-10

Back in the days of Java3D 1.3 I did a HUD package that allowed you to place and size components in the HUD and draw on them using Java2D. I know one guy has recently started using it in Java3D 1.5, so it should work. I'm not using Java3D any more for my own projects, but I'd be happy to answer questions on it.

You can pick it up from http://www.newdawnsoftware.com/resources/ under early access libraries.

HTH

Endolf

mokopa
Offline
Joined: 2005-11-14

Try this...?

public class My3DThing {

private Canvas3D c3d;
...

public My3DThing() {
...
GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
c3d = new OverlayCanvas(gc, 512, 512, null);
...
}

...
}

class OverlayCanvas extends Canvas3D {
private GraphicsContext3D gc3d;
private J3DGraphics2D j3dg2d;
private Image ttl;
private URL logourl;
...

public OverlayCanvas(GraphicsConfiguration gconfig, int w, int h, Image img) {
super(gconfig);
this.width = w;
this.height = h;
bufim = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
g2d = bufim.createGraphics();
j3dg2d = this.getGraphics2D();
try {
this.logo = img;
} catch (Exception e) {
System.err.println("Couldn't load logo: " + e);
}
MediaTracker mt = new MediaTracker(this);
ttl = Toolkit.getDefaultToolkit().getImage("./models/screen.png");
mt.addImage(ttl, 0);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("DrawOnCanvas3D: Could not load images! " + e);
}
mt.removeImage(ttl);
}

public void postRender() {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 1.0f));
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
g2d.drawImage(ttl, 2, 2, null);

j3dg2d.drawAndFlushImage(bufim, 0, 0, this);
}
}

y3m5
Offline
Joined: 2007-06-03

OK, I just found an answer on the web for this here:

http://www.cs.bris.ac.uk/Teaching/Resources/COMSM0105/J3D/Examples/Platf...

You use PlatformGeometry!

Any additional info on this would be welcome!

Thanks,
y3m5

cyguard
Offline
Joined: 2005-05-24

With the postRender() method you can draw directly in 2d, or for particular purposes you can try also Billboard Behavior.
Bye

Message was edited by: cyguard

Henjo van Rees

Hi,

I've been toying with 2D overlays in my Java3D scene for a while, but I am
kinda stuck.
I have a scene which is based on the DOT3 examples. When I try to place an
image on my scene with the postRender() function it only works when the
bumpmapped object is removed. Otherwise it's not shown.

If needed I can post some code for my own Canvas3D class, but it's pretty
straightforward.

Does anyone have an idea what may be going wrong?

--Henjo

On Tue, Aug 07, 2007 at 06:25:20AM -0700, java3d-interest@javadesktop.org wrote:
> With the postRenderer method you can draw directly in 2d, or for particular purposes you can try also Billboard Behavior.
> Bye
> [Message sent by forum member 'cyguard' (cyguard)]
>
> http://forums.java.net/jive/thread.jspa?messageID=229841
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@java3d.dev.java.net
> For additional commands, e-mail: interest-help@java3d.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@java3d.dev.java.net
For additional commands, e-mail: interest-help@java3d.dev.java.net

y3m5
Offline
Joined: 2007-06-03

Sorry for double post. Also interested in: how do you make a mouselook effect so you can rotate the view with you mouse like in the Quake games? I tried installing a MouseListener and MouseMotionListener but they didn't work. I tried continually requesting the focus and that didn't work. I tried putting the listeners in the frame containing the panel with the Canvas3D. That didn't work. So in sum, 2 questions:
* 2d graphics/text on top of Canvas3d
* Reading the mouse to position the view. (Am I going to have to resort to some behavior I haven't heard of yet for the TransformGroup effecting the view or something???)

Confused,
y3m5

pmpowers
Offline
Joined: 2006-08-15

You will greatly benefit by perusing the pdf's here:

http://java.sun.com/developer/onlineTraining/java3d/

And can build off of example classes that suit your needs:

https://java3d.dev.java.net/binary-builds.html

You need to use 'behaviors' for mouse interaction. I've not been pleased with results using PlatformGeometry and you may want to consider using J3DGraphics2D. On a Canvas3D you can getGraphics2D() that returns a specialized Graphics2D that you can draw onto. Override the postRender() method on a Canvas3D and do your drawing there...

@Override
public void postRender() {
getGraphics2D().drawAndFlushImage(yourBufferedImage);
}

I've always understood that it is better to do all your drawing in a BufferedImage and then use drawAndFlushImage(), but I suppose you can use any Graphics drawing methods. I've had flickering problems as the BI is updated. Can anyone familiar with the inner workings of the J3D rendering pipeline suggest the best way to use J3DGraphics2D that is changing constantly in response to user input.? Should I be using the ImageObserver interface to greater effect?

y3m5
Offline
Joined: 2007-06-03

Thanks, pmpowers!