[英]Can not paint an image in my JPanel
I'm having trouble painting an image in my GraphicsPanel (extension of JPanel). 我在我的GraphicsPanel(JPanel的扩展名)中绘画图像时遇到麻烦。 I tried loading from a file with the pathname, using getCodeBase(), getDocumentBase(), getResource(), and using BufferedImage. 我尝试使用getCodeBase(),getDocumentBase(),getResource()和BufferedImage从具有路径名的文件中加载。 Is there any way to draw the image without having to make it an ImageIcon inside a JLabel? 有没有什么方法可以绘制图像而不必在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.
}
} }
Summary of comments: 评论摘要:
SpriteSheet.png
is not located within location that ImageIcon(String)
would be able to find it. SpriteSheet.png
不在ImageIcon(String)
能够找到的位置。 ImageIcon(String)
expects that the String
references a File
on the file system, but SpriteSheet.png
is stored within the application context ( src/rpg/SpriteSheet.png
), this makes it an embedded resource. ImageIcon(String)
期望String
引用文件系统上的File
,但是SpriteSheet.png
存储在应用程序上下文( src/rpg/SpriteSheet.png
)中,这使其成为嵌入式资源。 Class#getResource
to load embedded resources, in this case either getClass().getResource("SpriteSheet.png")
or getClass().getResource("/rpg/SpriteSheet.png")
to be be sure. 使用Class#getResource
加载嵌入式资源,在这种情况下,请getClass().getResource("SpriteSheet.png")
使用getClass().getResource("SpriteSheet.png")
或getClass().getResource("/rpg/SpriteSheet.png")
。 ImageIO.read
over ImageIcon
. 在ImageIcon
使用ImageIO.read
。 It will at least throw an IOException
when the image can not be loaded for some reason, where as ImageIcon
can fail silently. 当由于某种原因而无法加载图像时,它至少会引发IOException
,因为ImageIcon
可能会静默失败。 GraphicsPanel
which is loading the resources is the same instance that is on the screen 确保正在加载资源的GraphicsPanel
实例与屏幕上的实例相同。 The following works for me. 以下对我有用。 I took your code, and added the panel to a JFrame. 我获取了您的代码,并将面板添加到JFrame中。 Also, I call getParameters();
另外,我叫getParameters();
before adding the Panel to the JFrame. 在将面板添加到JFrame之前。
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.JFrame;
import javax.swing.JLabel;
import java.awt.image.BufferedImage;
public class GraphicsPanel extends JPanel implements MouseListener,
MouseMotionListener {
public static void main(String args[]) {
GraphicsPanel s = new GraphicsPanel();
s.getParameters();
JFrame frame = new JFrame();
frame.add(s);
frame.setVisible(true);
}
/**
*
*/
private static final long serialVersionUID = 1L;
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.drawImage(currentTile, 100, 100, this);//Nothing gets drawn here
}
public void getParameters() {
currentTile = new ImageIcon("test.jpg").getImage();
repaint();
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.