How to rotate text with Graphics2D in Java?

35,984

Solution 1

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);

Solution 2

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();

Solution 3

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, 

Solution 4

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.

Share:
35,984

Related videos on Youtube

Rafael Carrillo
Author by

Rafael Carrillo

My name is Rafael I love software development, I find the problem analysis and solving pretty exciting, and watch how something I developed is helping to someone is the best part of my career. I like to work with Java and Python, I have experience developing microservices, desktop applications, web platforms, etc. My goal is to learn big data and push my development tools to the next level.

Updated on November 03, 2020

Comments

  • Rafael Carrillo
    Rafael Carrillo over 3 years

    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 :)

  • qed
    qed over 10 years
    So it rotates the string but not the coordinate system?
  • Mersenne
    Mersenne over 10 years
    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.
  • CodeMed
    CodeMed about 9 years
    This should be the accepted answer because it is simpler and thus applies to a broader range of situations. +1.
  • Programus
    Programus about 5 years
    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);
  • Balazs Kelemen
    Balazs Kelemen over 3 years
    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.