Constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| imageWidth = 48; imageHeight = 48; pixels = new int[ 48 * 48]; rotatedPixels = new int[ 48 * 48];
grabber = new PixelGrabber(passedImage, 0, 0, imageWidth,imageHeight, pixels, 0, imageHeight); try { status = grabber.grabPixels(); if ( !status) { throw new InterruptedException(); } } catch (InterruptedException e) { System.exit(0); } |
The *core*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| protected Image rotate(double angle) { int x, y; int fromX, fromY; int toX, toY; int transparent = 0x00000000; double radians = angle; double cosAngle = Math.cos(radians); double sinAngle = Math.sin(radians);
for ( y = 0; y < imageHeight; y++) { for ( x = 0; x < imageWidth; x++) { 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) ){ rotatedPixels[ (y * imageWidth) + x] = transparent; } else { rotatedPixels[ (y * imageWidth) + x] = pixels[ (fromY * imageWidth) + fromX]; } } }
rotatedImage = component.createImage( new MemoryImageSource(imageWidth, imageHeight, rotatedPixels, 0, imageWidth)); return rotatedImage; } |
I still would use Graphics2D rotate, but nothing feels like homemade code.