View Single Post
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Trying to draw text at an angle

 
0
  #2
Aug 22nd, 2008
a search i did i found this:
  1. public static BufferedImage rotate(Image image, double angle, int cx, int cy){
  2. int width = image.getWidth(null);
  3. int height = image.getHeight(null);
  4.  
  5. //The bounds of the image
  6. int minX, minY, maxX, maxY;
  7. minX = minY = maxX = maxY = 0;
  8.  
  9. //create an array containing the corners of the image
  10. //In the order TL,TR,BR,BL
  11. int[] corners = { 0, 0, width, 0, width, height, 0, height };
  12.  
  13. double theta = Math.toRadians(angle);
  14. for(int i=0;i<corners.length;i+=2){
  15. //Rotates the given point theta radians around (cx,cy)
  16. int x = (int)(Math.cos(theta)*(corners[i]-cx) -
  17. Math.sin(theta)*(corners[i+1]-cy)+cx);
  18. int y = (int)(Math.sin(theta)*(corners[i]-cx) +
  19. Math.cos(theta)*(corners[i+1]-cy)+cy);
  20.  
  21. //Update our bounds
  22. if(x>maxX) maxX = x;
  23. if(x<minX) minX = x;
  24. if(y>maxY) maxY = y;
  25. if(y<minY) minY = y;
  26. }
  27.  
  28. //Where the center of the old image should be on the image we are
  29. //just about to create so that the image is all in viewable space.
  30. cx = (int)(cx-minX);
  31. cy = (int)(cy-minY);
  32.  
  33. //Create a new image such that when we roate the old image, no pixels
  34. //will have a negative x or y coordinate.
  35. BufferedImage bi = createBufferedImage(maxX-minX, maxY-minY, true);
  36. Graphics2D g2 = bi.createGraphics();
  37.  
  38.  
  39. //Finally start the rotation process
  40. AffineTransform at = new AffineTransform();
  41. at.rotate(theta,cx,cy);
  42. g2.setTransform(at);
  43.  
  44. g2.drawImage(image,-minX,-minY,null);
  45.  
  46. g2.dispose();
  47.  
  48. return toBufferedImage(bi);
  49. }

it rotates an entire image, so you could make an image write a line on it and pass it to this method, it's likely to be slow, i don't know much about afflinetransform
Last edited by sciwizeh; Aug 22nd, 2008 at 8:48 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote