View Single Post
Join Date: Jun 2008
Posts: 398
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 20
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Trying to draw text at an angle

 
0
  #8
Aug 23rd, 2008
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.
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