3

I have moved on from C# to java and unable to render the image on java Jframe. Is there any ImageBox component or JFrame method that render the image Like this:

JPictureBox box=new JPictureBox("image_path");
JFrame fram=new JFrame();
fram.add(box); 
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Cody
  • 2,480
  • 5
  • 31
  • 62
  • 1
    Seems like what you looking for is `JLabel`. Use it's [JLabel.setIcon(...)](https://docs.oracle.com/javase/8/docs/api/javax/swing/JLabel.html#setIcon-javax.swing.Icon-) method. Hopefully this little [example](http://stackoverflow.com/a/11372350/1057230) or this detialed [answer](http://stackoverflow.com/a/9866659/1057230), be of some help, on the topic :-) – nIcE cOw Jul 23 '15 at 08:07
  • but this thing renders the image on whole JFrame. It does notr provide the method to scale the image. – Cody Jul 23 '15 at 08:10
  • 2
    @Cody Scaling is an expensive operation and all Swing components don't provide that functionality. You can have a look at [this example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) and [this example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) and [this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) for more of discussion on the topic and the issues involved – MadProgrammer Jul 23 '15 at 08:12
  • @Cody: Play with [Graphics2D](https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html), this provides a means to `scale/transform` related functionality. – nIcE cOw Jul 23 '15 at 08:12
  • 1
    @Cody And [something like this](http://stackoverflow.com/questions/25798156/resizing-icon-to-fit-on-jbutton-in-java/25798462#25798462) for an example of how a component might be made to achieve (it uses a `JButton` but the concept would be applicable to a `JLabel`). You need to remember though, `JLabel` has a lot of additional options, included text, horizontal and vertical alignment for both the text and icon, so it may not be the best base for the solution – MadProgrammer Jul 23 '15 at 08:13
  • [How to Use Labels](http://docs.oracle.com/javase/tutorial/uiswing/components/label.html) would be the basic answer – MadProgrammer Jul 23 '15 at 09:22

1 Answers1

1

This code creates a JFrame and displays the file with path f in that frame. It uses a JLabel as one of the commenters suggests.

    public static void display(String f) throws Exception {
        JFrame jf = new JFrame();
        JLabel jl = new JLabel(new ImageIcon(ImageIO.read(new File(f))));
        jf.add(jl);
        jf.setBounds(0, 0, 200, 200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

Note that you would probably want to handle exceptions better, set the size from the image (you can ask the result of ImageIO.read() for .getWidth() and .getHeight()), and so on.

Here is a slightly more complex example where the image changes size to fill its boundary:

    public static void display(final String f) throws Exception {
        JFrame jf = new JFrame();
        JPanel jp = new JPanel() {
            private BufferedImage bi = ImageIO.read(new File(f));
            public void paint(Graphics g) {
                Graphics2D g2 = (Graphics2D)g;
                RenderingHints rh = new RenderingHints(
                    RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                g2.setRenderingHints(rh);

                int w = getWidth();
                int h = getHeight();
                g.drawImage(bi, 0, 0, w, h, null);
            }
        };
        jf.add(jp);
        jf.setBounds(0, 0, 400, 200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

Note: after reading the links in the comments, I have to recommend MadProgrammer's exhaustive posts on the topic. The only merit of this answer is its short length - but if you want much better down-scaling, ratio-preserving scaling or other in-depth image-related, follow those links.

Community
  • 1
  • 1
tucuxi
  • 17,561
  • 2
  • 43
  • 74