java - 无法在我的 JPanel 中绘制图像

标签 java image swing jpanel

我在 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.
}

}

最佳答案

评论摘要:

  • SpriteSheet.png 未位于 ImageIcon(String) 能够找到的位置。 ImageIcon(String) 期望 String 引用文件系统上的 File,但 SpriteSheet.png 存储在应用程序上下文 (src/rpg/SpriteSheet.png) 中,这使其成为嵌入式资源。
  • 使用 Class#getResource 加载嵌入资源,在本例中可以肯定是 getClass().getResource("SpriteSheet.png")getClass().getResource("/rpg/SpriteSheet.png")
  • 使用ImageIO.read而不是ImageIcon。当由于某种原因无法加载图像时,它至少会抛出 IOException ,而 ImageIcon 可能会默默地失败。
  • 确保加载资源的 GraphicsPanel 实例与屏幕上的实例相同

关于java - 无法在我的 JPanel 中绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175448/

相关文章:

java - JXList 排序不起作用(swingx 库)

java - Stringbuilder 无法正常工作

java - 使用 JDBC 获取与表相对应的所有外键以及保存这些外键的表

java - JJWT 库和处理过期 ExpiredJWTException

java - UnsatisfiedLinkError Java 与 Rxtx 库

css - 2 个带有背景图像和内容的容器

android - 来自 URL 的 setimageBitmap 不适合我想要的大小

java - 主题切换后 Intellij 插件 JBTabbedPane UI 被替换

python - 在 Python 中保存异构图像网格

java - JFileChooser弹出2次