为啥java小窗口显示不了背景图片?

我想学着做一个马里奥小游戏,为啥java小窗口显示不了背景图片?就是空的,图片格式是PNG,我排查不出其它问题了

代码:

package com.tedu.day1301.com.sxt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;


public class MyFrame extends JFrame implements KeyListener {
//用于存储所有的场景
private List<BackGround> allBg=new ArrayList<BackGround>();

//用于存储当前的背景
private BackGround nowBg = new BackGround();

//用于双缓存
private Image offScreenImage = null;

    public MyFrame(){
        //设置窗口大小
        this.setSize(800,600);
        //设置窗口居中显示
        this.setLocationRelativeTo(null);
        //设置窗口的可见性
        this.setVisible(true);
        //设置点击窗口上的关闭键,结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口大小不变
        this.setResizable(false);
        //向窗口对象添加键盘接听器
        this.addKeyListener(this);
        //设置窗口名称
        this.setTitle("超超超超级玛丽");
        setLayout(null);
        //初始化图片
        StaticValue.init();
        //创建全部场景
        for(int i =1;i<=3;i++){
            allBg.add(new BackGround(i,i==3?true:false));
        }
        //将第一个场景设置为当前场景
        nowBg = allBg.get(2);
        //绘制图像
        repaint();

    }



    @Override
    public void paint(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = createImage(800, 600);
        }

        Graphics graphics = offScreenImage.getGraphics();
        graphics.fillRect(0, 0, 800, 600);

        // 绘制背景
        graphics.drawImage(nowBg.getBgImage(), 0, 0, this);

        // 将图像绘制到窗口中
        g.drawImage(offScreenImage, 0, 0, this);

        super.paint(g);
    }


    public static void main(String[] args) {
        MyFrame aa = new MyFrame();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
package com.tedu.day1301.com.sxt;

import java.awt.image.BufferedImage;

public class BackGround {


    //当前场景要显示的图片
    private BufferedImage bgImage=null;
    //判断当前是第几个场景
    private int sort;
    //判断是否是最后一个场景
    private boolean flag;

    public BackGround(){

    }

    public BackGround(int sort,boolean flag){
        this.sort=sort;
        this.flag=flag;

        if(flag){
            bgImage= StaticValue.bg2;
        }else{
            bgImage=StaticValue.bg;
        }
    }

    public BufferedImage getBgImage() {
        return bgImage;
    }

    public int getSort() {
        return sort;
    }

    public boolean isFlag() {
        return flag;
    }

}
package com.tedu.day1301.com.sxt;

public class BufferedIamge {
}
package com.tedu.day1301.com.sxt;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StaticValue  {
//背景
    public static BufferedImage bg= null;
    public static BufferedImage bg2= null;

//马里奥向左跳跃
    public static BufferedImage jumpL= null;
//马里奥向右跳跃
    public static BufferedImage jumpR= null;
//马里奥向左站立
    public static BufferedImage standL= null;
//马里奥向右站立
    public static BufferedImage standR= null;
//城堡
    public static BufferedImage tower= null;
//旗杆
    public static BufferedImage gan = null;
//障碍物
    public static List<BufferedImage> obstacle=new ArrayList<BufferedImage>();
//马里奥向左跑
     public static List<BufferedImage> runL=new ArrayList<BufferedImage>();
//马里奥向右跑
    public static List<BufferedImage> runR=new ArrayList<BufferedImage>();
//蘑菇敌人
    public static List<BufferedImage> mogu=new ArrayList<BufferedImage>();
//食人花敌人
    public static List<BufferedImage> flower=new ArrayList<BufferedImage>();
//路径的前缀,方便以后调用
    public static String path=System.getProperty("user.dir")+"/src/images";
    //初始化方法
    public static void init(){
        //加载背景图片
        try {
            bg = ImageIO.read(new File(path + "bg.png"));
            bg2 = ImageIO.read(new File(path + "bg2.png"));
//加载马里奥向左/向右站立
            standL = ImageIO.read(new File(path + "s_mario_stand_L.png"));
            standR = ImageIO.read(new File(path + "s_mario_stand_R.png"));
//加载城堡
           tower = ImageIO.read(new File(path + "tower.png"));
//加载旗杆
            gan = ImageIO.read(new File(path + "gan.png"));
//加载马里奥向左跳跃
            jumpL = ImageIO.read(new File(path + "s_mario_jump_L.png"));
//加载马里奥向右跳跃
            jumpR = ImageIO.read(new File(path + "s_mario_jump_R.png"));
        }catch(Exception a){

        }


    //加载马里奥向左跑

        for (int i = 0; i <=2 ; i++) {
            try {
                runL.add(ImageIO.read(new File(path + "s_mario_run"+i+"_L.png")));
            }catch(IOException a){

            }
        }


        for (int i = 0; i <=2 ; i++) {
            try {
                runR.add(ImageIO.read(new File(path + "s_mario_run"+i+"_R.png")));
            }catch(IOException a){

            }
        }


try{
    //加载障碍物
        obstacle.add(ImageIO.read(new File(path+"brick.png")));
        obstacle.add(ImageIO.read(new File(path+"soil_up.png")));
        obstacle.add(ImageIO.read(new File(path+"soil_base.png")));}
catch(Exception a){

}

//加载水管
        try{
        for(int i = 1;i<=4;i++){
            obstacle.add(ImageIO.read(new File(path+"pipe"+i+".png")));
        }}

        catch(Exception b){

        }
//加载不可破坏的砖块和旗子
        try{
            for(int i = 1;i<=4;i++){
            obstacle.add(ImageIO.read(new File(path+"brick2.png")));
            obstacle.add(ImageIO.read(new File(path+"flag.png")));
            }
        }catch(Exception a){

        }
//加载蘑菇敌人

    try{
        for(int i =1;i<=3;i++){
            mogu.add(ImageIO.read(new File(path+"fungus"+i+".png")));
        }}
    catch(Exception c){

    }

//加载食人花敌人
        try{    for(int i =1;i<=2;i++){
            flower.add(ImageIO.read(new File(path+"flower1."+i+".png")));
        }}
        catch(Exception c){

        }


    }


}


目录:


Day13
src
com
tedu
day1301
com
sxt
obj
BackGround
BufferedIamge
MyFrame
StaticValue
...

images(包)
bg.png
bg2.png
...


所有图片都显示不了吗?显示不了,应该是路径不对。
将path路径输出看看是否为存放图片的目录。
可以参考下面的代码,来判断文件资源是否存在。

bg = ImageIO.read(new File(path + "bg.png"));

改为:
 File file =new File(path + "bg.png");
 if (!file.exists()){
    System.out.println("文件不存在,请检查路径是否正确!");
}
bg = ImageIO.read(file);

在项目名称下面建一个images的文件夹,images文件夹放在src的外面,参考

img

bg = new JLabel(new ImageIcon("images/login.jpg"));
// 或者
Image img = new ImageIcon("images.bg.png").getImage();

盘符加上。

public static String path=System.getProperty("user.dir")+"/src/images";

这行代码最后再加一个/



  bg = ImageIO.read(new File(path + "src\\images\\bg.png"));
            bg2 = ImageIO.read(new File(path + "src\\images\\bg2.png"));

```这样吗?还是不显示...

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632