Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java3D - On Screen Text - HUD - Simple 2D Text?  (Read 4558 times)
0 Members and 1 Guest are viewing this topic.
Offline Ben_3D

Junior Newbie





« Posted 2006-05-06 21:09:07 »

Hi all,
  I'm new to java 3d, and am wondering how I go about putting some simple text on screen...I've been reading around and have tried Text3D and Text2D, but they seem to be releative to the 3d world...and my camera is moving around so the text isn't really 2d on screen text.

My simple 3d class extends the JFrame, and wanted to know if there was any examples of using java3d and putting some 2d text on screen...like a counter or something.

Thanks,

Ben

Quote
class MainWindow extends JFrame implements KeyListener
{
..
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #1 - Posted 2006-05-06 22:05:54 »

I have been trying to figure this same thing out for about a day now. And after realizing that javax.media.j3d.Canvas3D extends java.awt.Canvas and java.awt.Canvas exends java.awt.Component, you can simply call the getGraphics() method, like this:
Quote
Canvas3D canvas = new Canvas3D(config);
Graphics g = canvas.getGraphics();
Then use the traditional drawing methods of java.awt.Graphics to draw directly to the Canvas3D.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline Ben_3D

Junior Newbie





« Reply #2 - Posted 2006-05-06 23:52:46 »

Hi Jamison,
  well thanks for your idea...I tried this:

1  
2  
3  
Graphics g = canvas.getGraphics();
g.setFont(new Font("TimesRoman", Font.ITALIC, 28));
g.drawString("abcdefeghijklmonopdkdkdkd",80, 80);


But what I find is, no matter how I do it, I always seem to get a flicker...never seems like a smooth on screen text....its like every few frames it flickers or something...any ideas on how to fix this?

Thanks,

Ben

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #3 - Posted 2006-05-07 00:14:00 »

I don't have any problems. Perhaps your doing something wrong? Here's some sample code:
Quote
import java.awt.*;
import javax.swing.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;

public class Test extends JFrame implements Runnable {
   int fps = 30;
   Graphics screen;

   public Test() {
      setSize(400, 300);
      setResizable(false);
      setDefaultCloseOperation(EXIT_ON_CLOSE);

      GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
      Canvas3D canvas = new Canvas3D(gc);
      getContentPane().add(canvas);

      BranchGroup scene = new BranchGroup();
      scene.compile();

      SimpleUniverse su = new SimpleUniverse(canvas);
      su.addBranchGraph(scene);

      setVisible(true);

      screen = canvas.getGraphics();

      new Thread(this).start();
   }

   public void run() {
      while(true) {
         try {
            screen.setColor(Color.white);
            screen.setFont(new Font("TimesRoman", Font.ITALIC, 28));
            screen.drawString("abcdefeghijklmonopdkdkdkd",80, 80);

            Thread.currentThread().sleep(1000/fps);
         } catch(InterruptedException e){}
      }
   }

   public static void main(String args[]) {
      new Test();
   }
}

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #4 - Posted 2006-05-08 09:56:59 »

I'm sorry, but that source doesn't look like a particularly good solution.

The 2D rendering is not synchronized with the 3D rendering at all.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline Jamison

Junior Member




We're all idiots in one way or another.


« Reply #5 - Posted 2006-05-08 14:20:42 »

You know what? I'm a beginner to J3D too. I was only giving him source code that worked for me from what I found myself by looking through the documentation.

I don't like you. Check out my site Smiley www.gamedevforums.com
Offline Breakfast

Senior Member




for great justice!


« Reply #6 - Posted 2006-05-09 17:55:50 »

Here are a couple of relevant threads:
Dynamic Textures for UI followed by Best Approach to UI Panes also HUD on top of Canvas3D.
Offline Mr. Gol

Senior Member


Medals: 1



« Reply #7 - Posted 2006-05-10 11:30:02 »

Something like this. You have to override Canvas3D.postRender() to make sure the HUD is drawn everytime a frame is rendered. This is supposed to be quite slow, but since drawAndFlushImage was introduced it works good enough.

Quote

public class Viewport extends Canvas3D {

   private Dimension resolution;      
   private BufferedImage buffer;
   private Graphics2D g2;
   private J3DGraphics2D g3;
   
   public Viewport(GraphicsDevice gd,Dimension resolution) {
      
      super(gd.getBestConfiguration(new GraphicsConfigTemplate3D()));
      
      this.resolution=resolution;
      
      buffer=new BufferedImage(resolution.width,resolution.height,BufferedImage.TYPE_4BYTE_ABGR);
      g2=buffer.createGraphics();
      g3=this.getGraphics2D();
      
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
   }   

   public void postRender() {
      
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,0f));
      g2.setColor(new Color(0,0,0));
      g2.fillRect(0,0,resolution.width,resolution.height);
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1f));
         
      hud.renderFrame(g2,resolution.width,resolution.height);
         
      g3.drawAndFlushImage(buffer,0,0,this);   
   }
}

public class HUD {

   (...)

   public void renderFrame(Graphics2D g2,int width,int height) {

      // Drawing code goes here
   }
}


Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #8 - Posted 2006-05-10 16:22:50 »

Been a while since I worked with Java 3D, but using postRender() used to have an awful performance impact - anyone know if this is still the case.

We used to end up aligning quads with the screen and using dynamic textures for optimum performance.

Kev

Offline endolf
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #9 - Posted 2006-05-11 13:06:39 »

Hi

It's probably suffered a bit of bit rot by now, but there is a HUD system for Java 3D and a demo application on the newdawn software site here.

HTH

Endolf

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Conzar

Junior Member




There is nothing common about common sense


« Reply #10 - Posted 2006-10-03 04:41:18 »

Quote
Been a while since I worked with Java 3D, but using postRender() used to have an awful performance impact - anyone know if this is still the case.

Well as of J3D1.4.0_01 the performance is still bad (I am going through the pains of having to convert over to a new hud b/c of it).  By using JProfiler I discovered that postRender was taking up 68% of all computation time.  Thats horrible for just displaying hud stuff. 

Well, I think I'm gonna try and use the HUD stuff from j3d.org.

Ubuntu
Offline Conzar

Junior Member




There is nothing common about common sense


« Reply #11 - Posted 2006-10-04 05:21:18 »

Hey.
   So I am trying to use the j3d overlay package and I can't seem to get it to work.
This is what I am doing:
* Extended Canvas3D (I was previously using drawAndFlushImage but commented out that code)
* Added ImageOverlay with a BufferedImage.
* Getting Graphics2D from image and drawing to it the HUD Components


So here is what I do when creating Image Overlay in the constructor of Canvas3D
1  
2  
3  
4  
5  
6  
BufferedImage imageBuffer = OverlayUtilities.createBufferedImage(dimension,true);
Graphics2D g2d = imageBuffer.createGraphics();

ImageOverlay imageOverlay = new ImageOverlay(this,dimension,true,true,imageBuffer);
imageOverlay.initialize();
imageOverlay.setVisible(true);


Then in the postRender function
1  
2  
3  
// do update HUD graphics with g2g
updateHUDGraphics(g2g);
imageOverlay.repaint()


Am I doing something wrong?

Ubuntu
Offline Conzar

Junior Member




There is nothing common about common sense


« Reply #12 - Posted 2006-10-07 21:17:18 »

Well, I've asked the question over at the java.net forums about methods for rendering HUDS.

A poster replied and mentioned that performance is affected by the size of the image that you call drawAndImageFlush on.

Check it out here

http://forums.java.net/jive/thread.jspa?threadID=18922

Ubuntu
Offline Conzar

Junior Member




There is nothing common about common sense


« Reply #13 - Posted 2006-10-12 04:35:45 »

ok, I've just converted over to drawing to seperate images (panes) like described in the above post and I'm getting worse performance.
I'm down to 15FPS instead of 30FPS with the all in one image. 

Any suggsetions?

How about using the PlatformGeometry class to do all 2d drawing.  Is there anything to be aware of when using PlatformGeometry?

Ubuntu
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (37 views)
2013-05-17 21:29:12

alaslipknot (46 views)
2013-05-16 21:24:48

gouessej (75 views)
2013-05-16 00:53:38

gouessej (75 views)
2013-05-16 00:17:58

theagentd (83 views)
2013-05-15 15:01:13

theagentd (77 views)
2013-05-15 15:00:54

StreetDoggy (119 views)
2013-05-14 15:56:26

kutucuk (143 views)
2013-05-12 17:10:36

kutucuk (143 views)
2013-05-12 15:36:09

UnluckyDevil (153 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.098 seconds with 22 queries.