1

我一直在寻找一种以类似网格的方式在 JPanel 上显示图像的方法,并遇到了这个解决方案。该代码运行良好,但是当我尝试通过使用jpg图像而不是绘制矩形来修改它时,我无法让它工作。我的修改如下:

/*
 * courtesy of https://stackoverflow.com/questions/7222988/java-drag-and-drop-images-  in-a-list
 */

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.Serializable;
import javax.swing.*;
import javax.swing.border.Border;

public class ImagePreviewList {

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    JFrame frame = new JFrame("Image panel");
    frame.setSize(800, 300);
    frame.setLocationByPlatform(true);
    JList imageList = createImageList();
    frame.getContentPane().add(new JScrollPane(imageList));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

private static JList createImageList() {

    JList imageList = new JList(createModel());
    imageList.setCellRenderer(new ImageCellRenderer());
    imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    imageList.setVisibleRowCount(-1);
    imageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    imageList.setFixedCellWidth(240);
    imageList.setFixedCellHeight(120);

    imageList.setDragEnabled(true);
    imageList.setDropMode(DropMode.INSERT);
    imageList.setTransferHandler(new ImageTransferHandler(imageList));

    return imageList;
}

private static DefaultListModel createModel() {
    DefaultListModel model = new DefaultListModel();
    model.addElement(new RTS_image("3-Way_Stop_sign"));
    model.addElement(new RTS_image("4-Way_Stop_sign"));
    model.addElement(new RTS_image("No_Entry_sign"));
    model.addElement(new RTS_image("One-Way_Roadway_sign-0"));
    model.addElement(new RTS_image("One-Way_Roadway_sign-1"));
    model.addElement(new RTS_image("One-Way_Roadway_sign-3"));
    model.addElement(new RTS_image("Pedestrian-Priority_sign"));
    model.addElement(new RTS_image("Stop#Go_sign"));
    model.addElement(new RTS_image("Stop_and_Yield_sign"));
    model.addElement(new RTS_image("Stop_sign-0"));
    model.addElement(new RTS_image("Stop_sign-1"));
    model.addElement(new RTS_image("Yield_at_Mini-Circle_sign"));
    model.addElement(new RTS_image("Yield_sign"));
    model.addElement(new RTS_image("Yield_to_Pedestrians_sign"));
    model.addElement(new RTS_image("Yield_to_oncoming_traffic_sign"));
    return model;
}

static class ImageTransferHandler extends TransferHandler {

    private static final DataFlavor DATA_FLAVOUR = new DataFlavor(RTS_image.class,  "Images");

    private final JList previewList;
    private boolean inDrag;

    ImageTransferHandler(JList previewList) {
        this.previewList = previewList;
    }

    public int getSourceActions(JComponent c) {
        return TransferHandler.MOVE;
    }

    protected Transferable createTransferable(JComponent c) {
        inDrag = true;
        return new Transferable() {
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[] {DATA_FLAVOUR};
            }

            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return flavor.equals(DATA_FLAVOUR);
            }

            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                return previewList.getSelectedValue();
            }
        };
    }

    public boolean canImport(TransferSupport support) {
        if (!inDrag || !support.isDataFlavorSupported(DATA_FLAVOUR)) {
            return false;
        }

        JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
        if (dl.getIndex() == -1) {
            return false;
        } else {
            return true;
        }
    }

    public boolean importData(TransferSupport support) {
        if (!canImport(support)) {
            return false;
        }

        Transferable transferable = support.getTransferable();
        try {
            Object draggedImage = transferable.getTransferData(DATA_FLAVOUR);

            JList.DropLocation dl = (JList.DropLocation)support.getDropLocation();
            DefaultListModel model = (DefaultListModel)previewList.getModel();
            int dropIndex = dl.getIndex();
            if (model.indexOf(draggedImage) < dropIndex) {
                dropIndex--;
            }
            model.removeElement(draggedImage);
            model.add(dropIndex, draggedImage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected void exportDone(JComponent source, Transferable data, int action) {
        super.exportDone(source, data, action);
        inDrag = false;
    }
}

static class ImageCellRenderer extends JPanel implements ListCellRenderer {

    DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
    JLabel imageLabel = new JLabel();
    JLabel descriptionLabel = new JLabel();

    ImageCellRenderer() {
        setLayout(new BorderLayout());
        Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        imageLabel.setBorder(emptyBorder);
        descriptionLabel.setBorder(emptyBorder);
        add(imageLabel, BorderLayout.CENTER);
        add(descriptionLabel, BorderLayout.SOUTH);
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        defaultListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        setBorder(defaultListCellRenderer.getBorder());
        setBackground(defaultListCellRenderer.getBackground());
        imageLabel.setIcon((Icon)value);
        descriptionLabel.setText("Description");
        return this;
    }
}

static class RTS_image implements Icon, Serializable {
    private String Name_of_RoadSign; 

    RTS_image(String name_of_roadsign) {
        this.Name_of_RoadSign = name_of_roadsign;
    }

    java.net.URL imgURL = com.umusebo.RTS.ImagePreviewList.class.getResource("images/01_Regulatory_Signs/01_Control_Signs/reg_cont_" + Name_of_RoadSign + ".jpg");

    Image img = Toolkit.getDefaultToolkit().createImage(imgURL);

    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.drawImage(img, x, y, c);
    }

    public int getIconWidth() {
        return 114;
    }

    public int getIconHeight() {
        return 125;
    }

    public String getName_of_RoadSign() {
        return Name_of_RoadSign;
    }

    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        return Name_of_RoadSign.equals(((RTS_image)o).Name_of_RoadSign);
    }

}

}

执行后出现以下错误(对于我尝试显示的 15 张图像重复 15 次):

Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

问题可能出在哪里?

4

1 回答 1

3

您已经声明了许多您尝试使用来自其他变量的值来初始化的类级别字段null

例如,您正在imgURL从变量声明和初始化Name_of_RoadSign,这将是null.

java.net.URL imgURL = com.umusebo.RTS.ImagePreviewList.class.getResource(
    "images/01_Regulatory_Signs/01_Control_Signs/reg_cont_"
    + Name_of_RoadSign + ".jpg");

您必须记住,类字段是在执行任何类构造函数之前初始化的。

尝试在构造函数中初始化这些类变量

RTS_image(String name_of_roadsign) {
    this.Name_of_RoadSign = name_of_roadsign;
    imgURL = com.umusebo.RTS.ImagePreviewList.class.getResource(
        "images/01_Regulatory_Signs/01_Control_Signs/reg_cont_"
        + Name_of_RoadSign + ".jpg");
    img = Toolkit.getDefaultToolkit().createImage(imgURL);
}

ToolKit#createImage往往会默默地忽略错误,null即使无法加载图像也会返回 none 值。你最好使用它ImageIO。除了支持更广泛的图像格式之外,它还会抛出更有意义的异常,从而更容易诊断问题。

例如...

img = ImageIO.read(imgURL);

不要忘记,这会抛出一个,IOException所以你要么需要抓住它,要么(更好)将它传回给调用者。

我还鼓励您熟悉Java 编程语言的代码约定。你不会通过忽视他们来结交任何朋友或影响他们。;)

于 2013-02-03T19:30:14.173 回答