Results 1 to 3 of 3
  1. #1
    KamilR is offline Member
    Join Date
    Apr 2008
    Location
    Prague, CZE
    Posts
    4
    Rep Power
    0

    Unhappy JPanel in agreement with dimension of pictures

    Hi, I have a JPanel, i need draw a picture into this panel, but i need set width and height of this panel same like width and height of pictures, how should i do?

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    If you can get the dimension of your picture, then you can also do it....

    Initialize the GUI.
    Get the picture's dimension,
    Resize the frame.
    Resize the panel. and other components if affected.
    Draw the picture,
    update the GUI.
    freedom exists in the world of ideas

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

    Default

    The JPanel will report its preferredSize to its parent container. You can use the setPreferredSize method _or_ override the getPreferredSize method to set the preferredSize of the panel. If the panel is added to a container whose layout manager respects the preferredSize of its children, ie, tries to display the children at their preferredSize, the panel should appear at the same size as the image.
    In java this could look something like this:
    Java Code:
    class Pseudo extends JPanel {
        BufferedImage image;
        Dimension size = new Dimension();
    
        Pseudo(BufferedImage image) {
            this.image = image;
            size.setSize(image.getWidth(), image.getHeight());
            // Option 1: use [i]setPreferredSize[/i] method.
            setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);  // just in case
            g.drawImage(image,0,0,this);
        }
    
        /** Option 2: override [i]getPreferredSize[/i] method. */
        public Dimension getPreferredSize() {
            return size;
        }
    }

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  2. JPanel in agreement with dimension of pictures
    By KamilR in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2008, 07:38 AM
  3. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:24 AM
  4. Help Loading Up Pictures
    By marco in forum Java Applets
    Replies: 3
    Last Post: 08-14-2007, 10:32 PM
  5. Help with pictures en Java
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:36 AM

Posting Permissions

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