Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By jim829

Thread: displaying scaled picture in JPanel cuts part at bottom and right

  1. #1
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default displaying scaled picture in JPanel cuts part at bottom and right

    Hi,

    I am trying to accurately display a complete picture after scaling it. It cuts 27 bits at the bottom and 7 at right side. The picture I use has a small red frame around it to see if it is correct. The attachment contains the source and the picture I use.

    Many thanks for your help.

    RoMarco
    Attached Files Attached Files

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: displaying scaled picture in JPanel cuts part at bottom and right

    You say you scale the image. So what exactly is the problem? When you scale the source image will be altered, especially when you scale down where you will lose information.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: displaying scaled picture in JPanel cuts part at bottom and right

    Well, it seems that you didn't run the program. If you resize the picture manually when the program is run (pull the frame down and to the right) you will find, that the picture has NOT been cut. It's there! The window doesn't show it if windowWidth and windowHeight are not extended manually by some pixels!!! See line 88. This has nothing to do with rescaling.

    RoMarco

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: displaying scaled picture in JPanel cuts part at bottom and right

    Nope, I didn't. I don't respond well to threads that take the form of "here is the code, good luck". I'm not alone there, you might want to take a step back and actually ask a proper question.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: displaying scaled picture in JPanel cuts part at bottom and right

    IOW, to get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

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

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default SSCCE displaying scaled picture in JPanel cuts part at bottom and right

    Hi,

    I am trying to accurately display a complete picture after scaling it. The window cuts about 27 bits at the bottom and about 7 at right side. The picture I use has a small red frame around it to see if it is correct.

    The attachment contains the simple and fully working source with all comments and the picture I use to demonstrate how the problem shows the problem. At line 88 there is a PATCH that shows what must be done to view the whole picture.

    Easy example included.

    Many thanks for your help.

    RoMarco
    Attached Files Attached Files

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: SSCCE displaying scaled picture in JPanel cuts part at bottom and right

    May I first suggest you post your code in the thread (and between [code] [/code] tags). You may include the image as an attachment.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: SSCCE displaying scaled picture in JPanel cuts part at bottom and right

    OK, now I see you double posted this question. I'll let the moderators resolve this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Default Re: SSCCE displaying scaled picture in JPanel cuts part at bottom and right

    here the code - many thanks RoMarco

    Java Code:
     
    // name: ImagePanel2   test scaling image   rev.a   23.07.2013
    //
    //   purpose: this example was used to find out why the frame
    //	height is never correct. The example was taken from:
    //
    // http://www.java-forums.org/awt-swing/6574-add-image-jframe.html
    //
    
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class ImagePanel2 extends JPanel {
        BufferedImage image;
     
        public ImagePanel2(BufferedImage image) {
            this.image = image;
        }
     
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            g2 = bicubic.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g2.scale(scaleImage, scaleImage);
            g2.drawImage(image, 0, 0, null);
            g2.dispose();
    
            g.drawImage(bicubic, 0, 0, this);
        }
     
        Graphics2D g2 = null;
        static BufferedImage bicubic = null;
        static double scaleImage = 0.4; // factor by which Big image is to reduce for drawing
    
        static Insets insets;
        static int screenHeight;
        static int screenWidth;
        static int imageWidth = 0;
        static int imageHeight = 0;
        static int frameWidth = 0;
        static int frameHeight = 0;
        static int originX = 0;
        static int originY = 0;
    
    
        public static void main(String[] args) throws IOException {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension dim = tk.getScreenSize();
            screenWidth = dim.width;
            screenHeight = dim.height;
    
            String path = "./DrawingWithLimits.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            imageWidth = image.getWidth(null); // width of image
            imageHeight = image.getHeight(null); // height of image
            bicubic = new BufferedImage(imageWidth, imageHeight+10, BufferedImage.TYPE_INT_RGB);
            ImagePanel2 contentPane = new ImagePanel2(image);
    
            // You'll want to be sure this component is opaque
            // since it is required for contentPanes. Some
            // LAFs may use non-opaque components.
            contentPane.setOpaque(true);
    
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(contentPane);
            
            if (insets == null) {
               insets = f.getInsets();
               // they are all zero! (?)
            }
    //System.out.println("insets.left="+insets.left);
    //System.out.println("insets.right="+insets.right);
    System.out.println("insets.top="+insets.top);
    System.out.println("insets.bottom="+insets.bottom);
    
            frameWidth = (int) ((double)imageWidth * scaleImage);
            frameHeight = (int) ((double)imageHeight * scaleImage);
    //
    // frame height is 7 and 27 pixels too small! WHY?
    //
            f.setSize(frameWidth, frameHeight);
    // add some bits to get full width and height...
            //f.setSize(frameWidth+7, frameHeight+27);
    
            originX = (screenWidth-frameWidth)/2;
            originY = (screenHeight-frameHeight)/2;
            f.setLocation(originX, originY);
            f.setVisible(true);
        }
    }

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: displaying scaled picture in JPanel cuts part at bottom and right

    Please go through the Forum Rules, particularly the second paragraph. I've merged two threads here.

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

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: displaying scaled picture in JPanel cuts part at bottom and right

    You're doing a lot of unnecessary work to display the scaled image. Here is what I suggest.


    1. Use the calculated width and height to set the preferredSize of the panel, not the frame.
    2. Add the panel to the frame.
    3. just before you make the frame visible, pack the frame. (i.e. f.pack())
    4. This will properly size the panel. The frame will then be resized to hold the panel.
    5. Use the supplied graphics context to the paintComponent method to draw the image. Cast it to Graphics2D to set the rendering hints.

    Note: Your insets are zero because they are not filled in until you do a pack(). To see what pack() does, consult the source code.

    Also check out the tutorials in my signature. Especially the part on custom painting and any related links it points to.

    Regards,
    Jim
    Roberto Markovic likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 27
    Last Post: 04-06-2012, 02:37 PM
  2. Adding Scaled BufferedImage to ContentPane
    By switch in forum AWT / Swing
    Replies: 12
    Last Post: 01-26-2012, 01:46 AM
  3. Displaying a picture
    By alexisw in forum New To Java
    Replies: 10
    Last Post: 11-08-2011, 07:04 AM
  4. Inventory part 2 help displaying result
    By arrech326 in forum New To Java
    Replies: 4
    Last Post: 12-01-2009, 11:11 PM
  5. Error With Simply Displaying Picture
    By JDCAce in forum Java Applets
    Replies: 4
    Last Post: 10-07-2008, 08:09 PM

Tags for this Thread

Posting Permissions

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