根据按键旋转图像

时间:2012-06-12 08:19:21

标签: java swing rotation keylistener key-bindings

我试图修改现有代码以根据按键旋转图像。到目前为止,我已成功做到以下几点,我被困住了。我第一次使用了仿射变换。当按下RIGHT键时,图像旋转的次数只旋转一次。

package aircraftPackage;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class RotateImage extends JFrame implements KeyListener {
    private static final long serialVersionUID = 1L;
    private Image TestImage;
    private BufferedImage bf;
    private int cordX = 100;
    private int cordY = 100;
    private double currentAngle;

    public RotateImage(Image TestImage) {
     this.TestImage = TestImage;
     MediaTracker mt = new MediaTracker(this);
     mt.addImage(TestImage, 0);
     try {
       mt.waitForID(0);
     }
     catch (Exception e) {
       e.printStackTrace();
     }
        setTitle("Testing....");
        setSize(500, 500);
        imageLoader();
        setVisible(true);
    }
public void rotate() {
     //rotate 5 degrees at a time
     currentAngle+=5.0;
     if (currentAngle >= 360.0) {
       currentAngle = 0;
     }
     repaint();
   }


    public void imageLoader() {
        try {
            String testPath = "test.png";
            TestImage = ImageIO.read(getClass().getResourceAsStream(testPath));

        } catch (IOException ex) {
            ex.printStackTrace();
        }

        addKeyListener(this);
    }

    public void update(Graphics g){
           paint(g);
    }

    public void paint(Graphics g){

        bf = new BufferedImage( this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_RGB);

    try{
    animation(bf.getGraphics());
    g.drawImage(bf,0,0,null);
    }catch(Exception ex){

    }
}

    public void animation(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform origXform = g2d.getTransform();
        AffineTransform newXform = (AffineTransform)(origXform.clone());
        //center of rotation is center of the panel
        int xRot = this.getWidth()/2;
        int yRot = this.getHeight()/2;
        newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
        g2d.setTransform(newXform);
        //draw image centered in panel
        int x = (getWidth() - TestImage.getWidth(this))/2;
        int y = (getHeight() - TestImage.getHeight(this))/2;
        g2d.drawImage(TestImage, x, y, this);
        g2d.setTransform(origXform);
        g.drawImage(TestImage, cordX, cordY, this);
    }

    public static void main(String[] args) {

        new RotateImage(null);
    }

    public void keyPressed(KeyEvent ke) {
            final RotateImage ri = new RotateImage(TestImage);

        switch (ke.getKeyCode()) {
        case KeyEvent.VK_RIGHT: {
            cordX += 5;

           ri.rotate();
        }
            break;
        case KeyEvent.VK_LEFT: {
            cordX -= 5;
        }
            break;
        case KeyEvent.VK_DOWN: {
            cordY += 5;
        }
            break;
        case KeyEvent.VK_UP: {
            cordY -= 3;
        }
            break;
        }
        repaint();
    }

    public void keyTyped(KeyEvent ke) {
    }

    public void keyReleased(KeyEvent ke) {
    }
}
如果有人能在我犯错的地方纠正我,

会有所帮助。 感谢

2 个答案:

答案 0 :(得分:3)

您要在每个事件键上创建新的旋转图像的问题,因此它看起来不起作用 尝试将此行的位置更改为在每个键事件上无法修改

public static void main(String[] args) {

        new RotateImage(null);
    }

    public void keyPressed(KeyEvent ke) {
            final RotateImage ri = new RotateImage(TestImage);

<强>更新

原因是因为构造函数的值为null,您应该传递图像

new RotateImage(null);

在您的代码上修改此内容

1)使其静止

private static Image TestImage;

2)定义

private static RotateImage ri;

3)像这样打电话给主

public static void main(String[] args) {
         ri = new RotateImage(TestImage);
    }

第4步(已删除)

<强>更新

read these question on stack overflow another question

<强> UPDATE2:

这里是它完美运行的完整代码(右键)不要忘记将图像包含在同一个包中并且它的类型相同。这里是代码

package aircraftPackage;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class RotateImage extends JFrame implements KeyListener {
    private static final long serialVersionUID = 1L;
    private static Image TestImage;
    private static RotateImage ri;
    private BufferedImage bf;
    private int cordX = 100;
    private int cordY = 100;
    private double currentAngle;

    public RotateImage(Image TestImage) {
     this.TestImage = TestImage;
     MediaTracker mt = new MediaTracker(this);
     mt.addImage(TestImage, 0);
     try {
       mt.waitForID(0);
     }
     catch (Exception e) {
       e.printStackTrace();
     }
        setTitle("Testing....");
        setSize(500, 500);
        imageLoader();
        setVisible(true);
    }
public void rotate() {
     //rotate 5 degrees at a time
     currentAngle+=5.0;
     if (currentAngle >= 360.0) {
       currentAngle = 0;
     }
     repaint();
   }


    public void imageLoader() {
        try {
            String testPath = "test.png";
            TestImage = ImageIO.read(getClass().getResourceAsStream(testPath));

        } catch (IOException ex) {
            ex.printStackTrace();
        }

        addKeyListener(this);
    }

    public void update(Graphics g){
           paint(g);
    }

    public void paint(Graphics g){

        bf = new BufferedImage( this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_RGB);

    try{
    animation(bf.getGraphics());
    g.drawImage(bf,0,0,null);
    }catch(Exception ex){

    }
}

    public void animation(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform origXform = g2d.getTransform();
        AffineTransform newXform = (AffineTransform)(origXform.clone());
        //center of rotation is center of the panel
        int xRot = this.getWidth()/2;
        int yRot = this.getHeight()/2;
        newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
        g2d.setTransform(newXform);
        //draw image centered in panel
        int x = (getWidth() - TestImage.getWidth(this))/2;
        int y = (getHeight() - TestImage.getHeight(this))/2;
        g2d.drawImage(TestImage, x, y, this);
        g2d.setTransform(origXform);
        g.drawImage(TestImage, cordX, cordY, this);
    }

    public static void main(String[] args) {
         ri = new RotateImage(TestImage);

    }

    public void keyPressed(KeyEvent ke) {

        switch (ke.getKeyCode()) {
        case KeyEvent.VK_RIGHT: {
            cordX += 5;

           ri.rotate();
        }
            break;
        case KeyEvent.VK_LEFT: {
            cordX -= 5;
            ri.rotate();
        }
            break;
        case KeyEvent.VK_DOWN: {
            cordY += 5;
            ri.rotate();
        }
            break;
        case KeyEvent.VK_UP: {
            cordY -= 3;
            ri.rotate();
        }
            break;
        }
        repaint();
    }

    public void keyTyped(KeyEvent ke) {
    }

    public void keyReleased(KeyEvent ke) {
    }
}

答案 1 :(得分:1)

 public void keyPressed(KeyEvent ke) {
            final RotateImage ri = new RotateImage(TestImage);

        switch (ke.getKeyCode()) {
        case KeyEvent.VK_RIGHT: {
            cordX += 5;

           ri.rotate();
        }

您似乎每次都旋转相同的图像,因此它始终只旋转5度。

编辑:哼哼太晚了。 。见shareef post。