Results 1 to 6 of 6
  1. #1
    BrooksyFC is offline Member
    Join Date
    Mar 2012
    Posts
    20
    Rep Power
    0

    Default Image Dimensions in JFrame

    Here is my code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ImageViewer extends JFrame {
        private ImageIcon image1;
        private JLabel label1;
        
        ImageViewer() {
            setLayout(new FlowLayout());
            
            image1 = new ImageIcon(getClass().getResource("Snow.jpg"));
            
            label1 = new JLabel(image1);
            add(label1);
        }
        
        public static void main (String args[]) 
        {
            ImageViewer gui = new ImageViewer();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
            gui.pack();
            gui.setTitle("Image Program");
        }
    }
    Where and what do I need to put to change the size of the image as it is over sized.

    Thanks,
    Ryan

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Image Dimensions in JFrame

    An idea:
    Read the image file into an image object, Create a BufferedImage and get its Graphics context. Use the Graphics class's drawImage method to draw the image read from the file into the BufferedImage with the new size. Use the new image in the GUI
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Image Dimensions in JFrame

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Image Dimensions in JFrame

    ... or use getScaledInstance(...).

    If you control the image, it might be better to create an appropriately sized version in a decent image editor.

    If reducing image sizes for display is a recurring need, you could try extending ImageIcon and overriding the 3 Icon methods to do what you need. It's not difficult provided you do your background reading so you know exactly what you're going to do before you tackle the code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    BrooksyFC is offline Member
    Join Date
    Mar 2012
    Posts
    20
    Rep Power
    0

    Default Re: Image Dimensions in JFrame

    Thanks for the replies. How do I go about writing a BufferedImage code? I've never written one before.

    Also where on the code would I put
    getScaledInstance()

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Image Dimensions in JFrame

    Read the API doc for BufferedImage. You create one using its constructor:
    BufferedImage bi = new BufferedImage(<THE ARGS HERE>);
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 03-13-2011, 04:29 AM
  2. JFrame + image
    By Blacknight in forum New To Java
    Replies: 2
    Last Post: 05-07-2009, 05:34 AM
  3. java applet viewer dimensions
    By yuriythebest in forum Java Applets
    Replies: 1
    Last Post: 11-08-2008, 10:44 PM
  4. How can i get image dimensions using java?
    By Thilkumar82 in forum Advanced Java
    Replies: 0
    Last Post: 09-02-2008, 09:12 AM
  5. how to set full screen dimensions
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •