• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Resizing a picture

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to resize a picture and scrollpane when a user imports an image into the program. However, when I call the setSize methods, the size of the image remains unchanged. I've tried using maximum size and minimum size, as well as calling revalidate methods, but the size of the picture remains the default size when the program begins. What am I doing wrong here? (Also, this isn't the finished code. I am simply concerned with getting these sizes to change at runtime.)

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Images are painted at there actual size unless you do custom painting. You have two options.

a) if you just want to scale the image to a different fixed size then you can use Image.getScaledImage(...);

b) if you want the image to dynamically resize as you resize the frame then you need to do custom painting of the image. The Background Panel gives you an option to do this.
 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thank you for your time. I will work with image resizing and find out if it suits my design.
 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I almost have it, but I am confused with working with the Graphics class. I have a resized image which I used through some examples online, as well as a canvas object which can paint that image. Trying to understand this more, but I keep getting a blank page. What else could I be doing wrong here? That is, how do I make it fire the paint() method? I have a line of code to tell me when it fires, but nothing seems to be updating...

 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) There is not need to create a custom class to display the image. Just add the resized image to a JLabel

2) Even if you did need a custom class you would NOT extend Canvas. Canvas is an AWT component and your original code was using an ImageIcon which indicates you are using Swing. Did you even look at my example?
 
Chris Swanson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I forgot I could create a new Icon using the image that was resized. I was thinking the icon had to be read from a file for some reason. Thank you very much for your help!!
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic