How to rotate text with Graphics2D in Java?
Asked Answered
M

4

25

I want to rotate text on a JPanel using Graphics2D..

My code is this:

double paso=d.width/numeroBarras;
        double alto=datos[i].valor;
        Font fBarras=new Font("Serif", Font.PLAIN, 15);
        g2.setFont(fBarras);
        Rectangle2D barra=new Rectangle2D.Double(x,d.height-alto,paso,alto);
        //g2.fill(barra);
        x+=paso;
        g2.draw(barra);
        g2.rotate(-Math.PI/2);
        g2.setColor(Color.BLACK);
        g2.drawString(datos[i].titulo,(float)alto,(float)paso)

This method must draw a rectangle and a text over the rectangle, but when i run this method all the graphic is rotated and i just want rotate the text ..

Thanks :)

Marston answered 10/4, 2012 at 6:9 Comment(0)
E
31

The method Graphics2D.rotate applies transform to all subsequent rendering operations. You can preserve a copy of transform (with getTransform()) before applying rotation, and then restore the original.

g2.draw(barra);
AffineTransform orig = g2.getTransform();
g2.rotate(-Math.PI/2);
g2.setColor(Color.BLACK);
g2.drawString(datos[i].titulo,(float)alto,(float)paso);
g2.setTransform(orig);
Electromagnet answered 10/4, 2012 at 6:26 Comment(3)
So it rotates the string but not the coordinate system?Varietal
It modifies current transform of rendering context. Graphics2D reference states that the transform is used to convert from User Space coordinate system to Device Space. So, it rotates coordinate system:) The last line of this example is restoring transform after string rendering.Electromagnet
Right! This method rotates the whole coordinate system, this X and Y of the drawString are also meant in the rotated coordinate system. So you are likely drawString in a translated coordinate system to a 0, 0 position instead.Chant
A
32

This method will rotate the text and will render all other shapes the same.

Graphics2D g2 = (Graphics2D) g;
Font font = new Font(null, Font.PLAIN, 10);    
AffineTransform affineTransform = new AffineTransform();
affineTransform.rotate(Math.toRadians(45), 0, 0);
Font rotatedFont = font.deriveFont(affineTransform);
g2.setFont(rotatedFont);
g2.drawString("A String",0,0);
g2.dispose();
Asha answered 30/3, 2014 at 15:37 Comment(1)
This is what I want! For my case, I want to flip the text vertically and this is what I did: Font oldFont = g2.getFont(); Font newFont = oldFont.deriveFont(AffineTransform.getScaleInstance(1, -1)); g2.setFont(newFont);Gnash
E
31

The method Graphics2D.rotate applies transform to all subsequent rendering operations. You can preserve a copy of transform (with getTransform()) before applying rotation, and then restore the original.

g2.draw(barra);
AffineTransform orig = g2.getTransform();
g2.rotate(-Math.PI/2);
g2.setColor(Color.BLACK);
g2.drawString(datos[i].titulo,(float)alto,(float)paso);
g2.setTransform(orig);
Electromagnet answered 10/4, 2012 at 6:26 Comment(3)
So it rotates the string but not the coordinate system?Varietal
It modifies current transform of rendering context. Graphics2D reference states that the transform is used to convert from User Space coordinate system to Device Space. So, it rotates coordinate system:) The last line of this example is restoring transform after string rendering.Electromagnet
Right! This method rotates the whole coordinate system, this X and Y of the drawString are also meant in the rotated coordinate system. So you are likely drawString in a translated coordinate system to a 0, 0 position instead.Chant
D
23

This code is better and if you don't want to use AffineTransform.

public static void drawRotate(Graphics2D g2d, double x, double y, int angle, String text) 
{    
    g2d.translate((float)x,(float)y);
    g2d.rotate(Math.toRadians(angle));
    g2d.drawString(text,0,0);
    g2d.rotate(-Math.toRadians(angle));
    g2d.translate(-(float)x,-(float)y);
}    

Usage:

drawRotate(g2d, 100, 100, 45, "hello world");    // 100x100px, 45 degree, 
Demolition answered 2/11, 2014 at 23:44 Comment(1)
This should be the accepted answer because it is simpler and thus applies to a broader range of situations. +1.Fairish
S
3

I have a piece of code with the following in that I have added to include Rectangle object. I can see my text is rotating, not the rectangle.

Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

String s = "dasdasdasd1";

Font font = new Font("Courier", Font.PLAIN, 12);
g2d.translate(20, 20);

FontRenderContext frc = g2d.getFontRenderContext();

GlyphVector gv = font.createGlyphVector(frc, s);
int length = gv.getNumGlyphs();
Rectangle2D barra=new Rectangle2D.Double(0, 0, 700, 500);
g2d.draw(barra);
for (int i = 0; i < length; i++) {
  Point2D p = gv.getGlyphPosition(i);
  AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
  at.rotate((double) i / (double) (length - 1) * Math.PI / 3);

  Shape glyph = gv.getGlyphOutline(i);
  Shape transformedGlyph = at.createTransformedShape(glyph);
  g2d.fill(transformedGlyph);
}

May be you can try with this.

Syncopation answered 10/4, 2012 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.