将 Java 游戏转换为 Applet;加载不了图片 [英] Converted Java Game to Applet; Wont load pictures

查看:24
本文介绍了将 Java 游戏转换为 Applet;加载不了图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用 Java 做了一个相当大的游戏,我打算把它放在我的网站上,所以我把它弄乱了,把它变成了一个小程序.它启动并运行得很好,唯一的问题是它不会加载任何图片.这是我目前的做法:(请记住,这是一个 JPanel 扩展类,我在我的小程序类中创建了一个对象并添加到小程序中)

So I made a pretty big game in Java, and I intended to put it on my site so I messed with it and turned it into an applet. It launches and runs perfectly fine, the only problem is it wont load any of the pictures. Here is the way I am doing it currently: (Keep in mind this is a JPanel extended class that I create an object of in my applet class and add to the applet)

在 Screen(JPanel) 类中:

In the Screen(JPanel) class:

    for (int i = 0; i < tileset_ground.length; i++) {
        tileset_ground[i] = frame.loadImage("res/tileset_ground.png");
        tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26, 26)));
    }

在框架(小程序)类中:

In the frame(applet) class:

public Image loadImage(String url){
    return getImage(getCodeBase(), url);
}

推荐答案

我发现当我尝试从 jar 文件加载图像或使用小程序时,我需要获得更多信息,而不仅仅是/folder/file.name.我现在通常使用这种方法:

I found that when I was trying to load images from a jar file or using an applet I needed to have more information that just the /folder/file.name. I normally use this method now:

这是用来设置标签的图标或其他东西.

This is for setting the Icon of a label or something else.

jLabel.setIcon(ImageIcon(getClass().getResource("/Folder/File.name")));

如果我想返回图像,我会使用这个:

If I wanted to return the image I would use this:

Image image = ImageIcon(getClass().getResource("/Folder/File.name")).getImage();

有更好的方法可以做到这一点,但我发现这很容易也足够好.

There are better ways to do it but I found this was easy and good enough.

这篇关于将 Java 游戏转换为 Applet;加载不了图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆