Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Change the size of an image

  1. #1
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Change the size of an image

    Hello everyone..
    Is it possible to change the size (i.e.)height and width of an image through java.If it is,please do me a favor.

    Thanks.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Change the size of an image

    Short answer, yes.

    Longer answer, yes, I have the following code in my ImageUtility class.

        public static BufferedImage resizeImage(final Image image, int width, int height) {
            final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            final Graphics2D graphics2D = bufferedImage.createGraphics();
            graphics2D.setComposite(AlphaComposite.Src);
            graphics2D.drawImage(image, 0, 0, width, height, null);
            graphics2D.dispose();
     
            return bufferedImage;
        }

    // Json

  3. #3
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Change the size of an image

    I use this method.
    public static BufferedImage resizeImage(final Image image, int width, int height) {
    final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = bufferedImage.createGraphics();
    graphics2D.setComposite(AlphaComposite.Src);
    graphics2D.drawImage(image, 0, 0, width, height, null);
    graphics2D.dispose();
    return bufferedImage;
    }

    Also i called this method when loading the image like,

    Image imag = ((ImageIcon)ImageStore.applet_loadedImages.get("bg-2.png")).getImage();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int width =(int) d.getWidth();
    int height =(int) d.getHeight();
    resizeImage(imag,width,height);

    But its not resizing the image.In my code image is in label.Is anything wrong in my code?

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Change the size of an image

    The resizeImage method will return you a new image which you will have to use, it doesn't change your current image.

    Image imag = ((ImageIcon)ImageStore.applet_loadedImages.get("bg-2.png")).getImage();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int width =(int) d.getWidth();
    int height =(int) d.getHeight();
    final BufferedImage newImage = resizeImage(imag,width,height);

    Now use the newImage variable.

    // Json

  5. #5
    Member
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SOLVED]Re: Change the size of an image

    Thank u all.Its working.

Similar Threads

  1. How to Change JTextArea font, font size and color
    By Flash in forum Java Swing Tutorials
    Replies: 7
    Last Post: January 14th, 2012, 10:47 PM
  2. Can we increase the stack size for JVM ?
    By prasanna in forum Java Theory & Questions
    Replies: 4
    Last Post: August 4th, 2009, 04:17 PM
  3. Change JFrame components problem
    By bruno88 in forum AWT / Java Swing
    Replies: 0
    Last Post: June 30th, 2009, 02:25 PM
  4. How to set the Listbox size.
    By jacinto in forum AWT / Java Swing
    Replies: 4
    Last Post: June 22nd, 2009, 08:39 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 06:48 AM