Search code examples
javaswingjlabel

Displaying an image on a JLabel


I am having trouble displaying an image on a JLabel. I have another class called ControlPanel which saves the image in the project folder. I had the methods which save the image in this class but for some reason, I was getting a NullPointerException. When I moved them in the other class everything started working properly. The actual image is a bufferedImage on which the user draws.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GalleryPanel extends JPanel 
{

    private static final long serialVersionUID = 1L;
    private JLabel[] images;
    private static final int MAX_IMAGES = 12;
    private int currentImage;


    public void init()
    {   
        images = new JLabel[MAX_IMAGES];
        setLayout(new GridLayout(3,4));
        setBackground(Color.GRAY);
    }


    public void addImageToGallery(String fileName, String fileExtension)
    {
        if ( currentImage < images.length - 1)
        {   
            ImageIcon icon = new ImageIcon(fileName + fileExtension);

            images[currentImage] = new JLabel(icon, JLabel.CENTER);

            add(images[currentImage]);

            displayImage(currentImage);

            currentImage++;
        }
        else
        {
            throw new ArrayIndexOutOfBoundsException("The gallery is full");
        }
    }


    // display the doily image in the gallery
    public void displayImage(int index)
    {
        images[index].setSize(300, 300);
        add(images[index]);
    }


    public final int getMaxImages()
    {
        return MAX_IMAGES;
    }


    public Dimension getPreferredSize() 
    {
          return new Dimension(380, 700);
    }

}

those are the two methods in my other class which are responsible for saving the actual image

// Shows a dialog box which asks the user to choose a name for the file that he wants to save
public void saveImage(BufferedImage bufImage)
{
    String fileName = JOptionPane.showInputDialog("Choose image name");

    if (fileName != null)
    {   
        if(fileName.equals(""))
        {
            fileName = "Untitled";
        }

        chooseImageFormat(bufImage, fileName);
    }   

}


    //shows a dialog box which asks the user to select the file format of the image he would like to save
public void chooseImageFormat(BufferedImage bufImage, String fileName)
{
    Object[] imageFormats = {"PNG", "JPEG"};

    String userInput = (String) JOptionPane.showInputDialog(null, "Choose file format", "File Format Settings", JOptionPane.PLAIN_MESSAGE, null, imageFormats, "PNG");


    String imageFormat = (userInput.equals("PNG")) ? "PNG" : "JPEG";
    String fileExtension = (imageFormat.equals("PNG")) ? ".png" : ".jpg";

    File file = new File(fileName + fileExtension );

    try 
    {
        ImageIO.write(bufImage, imageFormat, file);
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    gallery.addImageToGallery(fileName, fileExtension);
}

Solution

  • I think you declare your gallery like this :

    GalleryPanel gallery;
    

    For that you get NullPointerException, so instead use this :

    GalleryPanel gallery = new GalleryPanel();
    

    EDIT

    It worked but is there a way to not instantiate gallery like this?

    You should to declare it and initialize it before you use it, there are another solution but you should to make many changes in your code, you have to make :

    public static void addImageToGallery(String fileName, String fileExtension) {
    

    You can call a static method like this

    GalleryPanel.addImageToGallery(fileName, fileExtension);
    

    But like i said, you should to make many changes.

    Good luck.