Results 1 to 1 of 1
Thread: Problem with AffineTransform
- 01-06-2010, 09:06 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 1
- Rep Power
- 0
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
Thanks,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); } }
PradeepLast edited by pradeep.badam; 01-06-2010 at 09:17 AM.
Similar Threads
-
JScrollPane and AffineTransform
By JReacher in forum New To JavaReplies: 0Last Post: 12-17-2009, 02:47 PM -
[SOLVED] AffineTransform
By robocop in forum New To JavaReplies: 2Last Post: 03-25-2009, 05:54 PM -
AffineTransform help
By tones in forum New To JavaReplies: 4Last Post: 12-19-2008, 07:24 AM -
AffineTransform demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 10:59 PM -
affineTransform rotation
By MichYer in forum AWT / SwingReplies: 0Last Post: 07-18-2007, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks