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

    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
    Cebu City, Philippines
    Rep Power


    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
    Rep Power


    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
        /** Option 2: override [i]getPreferredSize[/i] method. */
        public Dimension getPreferredSize() {
            return size;

Similar Threads

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