我在使用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.
}
}
答案 0 :(得分:1)
评论摘要:
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
实例与屏幕上的实例相同答案 1 :(得分:0)
以下适用于我。
我拿了你的代码,并将面板添加到JFrame中。另外,在将Panel添加到JFrame之前,我会调用getParameters();
。
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
}
}