Results 1 to 6 of 6
  1. #1
    Yoruichi is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Setting frame size to the size of an image

    Hello,

    I'm trying to add an image to a JPanel, and I want the frame size to be the exact same as the image size but I don't know which functions to use. Here's the code I'm using. Any ideas how to do this?

    Java Code:
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Test {
    
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    			{
    				public void run()
    				{
    					MyFrame frame = new MyFrame();
    					frame.pack();
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    					frame.setVisible(true);
    					frame.setResizable(false);
    				}
    			});
    	}
    }
    
    class MyFrame extends JFrame
    {
    	private JPanel imagePanel;
    	BufferedImage image;
    	Dimension dim;
    	
    	public MyFrame()
    	{
    		setTitle("Lab 7 tester");
    		
    		try{
    			image = ImageIO.read(new File("simple.png"));
    			
    		} catch (Exception e){
    			e.printStackTrace();
    		}
    		
    		dim = new Dimension();
    		dim.height = image.getHeight();
    		dim.width = image.getWidth();
    		
    		imagePanel = new JPanel(){
    			
    			public void paint(Graphics g)
    			{
    				super.paint(g);
    				g.drawImage(image, 0, 0, null);
    			}
    		};
    		
    		imagePanel.setPreferredSize(dim);
    		add(imagePanel);
    		
    	}
    	
    }
    Thanks.
    Last edited by Yoruichi; 03-26-2009 at 07:53 PM. Reason: mistake

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Explore {
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MyFrame frame = new MyFrame();
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class MyFrame extends JFrame
    {
        private JPanel imagePanel;
        BufferedImage image;
        Dimension dim = new Dimension();  // instantiate dim
    
        public MyFrame()
        {
            setTitle("Lab 7 tester");
            //setSize(300,300);
            String path = "http://www.java-forums.org/images/ca_evo/misc/logo.gif";
            try{
                URL url = new URL(path);
                System.out.println("url = " + url);
                image = ImageIO.read(url);
    
            } catch (IOException e){
                System.out.println(e.getMessage());
            }
            dim.height = image.getHeight();  // dim was null
            dim.width = image.getWidth();
    
            imagePanel = new JPanel(){
    
                public void paintComponent(Graphics g)
                {
                    super.paintComponent(g);
                    g.drawImage(image, 0, 0, null);
                }
            };
            imagePanel.setPreferredSize(dim);
            add(imagePanel);
        }
    }

  3. #3
    Yoruichi is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Sorry, I forgot to initialize dim.
    The frame is the same size as the image now, however when i set resizable to false, it becomes a bit bigger. Why is that?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Why is that?
    Frames are created with a peer which is platform-dependent.
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class Explore
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MyFrame left  = new MyFrame(true, new Point(25,25));
                    explore(left);
                    MyFrame right = new MyFrame(false, new Point(400,25));
                    explore(right);
                    Dimension d = getPanelSize(left);
                    System.out.printf("panel size = [%d, %d]%n",
                                       d.width, d.height);
                }
            });
        }
    
        private static void explore(JFrame frame) {
            Dimension size = frame.getSize();
            System.out.printf("%6s resizable size = [%d, %d]%n",
                               frame.isResizable() ? "is" : "is not",
                               size.width, size.height);
            Insets insets = frame.getInsets();
            System.out.printf("insets = [%d, %d, %d, %d]%n",
                               insets.top, insets.left,
                               insets.bottom, insets.right);
            System.out.println("----------------------");
        }
    
        private static Dimension getPanelSize(Container parent)
        {
            Component[] c = parent.getComponents();
            for(int i = 0; i < c.length; i++)
            {
                //System.out.println(c[i].getClass().getSimpleName());
                if(c[i] instanceof JPanel)
                {
                    return c[i].getSize();
                }
                if(((Container)c[i]).getComponentCount() > 0)
                {
                    return getPanelSize((Container)c[i]);
                }
            }
            return null;
        }
    }
    
    class MyFrame extends JFrame
    {
        BufferedImage image;
    
        public MyFrame(boolean resizable, Point loc)
        {
            super("resizable = " + resizable);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setResizable(resizable);
            try{
                File file = new File("images/bison.jpg");
                image = javax.imageio.ImageIO.read(file);
    
            }
            catch (IOException e)
            {
                System.out.println(e.getMessage());
            }
            JPanel imagePanel = new JPanel()
            {
                public void paintComponent(Graphics g)
                {
                    super.paintComponent(g);
                    g.drawImage(image, 0, 0, null);
                }
            };
            imagePanel.setPreferredSize(new Dimension(image.getWidth(),
                                                      image.getHeight()));
            add(imagePanel);
            pack();
            setLocation(loc);
            setVisible(true);
        }
    }

  5. #5
    Yoruichi is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Problem solved, thanks a lot for the help :)

  6. #6
    hablutzel1 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    I was trying to use setSize for a JPanel size... but now it is clear that I should use setPreferredSize() instead, It works even when the Jpanel is empty and with no more than Graphics created using public void paintComponent(Graphics g) ...

Similar Threads

  1. setting applet's size
    By nikita in forum Java Applets
    Replies: 9
    Last Post: 10-01-2008, 04:41 AM
  2. Prob. with screen resolution and Frame size??
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 08-16-2008, 12:51 PM
  3. Image size in a JFrame
    By nickbeacroft in forum AWT / Swing
    Replies: 2
    Last Post: 06-26-2008, 04:08 PM
  4. [SOLVED] How to set the frame size?
    By impact in forum New To Java
    Replies: 7
    Last Post: 05-02-2008, 11:57 AM
  5. how to set an image size
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:27 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
  •