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 11 of 11

Thread: Resizable image?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Resizable image?

    Hello,

    I have an image with a standard size, what I'm trying to do is I want to be able to resize the image manually after opening it, i.e. click one of the corners and drag it to the wished size. When I try to resize it the way I mentioned I'm able to resize the frame but not the image, I don't know how to resize both the frame and the panel (or the label). Any ideas?

    Thanks.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Resizable image?

    You might have to do some custom painting. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizable image?

    Quote Originally Posted by KevinWorkman View Post
    You might have to do some custom painting. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Thanks Kevin,

    And do you know how I can just fix the frame to the image? because something I don't like is that every time I click and drag the frame the image stays the same but not the frame, the frame gets kind of shifted? you know what I mean?

    This might be uncomfortable for new users who for some reason want to resize the image, they would normally click one of the corners, drag it, and then realize that the frame is getting resized but not the image. So resizing the frame back to the image's size is a bit annoying.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Resizable image?

    well if your image is inside your resizable frame, I would suggest to try giving your frame the BorderLayout, then add your image to the center area.
    (do a search on BorderLayout for cut and paste code to do this, its barely more complex then a standard add.)
    then INSIDE YOUR FRAME RESIZING, you need to revalidate() and or repaint().
    you may need to revalidate and or repaint the frame too, never quite sure what the GUI wants with these,
    but in some combination I can always find a solution for them to update immediately.

    post code if its more complex then this solution and ill try to help,
    Jonathan

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizable image?

    Thanks Jonathan, the repaint() part didn't work for me but I'm pretty sure I did it wrong, please check it out and let me know:

    package resize2;
     
    import java.awt.BorderLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class Resize2 extends JFrame {
     
        private JLabel label;
        private JPanel panel;
     
        public Resize2() {
            super("Sensor");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocation(250, 230);
            setLayout(new BorderLayout());
     
            label = new JLabel(new ImageIcon("rotation.jpg"));
            panel = new JPanel();
     
            add(panel);
            panel.add(label, BorderLayout.CENTER);
     
            repaint();
            panel.repaint();
     
            pack();
            setVisible(true);
        }
     
        public static void main (String args []){
            new Resize2();
        }
    }

    Thanks a lot.

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Resizable image?

    EDIT:
    the order you add things makes a difference too.

    add(panel);
    panel.add(label, BorderLayout.CENTER);

    repaint();
    panel.repaint();

    can probably be shortened to just reversing the order you add them.(no need to repaint I THINK if you do
    panel.add(label, BorderLayout.CENTER);
    add(panel);

    END EDIT:


    well, like I said. I am no master of the understanding behind these methods, but there are 2 objects that seem to be reachable and have an effect on displaying things immediately.

    1. sometimes if you are not re-adding a Panel/Button/Label etc, you need to repaint, or revalidate a change to them.
    2. usually this by itself doesn't work because the frame has no reason to know the objects have changed.

    There is a method to reach the Frames visual layer, so if you have a button inside a jpanel inside another jpanel inside a frame, (etc) this method will find the Frame layer:
    getRootPane()

    This being said, I use:

    revalidate();
    repaint();
    getRootPane().revalidate();
    getRootPane().repaint();

    to see if it works with all of them,
    then I mess around with taking them out in different combinations to find out the minimal "power" needed to get it to display immediately.

    One of these days i'll read up on the differences and exactly how they work, but I suspect the full understanding to be about 5+ hours of reading/example coding which I don't have atm.

    Looking at your code, you are just creating the JPanel objects, so there inst a reason to repaint them or revalidate them from my experience. you probably only need to repaint the frame.
    getRootPane().revalidate();
    getRootPane().repaint();

    my concern is, resizing normally repaints even if you don't want it to, that being said, are you sure the Image is being added?

    make sure to try the borderlayout / center trick.
    Last edited by JonLane; March 7th, 2012 at 09:04 PM.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Resizable image?

    I'm really not sure how using a different layout or calling revalidate will help resize the image. Even if the JLabel size is changed, that doesn't affect the image size (correct?).

    I would extend JPanel (or JLabel, any JComponent will do) and override the paintComponent() method. Then take a look at the Graphics API for handy methods for drawing an image at a particular size.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Resizable image?

    My experience is if you tell the image to be added inside a boarderlayout/Center it has demands on the object for resizing, and will demand it to be a certain size, so rethinking it, you would need to use border layout for the frame and everything else that is between the frame and the image. because they would all need to get the message of the Frame demanding a resize.

    so for example. if you add a image to a jlabel and a jlabel to a jpanel and a jpanel to a jframe, and have the jframe, jpanel, jlabel all use borderlayout
    and add the image the label and the jpanel to the CENTER region of their respective containers.

    when you resize the frame it tells the jpanel to follow suit, if the jpanel doesnt have any special layout, it will just take on this burden itself, but if it too uses borderlayout/Center region, it will tell its components they have to meet the size it ends up with, and so on and so forth. it seems like a lot of overhead, but it is more intuitive then having your image communicate directly with the jframe and resize based on this communication.

    also using borderlayout might not be the most lean overhead method of using this technique, there are probably a few simple methods that will force objects inside a container to obey, but borderlayour center region does this naturally, so i offered it as a quick fix.

    hopefully this is the solution!
    Jon.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Resizable image?

    Kevin's advice is correct - an ImageIcon is painted based the Graphics.drawImage method - which means it is independent of any Layout manager. If you wish to resize the image, one way could be to use a listener to listen for changes in size (for instance a ComponentListener) and resize the image manually, or better yet do so by overriding the paintComponent method and draw the scaled image as appropriate - for instance using the getScaledInstance() method of Image or creating an AffineTransform (here, having the appropriate LayoutManager may matter as you wish to get the scaled image based upon the Component size, which IS managed by the LayoutManager).

    Jon, if you truly think this can be accomplished using a LayoutManager, I recommend posting an SSCCE which demonstrates the functionality.
    Last edited by copeg; March 7th, 2012 at 10:46 PM.

  10. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (March 8th, 2012)

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Resizable image?

    Sure, using a layout helps resize the components... but that does not magically resize the images on those components, unless the painting code specifically takes the size into account. You can do that pretty easily using custom painting, but I don't think the default JLabel or JPanel painting code is going to do anything to resize the image automatically.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Resizable image?

    It seems behavior I witnessed in my program earlier this week is due to something else, the SSCCE using only layout managers failed to resize the image
    sorry for the rabbit chase knightmetal!
    public class SSCCELayoutResizeTest {
     
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setLayout(new BorderLayout());
            frame.setTitle("ResizeTest");
            frame.setSize(500, 500);
     
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
     
            JLabel label = new JLabel();
            //label.setLayout(new BorderLayout());
     
            ImageIcon image = new ImageIcon("picture.png"); //size of picture is 80x80 Pixels
     
            label.setIcon(image);
            panel.add(label, BorderLayout.CENTER);
            frame.add(panel, BorderLayout.CENTER);
     
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
    }

Similar Threads

  1. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 11:34 PM
  2. how to create resizable tooltip?
    By diyaots in forum Java IDEs
    Replies: 1
    Last Post: November 1st, 2011, 07:38 AM
  3. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM
  5. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM