//CustomPane.java
import javax.swing.* ;
import java.awt.* ;
import java.awt.image.* ;
public class CustomPane extends JPanel {
String str ;
BufferedImage original = new BufferedImage ( 200 , 50 ,
BufferedImage.TYPE_INT_ARGB ) ;
BufferedImage after ;
double A = 0 ;
double dA = 2 * Math.PI / 180 ;
public CustomPane ( ) {
this ( "Hello World" ) ;
}
public CustomPane ( String s ) {
str = s ;
Graphics g = original.getGraphics ( ) ;
g.setColor ( new Color ( 0 , 0 , 0 , 1 ) ) ;
g.fillRect ( 0 , 0 , original.getWidth ( ) , original.getHeight ( ) ) ;
g.setColor ( new Color ( 0 , 0 , 0 , 255 ) ) ;
g.drawString ( str , 10 , 10 ) ;
}
@ Override public void paint ( Graphics g ) {
A += dA ;
if ( A >= Math.PI/2 ) {
A -= Math.PI/2 ;
}
g.setColor(new Color(255,255,255));
after = rotateImage ( original , A );
g.fillRect ( 0 , 0 , original.getWidth ( ) , after.getHeight ( ) ) ;
g.drawImage (after , 10 , 10 , null ) ;
}
public static BufferedImage rotateImage ( BufferedImage bi ,
double angleRads ) {
double cosA = Math.cos ( angleRads ) ;
double sinA = Math.sin ( angleRads ) ;
BufferedImage nbi = new BufferedImage ( ( int ) Math.abs ( ( bi
.getWidth ( )
* cosA + bi.getHeight ( ) * sinA ) ) , ( int ) Math.abs ( ( bi
.getHeight ( )
* cosA + bi.getWidth ( ) * sinA ) ) ,
BufferedImage.TYPE_INT_ARGB ) ;
Graphics g = nbi.getGraphics ( ) ;
g.setColor ( new Color ( 0 , 0 , 0 , 0 ) ) ;
g.fillRect ( 0 , 0 , nbi.getWidth ( ) , nbi.getHeight ( ) ) ;
for ( int x = 0 ; x < nbi.getWidth ( ) ; x ++ ) {
for ( int y = 0 ; y < nbi.getHeight ( ) ; y ++ ) {
try {
nbi.setRGB ( ( int ) x , ( int ) y , bi.getRGB (
( int ) ( x * cosA + y * sinA ) , ( int ) ( y
* cosA - x * sinA ) ) ) ;
} catch ( Exception e ) {}
}
}
return nbi ;
}
}