md1hunox
md1hunox

Reputation: 3955

Rotating an image based on key presses

I was trying to modify an existing code to rotate an image based on key presses. so far i've managed to do the following and I'm stuck. i've made use of Affine transform for the first time. The image rotates only once when it's supposed to rotate as many times as the RIGHT key is pressed.

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) {
    }
}

would be helpful if anyone could correct me on where im making the mistake. Thanks

Upvotes: 2

Views: 3366

Answers (2)

shareef
shareef

Reputation: 9581

the problem you are creating new rotate image on each event key so it looks like not working try to change the place of this line to be none modifiable on each key event

public static void main(String[] args) {

        new RotateImage(null);
    }

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

UPDATE:

the reason is because the value of constructor is null you should pass image

new RotateImage(null);

modify this on your code

1)make it static

private static Image TestImage;

2)define

private static RotateImage ri;

3)call in main like this

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

step 4(removed)

UPDATE:

read these question on stack overflow another question

UPDATE2:

here is the full code it works perfectly ( the right key ) dont foget to include you image in the same package and its the same type .png here is the code

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) {
    }
}

Upvotes: 3

Julien Breuil
Julien Breuil

Reputation: 165

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

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

           ri.rotate();
        }

It seems that you are rotating the same image every time so it will always rotate for only 5 deg.

edit : hum too late . . see shareef post.

Upvotes: 1

Related Questions