1

(Sorry for the links, I'm new and I cannot post images)

I want to accomplish the following : create a table with the legends on the top, and in a diagonal way.

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

but I'm having some problems, I have the following image, and I'm trying to rotate it 45º (the result it's at the right),

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

Here is my code:

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

I tried the following:

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

which gives as result the image at the right in the second Image 2

so I need to apply a translation to get it to the right position

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

can someone please explain how the affineTransform works, or how can I get the text to be in a diagonal way.

Thanks

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
eduBecKs
  • 11
  • 2
  • 3
  • See [my answer to a similar question](http://stackoverflow.com/questions/10426883/affinetransform-truncates-image/10440699#10440699); it shows how to rotate any image any amount of degrees without truncating it. – Torious May 17 '12 at 22:29
  • Hi @Torious My problem was mostly to rotate the Strings. At first I thought it was easier to design the image, and rotate the whole image, but the results were not good (mostly because of the antialias). I tried to apply the transformations before rotating the images, and it worked. Here is the result [image](http://i.imgur.com/zas1U.png). I still have some problems centering the text to the middle of each cell, but that's another thing. Thanks. – eduBecKs May 23 '12 at 21:30

0 Answers0