Constructor
//imageWidth = toRotate.getWidth(observer);
//imageHeight = toRotate.getHeight(observer);
imageWidth = 48;
imageHeight = 48;
pixels = new int[ 48 * 48];
rotatedPixels = new int[ 48 * 48];
//grabber = new PixelGrabber(toRotate, 0, 0,
// imageWidth,imageHeight,
// pixels, 0, imageWidth);
grabber = new PixelGrabber(passedImage, 0, 0,
imageWidth,imageHeight,
pixels, 0, imageHeight);
try {
status = grabber.grabPixels();
if ( !status) {
throw new InterruptedException();
} // end if.
} catch (InterruptedException e) {
System.exit(0);
} // End try/catch.
The core
protected Image rotate(double angle) {
int x, y;
int fromX, fromY;
int toX, toY;
int transparent = 0x00000000;
//double radians = (((-(angle -180) ) %360) / 180.0) * Math.PI;
double radians = angle; //disable if you use ints.
//Change angle to int and then enable the above and you can use ints
double cosAngle = Math.cos(radians);
double sinAngle = Math.sin(radians);
for ( y = 0; y < imageHeight; y++) {
for ( x = 0; x < imageWidth; x++) {
// Rotate around the center of the image.
toX = ( imageWidth /2) - x;
toY = ( imageHeight /2) - y;
fromX = (int)( ( toX * cosAngle) - ( toY * sinAngle));
fromY = (int)( ( toX * sinAngle) + ( toY * cosAngle));
fromX += imageWidth /2;
fromY += imageHeight /2;
if ( (fromX < 0) || (fromX >= imageWidth) ||
(fromY < 0) || (fromY >= imageHeight) ){
// Rotated point is outside the image
rotatedPixels[ (y * imageWidth) + x] = transparent;
} else {
rotatedPixels[ (y * imageWidth) + x] =
pixels[ (fromY * imageWidth) + fromX];
} // End if.
} // End x loop.
} // End y loop.
rotatedImage = component.createImage(
new MemoryImageSource(imageWidth, imageHeight,
rotatedPixels, 0, imageWidth));
return rotatedImage;
} // end rotate;
I still would use Graphics2D rotate, but nothing feels like homemade code.