如何用Java中的Graphics2D旋转文本? [英] How to rotate text with Graphics2D in Java?

查看:2359
本文介绍了如何用Java中的Graphics2D旋转文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Graphics2D在JPanel上旋转文字..

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

我的代码是这样的:

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 ..

谢谢:)

推荐答案

方法 Graphics2D.rotate 将转换应用于所有后续渲染操作。您可以保留转换副本(使用 getTransform() )在应用轮换之前,然后恢复原始文件。

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

这篇关于如何用Java中的Graphics2D旋转文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆