Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Image in Java

I want to display an image but don't know what to do. Whether I have to install some library files or simply it can be done I don't know. Actually I want to do image processing, but first I have to take the image input and display image then I can get the effect of image processing as the output and decide whether it(algorithm) is correct or not. I have installed the eclipse only. I have searched in Google also but whatever they suggest is not working well. Either I have to install something or not.

I have tried the following code:

public class ImageTest {
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run(){
                ImageFrame frame = new ImageFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        }
        );
    }
}

class ImageFrame extends JFrame{
    public ImageFrame(){
        setTitle("ImageTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        ImageComponent component = new ImageComponent();
        add(component);
        getContentPane().validate();
        getContentPane().repaint();
    }

    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 200;
}

class ImageComponent extends JComponent{
    private static final long serialVersionUID = 1L;
    private Image image;
    public ImageComponent(){
        try{
            File image2 = new File("bishnu.jpg");
            image = ImageIO.read(image2);
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    public void paintComponent (Graphics g){
        if(image == null) return;
        int imageWidth = image.getWidth(this);
        int imageHeight = image.getHeight(this);

        g.drawImage(image, 50, 50, this);

        for (int i = 0; i*imageWidth <= getWidth(); i++)
            for(int j = 0; j*imageHeight <= getHeight();j++)
                if(i+j>0) g.copyArea(0, 0, imageWidth, imageHeight, i*imageWidth, j*imageHeight);
    }
}

It simply shows a graphical window but can't show the image "bishnu.jpg"

Should I install anything in eclipse? But I think nothing needs to install.

like image 935
Bishnu Bhattarai Avatar asked Jan 16 '13 07:01

Bishnu Bhattarai


People also ask

How do you insert an image in Java?

Adding an Image in Java JFrame Firstly, we create a JLabel using the Java Swing library. Secondly, we use the setIcon() method to add and display the image. This method defines to display the icon. However, if the value of the icon is null, nothing is displayed.

What is displaying image?

Display picture, often abbreviated as DP, is what users see on the screen of their device, whether it's a desktop monitor, smartphone, or tablet. We've all been on our mobile device and tried to touch one thing but ended up selecting something completely different.


2 Answers

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class DisplayImage {

    public static void main(String avg[]) throws IOException
    {
        DisplayImage abc=new DisplayImage();
    }

    public DisplayImage() throws IOException
    {
        BufferedImage img=ImageIO.read(new File("f://images.jpg"));
        ImageIcon icon=new ImageIcon(img);
        JFrame frame=new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(200,300);
        JLabel lbl=new JLabel();
        lbl.setIcon(icon);
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
like image 194
Abdul Rasheed Avatar answered Sep 20 '22 14:09

Abdul Rasheed


Running your code shows an image for me, after adjusting the path. Can you verify that your image path is correct, try absolute path for instance?

like image 41
OlavJ Avatar answered Sep 18 '22 14:09

OlavJ