解决了! crew4ok也帮助了其他人,谢谢!以下代码部分出错。我正在尝试将png图像加载到BufferedImage类型但不能这样做,我在ubuntu工作。我有一个名为TicTacToe的根目录,在它下面我有src和res文件夹。在src我有我的java文件和res我有一个png图像。当我尝试从res文件夹访问png文件时,它会出错。 我的目录结构的链接:http://tinypic.com/view.php?pic=210aamd&s=5#.Up38mLUW3h8
package com.blogspot.edwn112;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Game extends JFrame implements MouseListener {
private JPanel panel = new JPanel();
private JPanel gameArea = new JPanel();
private JButton button = new JButton("Play Again");
private JLabel label;
private BufferedImage resizedImage;
public Game() {
addMouseListener(this);
panel.add(button);
BufferedImage image = null;
try {
image = ImageIO.read(new File("/TicTacToe/res/tictactoe.png"));
} catch (IOException e) {
e.printStackTrace();
}
resizedImage = resize(image, 100, 100);
gameArea.add(label);
add(gameArea, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public static BufferedImage resize(BufferedImage image, int width,
int height) {
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return bi;
}
public void paint(Graphics g) {
g.drawImage(resizedImage, 0, 0, getWidth(), getHeight(), null);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Error in native look");
}
JFrame frame = new Game();
frame.setTitle("Tic Tac Toe");
frame.setSize(400, 300);
// frame.pack();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
错误:
存档所需的库:项目'TesTacToe'中的'res / tictactoe.png'无法读取或不是有效的ZIP文件
javax.imageio.IIOException:无法读取输入文件!
在javax.imageio.ImageIO.read(ImageIO.java:1301)
答案 0 :(得分:0)
如果项目根文件夹是TicTacToe,您只想使用 "res/tictactoe.png"
作为文件路径。
TicTacToe (project root dir)
res
tictacttoe.png
src
当你使用这个"TicTacToe/res/tictactoe.png"
你说文件结构是这样的
ProjectRoot
TicTacToe
res
tictactoe.png
答案 1 :(得分:0)
您是否尝试过新文件(getClass()。getResource(“// to tictactoe”);?
答案 2 :(得分:0)
你的应用有足够的权限来读取文件吗? 我在ImageIO.read()源代码中找到了这些行:
if (!input.canRead()) {
throw new IIOException("Can't read input file!");
}
而input.can Read()反过来给了我们:
public boolean canRead() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return fs.checkAccess(this, FileSystem.ACCESS_READ);
}