Skip to main content

[JAVA2D] problem with FontRenderContext's AffineTransform

1 reply [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Doug Felt

Tricia:

I'm not sure what JDK you are running. FontRenderContext.getTransform
should be returning a non-null value. Internally, in later JDKs, it uses a
null transform to represent the identity transform, so perhaps this is what
you are seeing in the debugger. In older JDK's the Graphics2D transform
was not propagated to the FRC, but this should be ok in at least by JDK 1.4
and later-- I'm not able to check at the moment.

The FRC transform is only used to inform the TextLayout about the size and
orientation of the pixels on which it should measure and layout
glyphs. Generally the effects of changes to the transform on line metrics
are small. The FRC transform does not cause the text to scale or rotate,
the transform in effect on the Graphics when you render does that. If your
text is not showing up at all, perhaps you are incorrectly scaling the
advances you get back from the TextLayout before you position it for drawing.

Doug

At 12:33 PM 7/17/2004, Tricia Crichton wrote:
>---------------------- Information from the mail header
>-----------------------
>Sender: Discussion list for Java 2D API
>Poster: Tricia Crichton
>Subject: problem with FontRenderContext's AffineTransform
>-------------------------------------------------------------------------------
>
>Hi,
> I'm trying to add labels to an image generated in the same program.
> I need labels both on the left and right sides of a chart/diagram.
> The right side is easy just using the Graphics2D.drawstring(.....)
>methods.
> For the left side, I previously did the same, using an arbitrary
>space from the edge of the diagram as my starting X point. That works,
>but some of my labels end up closer to the diagram than I'd like (they
>are the longer strings, of course).
> So I thought I'd try using a TextLayout to determine the actual size
>of the string and use that measurement to position the starting X point.
> The function that manages the drawing calls several others, passing
>them the Graphics2D object. This seems to work fine. My problem is that
>I need to change the AffineTransform when I get to the labels to
>eliminate some substantial scaling. This works fine when I use the
>drawString () methods, but the changes to the Graphics2D transform are
>not propigated to the FontRenderContext's AffineTransform, even if I
>don't get that transform until after I make the changes. (Running in a
>debugger, the return value of FontRenderContext.getTransform() is null.)
> The final result is that my left side labels are not appearing at
>all. (I think they are being drawn in the UserSpace outside my image.)
> Any ideas on how I could fix this?
>
>Thanks,
>Tricia Crichton
>
> private void drawLabels(Graphics2D my2d){
> if (!DEBUG){ //the results of these and the following set of
>println's follow the problematic code.
> System.out.println("StChart drawLabels on entry");
> System.out.println(my2d.getTransform().toString());
>
>System.out.println(my2d.getFontRenderContext().getTransform().toString());
> }
> Row row;
> TextLayout label;
> Font f = my2d.getFont();
> float fontSize = f.getSize2D();
> //this is where I change the scale factors
> double transMatrix[] = new double [6];
> my2d.getTransform().getMatrix(transMatrix);
> transMatrix[0] = 1; //eliminate scaling
> transMatrix[3] = 1;
> my2d.setTransform(new AffineTransform(transMatrix));
> /* the above line seems to null out the nested transform:
> StChart drawLabels on entry
> AffineTransform[[44.0, 0.0, 274.75], [0.0, 44.0, 405.1001892089844]]
> AffineTransform[[44.0, 0.0, 274.75], [0.0, 44.0, 405.1001892089844]]
> StChart drawLabels after reset Transform
> AffineTransform[[1.0, 0.0, 274.75], [0.0, 1.0, 405.1001892089844]]
> AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]*/
> //my2d.getTransform().setToScale(1,1);
> /* this line doesn't null out the nested transform, but it also
>doesn't seem to work at all,
> the original Graphics2D transform is unchanged.
> StChart drawLabels on entry
> AffineTransform[[44.0, 0.0, 274.75], [0.0, 44.0, 405.1001892089844]]
> AffineTransform[[44.0, 0.0, 274.75], [0.0, 44.0, 405.1001892089844]]
> StChart drawLabels after reset Transform
> AffineTransform[[44.0, 0.0, 274.75], [0.0, 44.0, 405.1001892089844]]
> AffineTransform[[44.0, 0.0, 274.75], [0.0, 44.0, 405.1001892089844]] */
> if (!DEBUG){
> System.out.println("StChart drawLabels after reset Transform");
> System.out.println(my2d.getTransform().toString());
>
>System.out.println(my2d.getFontRenderContext().getTransform().toString());
> }
> my2d.setPaint(Color.black);
> FontRenderContext frc = my2d.getFontRenderContext();
> if (frc == null) System.out.println("noFontRenderContext!!!!!");
> for (int r = 0; r < getSize(); r += 5){
> row = getRow(r);
> if (row != null){
> label = new TextLayout(row.getRowInd()
> + " (" + Integer.toString(row.getStCount()) + " sts)",f,frc);
> //my2d.drawString(label,
> // getRightLabelX(row), getLabelY(row, fontSize));
> label.draw(my2d,
> getRightLabelX(row), getLabelY(row, fontSize));
> //my2d.drawString(Integer.toString(row.getRowInd()), //old
>(working) code
> // getLeftLabelX(row,label), getLabelY(row, fontSize));
> label.draw(my2d, //new (not working) code
> getLeftLabelX(row,label), getLabelY(row, fontSize));
> }
> }//close cycle rows
> }//close drawLabels
>
>===========================================================================
>To unsubscribe, send email to listserv@java.sun.com and include in the body
>of the message "signoff JAVA2D-INTEREST". For general help, send email to
>listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".