旋转缓冲图像(对角线文本)

时间:2012-05-17 19:10:40

标签: java rotation affinetransform

(对不起链接,我是新的,我无法发布图片)

我想完成以下操作:在顶部和对角线方式创建一个带有图例的表格。

http://i.stack.imgur.com/HqtJL.png

但我遇到了一些问题,我有以下图片,我正在尝试将其旋转45º(结果在右边),

http://i.stack.imgur.com/KOlIK.png

这是我的代码:

//just some labels
ArrayList<String> labels = new ArrayList<String>();
labels.add("Juan");
labels.add("QWERTYYY");
labels.add("ANA");

// margin
int margin=3;

//diagonal = 45º
// value to shift each label 
int diagonalShift = (int)(cellSizeWidth / Math.sqrt(2d));

// height, width represent the size of the final image
// heightSub, widthSub represent the size of the image to be rotated taking into account the shift for each label
int widthSub = height + (diagonalShift * labels.size());
int heightSub = width;


// image to Display 
BufferedImage image = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = (Graphics2D) image.getGraphics();

// tempImage: subImage to rotate and place in image
BufferedImage tempImage = new BufferedImage(widthSub, heightSub, BufferedImage.TYPE_INT_RGB);
Graphics2D tempImageGraphics = (Graphics2D) tempImage.getGraphics();
tempImageGraphics.setColor(Color.BLUE);
tempImageGraphics.drawRect(0, 0, widthSub-1, heightSub-1);

// I'd like to use antialias, but it's giving bad results
// tempImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);


// drawing labels
// as we're designing a table cellSizeWidth and CellSizeHeight represent the dimensions for each cell
tempImageGraphics.setColor(Color.WHITE);
for (int i = 0; i < labels.size(); i++) {
    String label = labels.get(i);
    tempImageGraphics.drawString(label,
                                 margin + (i * diagonalShift),
                                 (int) (i * cellSizeWidth) + fontSize + centerDistance);
}

我尝试了以下内容:

//rotating
AffineTransform fontAfineTransform = new AffineTransform();
// fontAfineTransform.rotate(verticalTextDirection.rotationAngle());

结果显示第二张图像2

右侧的图像

因此我需要应用翻译才能将其置于正确的位置

// Math.sqrt(2d) because I'm working with 45º and the height becomes the hypotenuse
// fontAfineTransform.translate(-height/Math.sqrt(2d),height/Math.sqrt(2d));


//drawing into image
imageGraphics.drawImage(tempImage, fontAfineTransform, null);

有人可以解释一下affineTransform是如何工作的,或者我怎样才能让文本以对角方式出现。

由于

0 个答案:

没有答案