如何在Java中使用Graphics2D旋转文本?

时间:2022-04-15 11:05:13

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

我想使用Graphics2D在JPanel上旋转文本。

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

谢谢:)

4 个解决方案

#1


22  

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.

Graphics2D方法。将转换应用于所有后续的呈现操作。您可以在应用旋转之前保存转换的副本(使用getTransform()),然后恢复原始的。

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

#2


16  

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

#3


9  

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

这段代码更好,如果您不想使用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, 

#4


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.

也许你可以试试这个。

#1


22  

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.

Graphics2D方法。将转换应用于所有后续的呈现操作。您可以在应用旋转之前保存转换的副本(使用getTransform()),然后恢复原始的。

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

#2


16  

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

#3


9  

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

这段代码更好,如果您不想使用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, 

#4


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.

也许你可以试试这个。