Hi Everyone,

I have got a an issue with text rendering within Java2D.

When I use small fonts for rotated string drawing I get issue:

it seems that chars of one string have different (or shifted) baselines.

This issue appears for only some rotation agles and is obvious when the string 
contains several identical glyphs (like zero's in the example attached).

The attached image illustrates the problem we are running into, look in 
particular at angles of 20, 40, 160, 340 degrees.

Thanks for letting me know if there is an alternate text rendering method I 
could use to avoid this issue.

 

Thanks in advance,

Oleksiy Palchyk.


===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

<<C:\Documents and Settings\Oleksiy_Palchyk\My Documents\_from_vincent\test_java2d.PNG>>

/*************************************************************************************
* Copyright (c) 2007, Oracle Corporation.
*
* All rights reserved.  No part of this program may be copied or used
* without the express written consent of Oracle Corporation.
*************************************************************************************/

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;


public class Test_Java2D {
    static int width = 300;
    static int height = 300;
    static int hole = 50;
    static int angle = 20;
    
    public static void main (final String[] args) {
        JFrame frame = new JFrame("Test Java2D");

        
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(
        new JPanel(){
	    /**
	     * The size of the text drawn by the component.
	     */
	    int fontSize = 12;

	    /**
	     * The text drawn by the component.
             */
	    String text = "100.0 (XML)";
        
	    /**
	     * The font used to draw text.
	     */
	    Font font = new Font("", Font.PLAIN, 12);

            public void paintComponent (Graphics g) {
		Dimension size = getSize();
		int width = size.width;
		int height = size.height;
		int x = width / 2;
		int y = height / 2;
		
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                
                g2d.clearRect(0, 0, 700, 700);
		g2d.setFont(font);

                Color gray = new Color(200, 200, 200);
                for (int i = 0; i < 360; i+= angle) {
                    g2d.rotate(angle * Math.PI / 180, x, y);
		    g2d.setColor(gray);
		    g2d.drawLine(x + hole - 5, y, x + hole + 100, y);
		    g2d.setColor(Color.BLACK);
		    g2d.drawString(text, x + hole, y);
                } 
              }
          },
          BorderLayout.CENTER);
      frame.setSize(width, height);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.setBackground(Color.white);
    }


}


Reply via email to