View Single Post
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Trying to draw text at an angle

 
0
  #9
Aug 23rd, 2008
Originally Posted by sciwizeh View Post
ok, I wrote these two classes CustomPane is the one that has the rotateImage() method.

  1. //CustomPane.java
  2. import javax.swing.* ;
  3. import java.awt.* ;
  4. import java.awt.image.* ;
  5.  
  6. public class CustomPane extends JPanel {
  7. String str ;
  8. BufferedImage original = new BufferedImage ( 200 , 50 ,
  9. BufferedImage.TYPE_INT_ARGB ) ;
  10. BufferedImage after ;
  11. double A = 0 ;
  12. double dA = 2 * Math.PI / 180 ;
  13.  
  14. public CustomPane ( ) {
  15. this ( "Hello World" ) ;
  16. }
  17.  
  18. public CustomPane ( String s ) {
  19. str = s ;
  20. Graphics g = original.getGraphics ( ) ;
  21. g.setColor ( new Color ( 0 , 0 , 0 , 1 ) ) ;
  22. g.fillRect ( 0 , 0 , original.getWidth ( ) , original.getHeight ( ) ) ;
  23. g.setColor ( new Color ( 0 , 0 , 0 , 255 ) ) ;
  24. g.drawString ( str , 10 , 10 ) ;
  25. }
  26.  
  27. @ Override public void paint ( Graphics g ) {
  28. A += dA ;
  29. if ( A >= Math.PI/2 ) {
  30. A -= Math.PI/2 ;
  31. }
  32. g.setColor(new Color(255,255,255));
  33. after = rotateImage ( original , A );
  34. g.fillRect ( 0 , 0 , original.getWidth ( ) , after.getHeight ( ) ) ;
  35. g.drawImage (after , 10 , 10 , null ) ;
  36. }
  37.  
  38. public static BufferedImage rotateImage ( BufferedImage bi ,
  39. double angleRads ) {
  40. double cosA = Math.cos ( angleRads ) ;
  41. double sinA = Math.sin ( angleRads ) ;
  42. BufferedImage nbi = new BufferedImage ( ( int ) Math.abs ( ( bi
  43. .getWidth ( )
  44. * cosA + bi.getHeight ( ) * sinA ) ) , ( int ) Math.abs ( ( bi
  45. .getHeight ( )
  46. * cosA + bi.getWidth ( ) * sinA ) ) ,
  47. BufferedImage.TYPE_INT_ARGB ) ;
  48. Graphics g = nbi.getGraphics ( ) ;
  49. g.setColor ( new Color ( 0 , 0 , 0 , 0 ) ) ;
  50. g.fillRect ( 0 , 0 , nbi.getWidth ( ) , nbi.getHeight ( ) ) ;
  51. for ( int x = 0 ; x < nbi.getWidth ( ) ; x ++ ) {
  52. for ( int y = 0 ; y < nbi.getHeight ( ) ; y ++ ) {
  53. try {
  54. nbi.setRGB ( ( int ) x , ( int ) y , bi.getRGB (
  55. ( int ) ( x * cosA + y * sinA ) , ( int ) ( y
  56. * cosA - x * sinA ) ) ) ;
  57. } catch ( Exception e ) {}
  58. }
  59. }
  60. return nbi ;
  61. }
  62. }
  1. //RotateText.java
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.geom.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import java.util.concurrent.*;
  8. public class RotateText extends JFrame implements Runnable
  9. {
  10. CustomPane c = new CustomPane("This text will be rotated");
  11. public RotateText()
  12. {
  13. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. setSize(200,200);
  15. this.add(c);
  16. c.repaint();
  17. setVisible(true);
  18. ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(3);
  19. exec.scheduleAtFixedRate(this,0L,33L,TimeUnit.MILLISECONDS);
  20. }
  21. public static void main(String[] args){
  22. new RotateText();
  23. }
  24. public void run(){
  25. c.repaint();
  26. }
  27. }

this example is of an animated rotating sentence, as i said this method is very slow, and inaccurate i hope that it is clear what i did. mostly basic trig.

Hey thanks! I think I get he idea a little better now. I've changed the math to make the calculation a bit faster, but the idea is the same. You have a pixel that is at location (x1,y1) at angle 1 and you have to find the corresponding (x2, y2) point at angle 2 that corresponds to it. It's not a completely brute force method, but I'm still assigning a color to the buffered image on a pixel by pixel basis. I wonder whether there's a way where you don't have to go pixel by pixel like that? I've been getting decent results except sometimes for no obvious reason, I'm getting little dots in my letters instead of nice contiguous color patterns. The characters tend to look the best when they are close to horizontal or close to vertical. I'm thinking I've got some slight round-off error. But I'm a lot more on the right track now, so thank you again!
Reply With Quote