gpt4 book ai didi

java - java程序中图像源路径异常

转载 作者:行者123 更新时间:2023-11-30 04:54:20 25 4
gpt4 key购买 nike

我正在关注this使用eclipse的教程。我收到图像路径的异常。我在程序 src 目录中创建了一个文件夹 images 并将图像放入其中。我还尝试将图像直接放入 src 文件夹中。但结果是一样的。

这是异常的程序和堆栈跟踪。

逃亡:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Puzzle.initUI(Puzzle.java:47)
at Puzzle.<init>(Puzzle.java:31)
at Puzzle$1.run(Puzzle.java:141)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Java 程序:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Puzzle extends JFrame implements ActionListener {

private JPanel centerPanel;
private JButton button;
private JLabel label;
private Image source;
private Image image;
int[][] pos;
int width, height;

public Puzzle() {

initUI();
}

public final void initUI() {

pos = new int[][]{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{9, 10, 11}
};


centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4, 4, 0, 0));

ImageIcon sid = new ImageIcon(Puzzle.class.getResource("/images/mickey.jpg"));
source = sid.getImage();

width = sid.getIconWidth();
height = sid.getIconHeight();


add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);


for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
if (j == 2 && i == 3) {
label = new JLabel("");
centerPanel.add(label);
} else {
button = new JButton();
button.addActionListener(this);
centerPanel.add(button);
image = createImage(new FilteredImageSource(source.getSource(),
new CropImageFilter(j * width / 3, i * height / 4,
(width / 3) + 1, height / 4)));
button.setIcon(new ImageIcon(image));
}
}
}

setSize(325, 275);
setTitle("Puzzle");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {

JButton button = (JButton) e.getSource();
Dimension size = button.getSize();

int labelX = label.getX();
int labelY = label.getY();
int buttonX = button.getX();
int buttonY = button.getY();
int buttonPosX = buttonX / size.width;
int buttonPosY = buttonY / size.height;
int buttonIndex = pos[buttonPosY][buttonPosX];



if (labelX == buttonX && (labelY - buttonY) == size.height) {

int labelIndex = buttonIndex + 3;

centerPanel.remove(buttonIndex);
centerPanel.add(label, buttonIndex);
centerPanel.add(button, labelIndex);
centerPanel.validate();
}

if (labelX == buttonX && (labelY - buttonY) == -size.height) {

int labelIndex = buttonIndex - 3;
centerPanel.remove(labelIndex);
centerPanel.add(button, labelIndex);
centerPanel.add(label, buttonIndex);
centerPanel.validate();
}

if (labelY == buttonY && (labelX - buttonX) == size.width) {

int labelIndex = buttonIndex + 1;

centerPanel.remove(buttonIndex);
centerPanel.add(label, buttonIndex);
centerPanel.add(button, labelIndex);
centerPanel.validate();
}

if (labelY == buttonY && (labelX - buttonX) == -size.width) {

int labelIndex = buttonIndex - 1;

centerPanel.remove(buttonIndex);
centerPanel.add(label, labelIndex);
centerPanel.add(button, labelIndex);
centerPanel.validate();
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
Puzzle puzzle = new Puzzle();
puzzle.setVisible(true);
}
});
}
}

目录结构:

swingExamples (it's java project)
--src
--bin
--images
--.settings

最佳答案

您需要构建程序的类路径中的图像文件,而不是源文件夹中的图像文件。这意味着无论您的 Puzzle.class 文件被编译到哪里,您都需要一个在类路径根目录下包含 jpeg 的 Images 文件夹。如果您提供程序的目录结构,我可以为您提供更多帮助。

关于java - java程序中图像源路径异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9039411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com