核心代码如下:
旋转操作 每按一次旋转90度
void xuanzhuan_actionPerformed(ActionEvent e){
try {
Image ia =ic.getImage();
int width = ia.getWidth(this);
int height = ia.getHeight(this);
int pixels[] = new int [width * height];
PixelGrabber pg = new PixelGrabber(ia, 0, 0, width, height, pixels, 0, width);
if (pg.grabPixels() && ((pg.status()& ImageObserver.ALLBITS) !=0)) {
image1 = createImage(new MemoryImageSource(height,width, rowFlipPixels(rot90Pixels(pixels, width, height), width, height), 0,height));
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ImageIcon ica = new ImageIcon(image1);
label.setIcon(ica);
repaint();
}
private int[] rowFlipPixels(int pixels[], int width, int height) {
int newPixels[] = null;
if ((width * height) == pixels.length) {
newPixels = new int[width * height];
int newIndex = 0;
for (int y = height - 1; y >= 0; y--)
for (int x = width - 1; x >= 0; x--)
newPixels[newIndex++] = pixels[y * width + x];
}
return newPixels;
}
private int[] rot90Pixels(int pixels[], int width, int height) {
int newPixels[] = null;
if ((width * height) == pixels.length) {
newPixels = new int[width * height];
int newIndex = 0;
for (int x = width - 1; x >= 0; x--)
for (int y = 0; y < height; y++)
newPixels[newIndex++] = pixels[y * width + x];
}
return newPixels;
}
|