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 9139235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:21:40+00:00 2026-06-17T09:21:40+00:00

I am just wondering how to rotate a rectangle image with paintComponent() method of

  • 0

I am just wondering how to rotate a rectangle image with paintComponent() method of JLabel component and set its new width and height correctly?

I tried to rotate (see attached image) and the image scale becomes bigger but the JLabel scale keeps the same what makes image be out of JLabel bounds or something :S So my question is how to set image new width and height to component dynamically in a more optimal way?

enter image description here

  • 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-17T09:21:41+00:00Added an answer on June 17, 2026 at 9:21 am

    OK I tried the sample David Kroukamp showed me. Thanks, David, you pointed me to right direction. I think it is a really good snippet to lean back on.

    public static BufferedImage rotateImage(Image image, int angle)
        {
            double sin = Math.abs(Math.sin(angle));
            double cos = Math.abs(Math.cos(angle));
            int originalWidth = image.getWidth(null);
            int originalHeight = image.getHeight(null);
            int newWidth = (int) Math.floor(originalWidth * cos + originalHeight * sin);
            int newHeight = (int) Math.floor(originalHeight * cos + originalWidth * sin);
            BufferedImage rotatedBI = new BufferedImage(newWidth, newHeight, BufferedImage.TRANSLUCENT);
            Graphics2D g2d = rotatedBI.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.translate((newWidth - originalWidth) / 2, (newHeight - originalHeight) / 2);
            g2d.rotate(angle, originalWidth / 2, originalHeight / 2);
            g2d.drawImage(image, 0, 0, null);
            g2d.dispose();
            return rotatedBI;
        }
    

    David Kroukamp’s result image preview

    I tested the snippet and the result image + its component’s scale were really dynamic changed.
    But the result component width I’ve got has been always a little bit wider than it’s inner image was :S

    For example here is my version of an image rotated in 45 degrees angle

    snippet based result image

    … As I could find (on blue background) its width is not totally fit to surround white image … Actually I was looking for some kind of standard Java Image Processing rotate solution but I couldn’t find it 🙁 so I had to dig deeper to the problem and at least figure out how to solve the ‘wider effect’ and avoid create new BufferedImage object every re-paint…

    OK, as a reault of my research, I tried to write some kind of my rotate code adaptation. Here it is :

    >tested

    public static void rotateImage(Graphics g, Image image,int tilt,JComponent component)
        {
    
            // create the transform, note that the transformations happen
    
                      // in reversed order (so check them backwards)
                      AffineTransform at = new AffineTransform();
    
                      //5. modify component scale ...
    
                      double sin = Math.abs(Math.sin(Math.toRadians(tilt)));
                      double cos = Math.abs(Math.cos(Math.toRadians(tilt)));
    
                      int w=image.getWidth(null);
                      int h=image.getHeight(null);
                      int newW=(int) Math.floor(w * cos + h * sin);
                      int newH=(int) Math.floor(h * cos + w * sin);
    
                      component.setSize(newW, newH);
    
                      int width=component.getWidth();
                      int height=component.getHeight();
    
                      // 4. translate it to the center of the component
                      at.translate(width / 2, height / 2);
    
                      // 3. do the actual rotation
                      at.rotate(Math.toRadians(tilt));
    
                      // 2. just a scale because this image is big
        //              at.scale(1, 1);
    
    
                      // 1. translate the object so that you rotate it around the
                      //    center (easier :))
                      at.translate(-image.getWidth(null)/2, -image.getHeight(null)/2);
    
    
    
                      // draw the image
                      ((Graphics2D) g).drawImage(image, at, null);
    
    
            }
    

    … so the result image rotated on -30 degrees tilt looks like this

    enter image description here

    I dearly hope the tip saves ones day 🙂


    Thank you all for help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering: I'm trying to set up an adaptive image handler in Coldfusion8, which
I am wondering if there is any way to dynamically rotate an image or
Just wondering if anyone knows of a way to change the panorama background image
Just wondering is there the same method for an Array object similar to indexOf
Just wondering if there's a preferred method or Best Practice for storing Updated By
Just wondering if this is the most efficient method of doing this? is there
Just wondering, if there is any way or codes, that can set countdown timer
just wondering if there is a way to reduce the amount of code needed
Just wondering the best way to replace in place matches on a string. value.replace(bob,
Just wondering, having the following simple code: var object1 = { name: function (){

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.