I'm running into problems using TextLines and TextAAnchors; the rendering of them does not seem to respect the text anchors. Here's a SCSE to illustrate what I'm talking about
Code: Select all
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import org.jfree.text.TextFragment;
import org.jfree.text.TextLine;
import org.jfree.ui.TextAnchor;
/**
*
* @author NDUNN
* @date Feb 3, 2010
*/
public class TestTextLine extends JComponent {
public TestTextLine() {
super();
setPreferredSize(new Dimension(100,100));
}
/**
*
* @param g
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
TextLine redTextLine = new TextLine("Top right", TextFragment.DEFAULT_FONT, Color.RED);
TextLine blueTextLine = new TextLine("Center", TextFragment.DEFAULT_FONT, Color.BLUE);
TextLine greenTextLine = new TextLine("Top left", TextFragment.DEFAULT_FONT, Color.GREEN);
int x = getWidth()/2;
int y = getHeight()/2;
g2.drawOval(x-1, y-1, 2, 2);
redTextLine.draw(g2, x, y, TextAnchor.TOP_RIGHT, 0, 0, 0);
blueTextLine.draw(g2, x, y, TextAnchor.CENTER, 0, 0, 0);
greenTextLine.draw(g2, x, y, TextAnchor.TOP_LEFT, 0, 0, 0);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new TestTextLine());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Note that the top left and top right are drawn on top of each other, meaning the top right is not in its correct place. Furthermore, the center is not being drawn centered around the point.
What is going on here? I have pried into the source code and see code in the TextUtilities class that deals with calculating offsets from the TextAnchors, and it appears to do what it should. I'm at a loss.
I am using JCommon-1.0.16.