[solved] HUDs, Java2D, Text and coordinate system conversion problems

Hello again…



I’ve played around with the  “huds for the total noob” tutorial, especially

the last section using Java2D.



I want to draw a text (rendered by java2d). This works but everything is inverted.



screenshot



Hmm, something with the coordinate system (my noobish guess…)



“TextureManager” has a “flip” parameter in the constructor… fine, but I don’t use TextureManager if I use Java2D.



This is the “paint” method of my class “StartMenu”:


        g.setBackground (new Color (0f, 0f, 0f, 0f));
        g.clearRect (0, 0, getWidth (), getHeight ());
        font = new Font("SansSerif", Font.PLAIN, 36 );
        g.setFont (font);
        g.setColor (new Color (0f, 1f, 0f, 1f));
        g.drawString ("1 = Neu starten", 20, 20);
        g.drawString ("2 = Ende", 20, 56);



(Yeah, in German... "1 = start", "2 = end", that simple..)

This is the part where i "load" my texture in the game (... where I get the
image data from my "StartMenu" class generated via Java2D.


        startMenu = new StartMenu();
        Texture texture = new Texture();
        texture.setApply(Texture.AM_MODULATE);
        texture.setBlendColor(new ColorRGBA(1, 1, 1, 1));
        texture.setFilter(Texture.MM_LINEAR);
        texture.setImage(startMenu);
        texture.setMipmapState(Texture.FM_LINEAR);
        infoQuad_ts = display.getRenderer().createTextureState();
        infoQuad_ts.setTexture(texture);
        // infoQuad_ts.setCorrection(TextureState.CM_PERSPECTIVE);
        infoQuad_ts.setEnabled(true);
        infoQuad.setRenderState(infoQuad_ts);
        infoQuad.updateRenderState();
       




Any input?

I've read some stuff about "affine transformations" in Java2d, but every example is about
rotating, scaling, sheering and stuff... but a coordinate system conversion.. hmmm..

Thanks in advance..

the origin in awt is top-left while in opengl it is bottom-left. that's why your generated image is flipped vertically.

you'll have to flip your image using java2d if you don't use the TextureManager.

Thanks sfera, I’ve thought about using Java2d for flipping at the very beginning,

but it was hard to find this special case… scaling, translating, rotating… no problem.



But “flipping” or “coordinate system translation”… no good hits in the net…



But there is comment at javalobby…



http://www.javalobby.org/java/forums/t19387.html



…and if you do it right (=“right order”) it works:



        AffineTransform tx = AffineTransform.getScaleInstance(1.0, -1.0);
        g.setTransform (tx);
        g.setBackground (new Color (0f, 0f, 0f, 0f));
        g.clearRect (0, 0, getWidth (), getHeight ());
        font = new Font("SansSerif", Font.PLAIN, 36 );
        g.setFont (font);
        g.setColor (new Color (0f, 1f, 0f, 1f));
        g.drawString ("1 = Neu starten", 0, -150);
        g.drawString ("2 = Ende", 0, -120);



The coordinates of the "drawString" method calls are not perfect, of course
(static, should be dynamic in relation to the size of the texture), but I think
you can see the trick.

did you try to call g.setTransform(AffineTransform.getScaleInstance(1.0, -1.0)); after drawing the strings (using "normal" coordinates)?

i'm too lazy to try it out myself :wink:



…and it's always about the "right order" :slight_smile:

sfera said:

did you try to call g.setTransform(AffineTransform.getScaleInstance(1.0, -1.0)); after drawing the strings (using "normal" coordinates)?
i'm too lazy to try it out myself ;)

...and it's always about the "right order" :)


Nope, doesn't work! You've got to call the setTransform method before the draw methods, as it seems..

If I do it your way, the text is still "flipped".

my mistake: i meant transform and not setTransform 

sfera said:

my mistake: i meant transform and not setTransform 


Doesn't work either.

But I noticed that you have to change the lines


        g.setTransform (tx);
        g.setBackground (new Color (0f, 0f, 0f, 0f));
        g.clearRect (0, 0, getWidth (), getHeight ());



to

       
        g.setBackground (new Color (0f, 0f, 0f, 0f));
        g.clearRect (0, 0, getWidth (), getHeight ());
        g.setTransform (tx);



Otherwise the setting of the background (and clear call)
won't work.. (i guess it would work if I would adapt
the coordinates of clearRect to the new coordinate system).