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

  • SEARCH
  • Home
  • 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 3877120
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:27:39+00:00 2026-05-19T22:27:39+00:00

I am having trouble getting a rotated BufferedImage to display. I think the rotation

  • 0

I am having trouble getting a rotated BufferedImage to display. I think the rotation is working just fine, but I can’t actually draw it to the screen. My code:

Class extends JPanel {
    BufferedImage img;
    int rotation = 0;

    public void paintComponent(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        img2d = img.createGraphics();
        img2d.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2);
        g.drawImage(img, imgx, imgy, null);
        this.repaint();
    }
}

This is not working for me. I could not find any way to draw the rotated img2d onto g.

EDIT: I have multiple objects that are being drawn onto g, so I can’t rotate that. I need to be able to rotate things individually.

  • 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-05-19T22:27:40+00:00Added an answer on May 19, 2026 at 10:27 pm

    I would use Graphics2D.drawImage(image, affinetranform, imageobserver).

    The code example below rotates and translates an image to the center of the component. This is a screenshot of the result:

    screenshot

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Test");
    
        frame.add(new JComponent() {
            BufferedImage image = ImageIO.read(
                    new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
    
                // create the transform, note that the transformations happen
                // in reversed order (so check them backwards)
                AffineTransform at = new AffineTransform();
    
                // 4. translate it to the center of the component
                at.translate(getWidth() / 2, getHeight() / 2);
    
                // 3. do the actual rotation
                at.rotate(Math.PI / 4);
    
                // 2. just a scale because this image is big
                at.scale(0.5, 0.5);
    
                // 1. translate the object so that you rotate it around the 
                //    center (easier :))
                at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
    
                // draw the image
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(image, at, null);
    
                // continue drawing other stuff (non-transformed)
                //...
            }
        });
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a jQuery plugin, but am having some trouble getting my variables
Im having trouble getting a function within a function working, do you think what
Hi guys I'm having trouble getting my code to correctly display html within the
I'm having trouble getting the following to work in SQL Server 2k, but it
I having trouble getting append() to work in safari for some reason, but it
Having trouble getting the javascript alert to display from my code behind. c# -
I'm having trouble getting this working in most browsers, except for IE (it even
Im having trouble getting the following to display in ie6, everything works find in
I am having trouble getting my chat application working correctly. I have 2 textbox's,
I'm having trouble getting UISplitViewController working in a Universal app where I've already coded

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.