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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:11:22+00:00 2026-06-03T00:11:22+00:00

A part of my application is working as a tutorial. For that purpose I

  • 0

A part of my application is working as a tutorial. For that purpose I got JPanels that display a JLabel that has an image as content. Although when the image is larger than what fits the screen it will just cut what doesn’t fit. What I need to do is resize the image so that I will fit the space the JLabel is given. Also tried using JScrollPanes but didn’t make any difference(although I prefer to resize image).

I tried getting a scaled instance of the image using

Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);

but it didn’t make any difference.

This is the current code :

private JLabel getTutorialLabel(int type) {
        String path = "/Images/Tutorial/test" + type + ".png";
        try {
            BufferedImage tutorialPic = ImageIO.read(getClass().getResource(path));
//            int height = (int) (screenDim.height * 0.9);
//            int width = (int) (screenDim.width * 0.9);
//            Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            JLabel picLabel = new JLabel(new ImageIcon(tutorialPic));
            return picLabel;
        } catch (IOException ex) {
            Logger.getLogger(Tutorial.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

How I get the image to display properly?

Please note that there are other components displayed along with the JLabel(JMenuBar and some buttons on bottom of screen) so I need to image to fill the space JLabel is given.

  • 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-03T00:11:24+00:00Added an answer on June 3, 2026 at 12:11 am

    Edit: updated the method

    You could try this method in the Java Helper Library.

    Or, here’s the method itself:

    /**
    * This method resizes the given image using Image.SCALE_SMOOTH.
    *
    * @param image the image to be resized
    * @param width the desired width of the new image. Negative values force the only constraint to be height.
    * @param height the desired height of the new image. Negative values force the only constraint to be width.
    * @param max if true, sets the width and height as maximum heights and widths, if false, they are minimums.
    * @return the resized image.
    */
    public static Image resizeImage(Image image, int width, int height, boolean max) {
      if (width < 0 && height > 0) {
        return resizeImageBy(image, height, false);
      } else if (width > 0 && height < 0) {
        return resizeImageBy(image, width, true);
      } else if (width < 0 && height < 0) {
        PrinterHelper.printErr("Setting the image size to (width, height) of: ("
                + width + ", " + height + ") effectively means \"do nothing\"... Returning original image");
        return image;
        //alternatively you can use System.err.println("");
        //or you could just ignore this case
      }
      int currentHeight = image.getHeight(null);
      int currentWidth = image.getWidth(null);
      int expectedWidth = (height * currentWidth) / currentHeight;
      //Size will be set to the height
      //unless the expectedWidth is greater than the width and the constraint is maximum
      //or the expectedWidth is less than the width and the constraint is minimum
      int size = height;
      if (max && expectedWidth > width) {
        size = width;
      } else if (!max && expectedWidth < width) {
        size = width;
      }
      return resizeImageBy(image, size, (size == width));
    }
    
    /**
    * Resizes the given image using Image.SCALE_SMOOTH.
    *
    * @param image the image to be resized
    * @param size the size to resize the width/height by (see setWidth)
    * @param setWidth whether the size applies to the height or to the width
    * @return the resized image
    */
    public static Image resizeImageBy(Image image, int size, boolean setWidth) {
      if (setWidth) {
        return image.getScaledInstance(size, -1, Image.SCALE_SMOOTH);
      } else {
        return image.getScaledInstance(-1, size, Image.SCALE_SMOOTH);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A part of the application I'm working on is an swf that shows a
A part of the application that I am working on is a legacy Vb6
Just working through a tutorial on asp.net/mvc for the music store application. Ref: http://www.asp.net/mvc/tutorials/mvc-music-store-part-3
A part of an application I'm working on is a simple pthread-based server that
I am working on an application that uploads a file to amazon s3(part of
I'm working on a document application and part of this application I've to add
I was working on a public comments part of an application on Friday when
I have a WCF service as part of a client/server application I am working
I'm working in ASP.NET c# application. I came to a part where I need
I'm working on a server-to-server authentication via RMI. There's on application-server (server part) and

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.