【发布时间】:2014-01-17 00:03:23
【问题描述】:
我在 GraphicsPanel(JPanel 的扩展)中绘制图像时遇到问题。我尝试使用 getCodeBase()、getDocumentBase()、getResource() 和 BufferedImage 从具有路径名的文件加载。有什么方法可以绘制图像而不必在 JLabel 中使其成为 ImageIcon?
package rpg;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
public class GraphicsPanel extends JPanel implements MouseListener, MouseMotionListener {
private WorldBuilder wb;
public int currentTileType = 0;//tile types. 0=bgtile, 1=object, 2=NPC
public String currentTileName = "";
public Image currentTile;
public GraphicsPanel() {
super();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(0, 0, 720, 528);
g.drawImage(currentTile, 100, 100, this);//Nothing gets drawn here
}
public void getParameters(WorldBuilder wb) {
this.wb = wb;
this.currentTileType = wb.currentTileType;
this.currentTileName = wb.currentTileName;
/*
try {
currentTile = ImageIO.read(new File("SpriteSheet.png"));
} catch (IOException e) {
System.out.println("failed");
}
*/
currentTile = new ImageIcon("SpriteSheet.png").getImage();
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
【问题讨论】:
-
在
100x100处绘制可能超出了组件的可见范围。ImageIcon(String)期望String是文件引用,因此如果图像是嵌入资源,则找不到图像。尝试改用ImageIO.read,因为当出现问题时它至少会抛出IOException... -
组件的尺寸为 720x528。我以前使用 ImageIO.read 但这也没有用。目前,我只是将图像文件与 java 文件放在同一目录中以测试它,因为直接从文件引用中读取应该可以这样工作。
-
首先,您没有提供足够的信息来“猜测”组件大小。其次,如果图像文件与类文件(
rpg)在同一个“包”中,那么文件路径是错误的,从应用程序的上下文来看,它更像rpg/SpriteSheet.png。你可以用System.out.println(new File("SpriteSheet.png").exists());来测试它,它很可能会打印出false。如果SpriteSheet.png在应用程序的上下文中,那么您应该尝试改用ImageIO.read(getClass().getResource("SpriteSheet.png"))。 -
对组件尺寸感到抱歉,在我的开场白中忘了说。事实上它确实说“假”所以我猜问题可能是路径名?添加“rpg/SpriteSheet.png”后仍然显示为假。我对默认路径名不太了解。如果我的图像与我的 java 文件在同一个包中,我会使用什么。另外,如果我希望我的图像位于单独的文件夹中,我应该将文件夹放在哪里以及如何引用图像?
-
精灵可能没有被复制到类的相同位置或被视为嵌入资源。你是如何构建你的应用程序的?你试过
ImageIO.read(getClass().getResource("SpriteSheet.png"))吗?