java图像旋转顺时针90度的问题
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)) {
k = createImage(new MemoryImageSource(height,width, colFlipPixels(pixels, width, height), 0, width));
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ImageIcon ica=new ImageIcon(k);
jLabel1.setIcon(ica);
repaint();
}
private int[] colFlipPixels(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 = 0; y < height; y++)
for (int x = width - 1; x >= 0; x--)
newPixels[newIndex++] = pixels[y * width + x];
}
return newPixels;
}
以上是旋转90度的代码,还有180度的和270度的,和以上基本一样,就是在循环嵌套那里有明显区别,但180度和270度是可以实现的,90度的实现不了,求高手指点!提前谢过了啊! 展开
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
/**
* @author Hardneedl
*/
class ImageTransform extends JFrame {
private static final Dimension minSize = new Dimension(900, 800);
private static final Dimension maxSize = new Dimension(900, 800);
private static final Dimension preferredSize = new Dimension(900, 800);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private class Canvas extends JComponent{
private BufferedImage img;
private AffineTransform trans=new AffineTransform();
private void setImage(BufferedImage img){
if(img!=null) this.img = img;
if(isVisible()) paintImmediately(0,0,getWidth(),getHeight());
}
private void setRota(double rota){
trans.setToRotation(rota, (getWidth())>>1, ( getHeight() )>>1 );
if(isVisible()) paintImmediately(0,0,getWidth(),getHeight());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0,0,getWidth(),getHeight());
if (img==null)return;
Graphics2D gg= (Graphics2D)g.create();
int iw = img.getWidth(), ih = img.getHeight();
int anchorX = (getWidth()- iw)>>1, anchorY = (getHeight() - ih)>>1;
AffineTransform af = gg.getTransform();
af.translate(anchorX, anchorY);
af.concatenate(trans);
gg.drawImage(img,af,this);
gg.dispose();
}
}
private Canvas canvas = new Canvas();
ImageTransform() throws HeadlessException {
init();
doLay();
attachListeners();
}
private void init() {
try {
canvas.setImage(ImageIO.read(new File("1.png")));
canvas.setRota(Math.PI/3);
} catch (IOException e) {
e.printStackTrace();
}
}
private void doLay() {
Container container = getContentPane();
container.add(canvas,BorderLayout.CENTER);
pack();
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
new ImageTransform().setVisible(true);
}
}