Results 1 to 1 of 1
  1. #1
    pradeep.badam is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Unhappy Problem with AffineTransform

    Hi,

    Text is drawn to Panel using Graphics2D, TextLayout and AffineTransform. A rectangle is drawn around the text where one side of rectangle touches text. When I rotate Text and Rectangle by 90 degrees around the center, text is shifted by one pixel away from the side of the rectangle. Any help is appreciated.

    Please see the attachements with Text at 0 degree and Text at 90 degrees.
    Have a look how character 'D' is drawn. It’s touching rectangle completely in horizontal (at zero degree) and not touching in vertical (90 degree).

    Please see the below code: For rotation change the angle from 0 to 90

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.font.TextAttribute;
    import java.awt.font.TextLayout;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Rectangle2D;
    import java.text.AttributedString;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class DrawText extends JPanel {
    
    	public DrawText() {
    		setBackground(Color.white);
    		setSize(350, 400);
    
    	}
    
    	private static void adjustGraphics(Graphics2D g) {
    		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    		g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    				RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    		g.setRenderingHint(RenderingHints.KEY_RENDERING,
    				RenderingHints.VALUE_RENDER_QUALITY);
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		adjustGraphics(g2);
    		g2.setColor(Color.black);
    		String toPrint = "Draw Text Test!";
    
    		double nX = 100, nY = 100, width = 100, height = 0;
    		double angle = 0;// Change the angle from 0 to 90
    		angle = Math.toRadians(angle);
    
    		FontMetrics fm = this.getFontMetrics(g2.getFont());
    		height = fm.getHeight();
    
    		AffineTransform afNew = new AffineTransform();
    		afNew.rotate(-angle, nX + width / 2.0, nY + height / 2.0);
    		g2.setTransform(afNew);
    
    		g2.draw(new Rectangle2D.Double(nX, nY, width, height));
    		nY = nY + (fm.getAscent() - fm.getLeading());
    
    		AttributedString attStr = new AttributedString(toPrint);
    		attStr.addAttribute(TextAttribute.FONT, g2.getFont());
    		TextLayout textLayout = new TextLayout(attStr.getIterator(), g2
    				.getFontRenderContext());
    		nX = nX - textLayout.getBounds().getX();
    		textLayout.draw(g2, (float) nX, (float) nY);
    	}
    
    	public static void main(String arg[]) {
    		JFrame frame = new JFrame();
    		frame.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    		frame.getContentPane().add("Center", new DrawText());
    		frame.pack();
    		frame.setSize(new Dimension(350, 400));
    		frame.setVisible(true);
    	}
    }
    Thanks,
    Pradeep
    Attached Thumbnails Attached Thumbnails Problem with AffineTransform-text-0-degree.jpg   Problem with AffineTransform-text-90-degree.jpg  
    Last edited by pradeep.badam; 01-06-2010 at 10:17 AM.

Similar Threads

  1. JScrollPane and AffineTransform
    By JReacher in forum New To Java
    Replies: 0
    Last Post: 12-17-2009, 03:47 PM
  2. [SOLVED] AffineTransform
    By robocop in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 06:54 PM
  3. AffineTransform help
    By tones in forum New To Java
    Replies: 4
    Last Post: 12-19-2008, 08:24 AM
  4. AffineTransform demo
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:59 PM
  5. affineTransform rotation
    By MichYer in forum AWT / Swing
    Replies: 0
    Last Post: 07-18-2007, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •