Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:14:35+00:00 2026-06-05T11:14:35+00:00

I was trying to modify an existing code to rotate an image based on

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:14:37+00:00Added an answer on June 5, 2026 at 11:14 am

    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) {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update (add/modify files) an existing JAR file, and this code (using
I am trying to modify an existing MySQL database for use in a new
I'm trying to modify and existing Access application to use MySQL as a database
I am trying to modify an existing PDF with iText. My code currently edits
I'm trying to use django-evolution to modify some models from an existing project. Now
I am trying to modify an existing WPF application for localization. One of my
I'm trying to modify an existing NSIS install script to allow for different licence
I am trying to modify the source code of Joomla module but I don't
I am new to Ruby On Rails, and currently trying to modify an existing
I'm trying to create an extension to modify an existing site, and fix a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.