Skip to main content

[JAVA2D] problem with FontRenderContext's AffineTransform

2 replies [Last post]
Anonymous

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".

Reply viewing options

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

Try transforming the font instead. Applying the transform to the FontRenderContext won't work (as someone found out recently in the other Java2D forum).

Jim

I have not worked with the TextLayout, so this may not be right. But
usually you don't want to change the transform of the Graphics2D, you
want to transform the object you are working with and draw it to the
Graphics2D. So I would try something like this.

private void drawLabels(Graphics2D my2d){
AffineTransform transform = new AffineTransform();
// alter the transform to do whatever scaling you need
FontRenderContext frc = new FontRenderContext (transform, true, true);
// create or get a font
TextLayout label = new TextLayout("Hello World", font, frc);
// calc the x and y position
label.draw(my2d, x, y);
}

|*
*|

===========================================================================
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".