在 JLabel 上显示图像

Displaying an image on a JLabel

我在 JLabel 上显示图像时遇到问题。我有另一个名为 ControlPanel 的 class,它将图像保存在项目文件夹中。我有将图像保存在此 class 中的方法,但由于某种原因,我收到了 NullPointerException。当我将它们移到另一个 class 时,一切都开始正常工作。实际图像是用户绘制的 bufferedImage。

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);
    }

}

这是我另一个class中的两个方法,它们负责保存实际图像

// 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);
}

我想你是这样声明你的画廊的:

GalleryPanel gallery;

为此你得到 NullPointerException,所以改用这个:

GalleryPanel gallery = new GalleryPanel();

编辑

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

你应该在使用它之前声明它并初始化它,还有另一种解决方案但是你应该在你的代码中做很多改变,你必须做:

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

你可以像这样调用静态方法

GalleryPanel.addImageToGallery(fileName, fileExtension);

但是就像我说的,你应该做很多改变。

祝你好运。