import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class spriteStore {
public static BufferedImage playerStanding;
public void getImage()
{
try
{
playerStanding = ImageIO.read(new File("Cobalt\\pictures\\playerStanding1.png"));
}
catch(Exception e){System.out.println("Picture not found");}
}
}
我正在尝试读取图像以保存为 BufferedImage 对象,但是当我运行主代码时,
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Cobalt {
public Boolean movingLeft, movingRight, firstJump, secondJump;
public int jump = 0;
public Dimension screenSize;
public JFrame frame;
public JPanel panel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run(){
new Cobalt();
}
});
}
public Cobalt()
{
frame = new JFrame("COBALT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
frame.setSize(500,500); //width and height
panel = new MyPanel();
frame.getContentPane().add(panel);
}
class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public void paint(Graphics g)
{
Graphics g2D = (Graphics2D) g;
super.paint(g);
g2D.drawImage(spriteStore.playerStanding, 100, 100, null);
}
}
}
并且图像不会显示。我正在使用 eclipse,并且相对来说是个菜鸟,所以请告诉我我的错误。