JCommon text anchors not working correctly

A discussion forum for the JCommon class library.
Locked
I82Much
Posts: 5
Joined: Fri Jun 26, 2009 6:55 pm

JCommon text anchors not working correctly

Post by I82Much » Wed Feb 03, 2010 3:38 pm

Hi guys,

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);
    }
}
Here's an image of the output I see:
Image


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.

I82Much
Posts: 5
Joined: Fri Jun 26, 2009 6:55 pm

Re: JCommon text anchors not working correctly

Post by I82Much » Wed Jul 07, 2010 3:27 pm

Well I never did find out what was wrong but I went ahead and implemented it myself.

http://stackoverflow.com/questions/2278 ... hor-points

Code: Select all

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestFontLayout extends JPanel {

    public enum AnchorPoint{
        UPPER_LEFT,
        TOP_CENTER,
        UPPER_RIGHT,
        RIGHT_CENTER,
        LOWER_RIGHT,
        BOTTOM_CENTER,
        LOWER_LEFT,
        LEFT_CENTER,
        CENTER
    };

    public void drawText(TextLayout text, AnchorPoint point, Graphics2D g2, float x, float y) {
        float translationX = 0.0f;
        float translationY = 0.0f;

        Rectangle2D bounds = text.getBounds();
        float midYOffset = (float) bounds.getHeight()/2;
        float midXOffset = (float) -bounds.getWidth()/2;

        float topYOffset = (float) bounds.getHeight();
        float bottomYOffset = 0.0f;

        float leftXOffset = 0.0f;
        float rightXOffset = (float) -bounds.getWidth();

        // Adjust x values
        switch (point) {
            // Left
            case UPPER_LEFT:
            case LOWER_LEFT:
            case LEFT_CENTER:
                translationX = leftXOffset;
                break;
                // Mid
            case TOP_CENTER:
            case BOTTOM_CENTER:
            case CENTER:
                translationX = midXOffset;
                break;
            // Right
            case UPPER_RIGHT:
            case RIGHT_CENTER:
            case LOWER_RIGHT:
                translationX = rightXOffset;
        }

        // Adjust y values
        switch (point) {
            // Top
            case UPPER_LEFT:
            case UPPER_RIGHT:
            case TOP_CENTER:
                translationY = topYOffset;
                break;
            // Mid
            case LEFT_CENTER:
            case CENTER:
            case RIGHT_CENTER:
                translationY = midYOffset;
                break;
            // Bottom
            case LOWER_LEFT:
            case BOTTOM_CENTER:
            case LOWER_RIGHT:
                translationY = bottomYOffset;
        }

        text.draw(g2, x + translationX, y + translationY);

    }

    public TestFontLayout() {
        super();
        setPreferredSize(new Dimension(400,400));
    }
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        // Ensure that the default painting occurs
        super.paintComponent(g);

        Point2D loc = new Point2D.Double(getWidth()/2,getHeight()/2);
        Font font = Font.decode("Helvetica");
        FontRenderContext frc = g2.getFontRenderContext();
        TextLayout layout = new TextLayout("This is a string", font, frc);

        g2.setColor(Color.RED);
        g2.drawRect(getWidth()/2, getHeight()/2, 1,1);

        g2.setColor(Color.PINK);
        drawText(layout, AnchorPoint.UPPER_LEFT, g2, 0, 0);

        g2.setColor(Color.BLUE);
        drawText(layout, AnchorPoint.TOP_CENTER, g2, getWidth()/2, 0);

        g2.setColor(Color.ORANGE);
        drawText(layout, AnchorPoint.UPPER_RIGHT, g2, getWidth(), 0);

        g2.setColor(Color.CYAN);
        drawText(layout, AnchorPoint.RIGHT_CENTER, g2, getWidth(), getHeight()/2);

        g2.setColor(Color.ORANGE);
        drawText(layout, AnchorPoint.LOWER_RIGHT, g2, getWidth(), getHeight());

        g2.setColor(Color.BLACK);
        drawText(layout, AnchorPoint.BOTTOM_CENTER, g2, getWidth()/2, getHeight());


        g2.setColor(Color.YELLOW);
        drawText(layout, AnchorPoint.LOWER_LEFT, g2, 0, getHeight());

        g2.setColor(Color.DARK_GRAY);
        drawText(layout, AnchorPoint.LEFT_CENTER, g2, 0, getHeight()/2);


        g2.setColor(Color.MAGENTA);
        drawText(layout, AnchorPoint.CENTER, g2, getWidth()/2, getHeight()/2);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.add(new TestFontLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }
}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: JCommon text anchors not working correctly

Post by david.gilbert » Sun Sep 01, 2013 1:14 pm

Way too late, but the bug getting in your way has been fixed in JCommon 1.0.20. Thanks for posting the example code, it helped a lot.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked