转换Java游戏到Applet的;不会加载图片 [英] Converted Java Game to Applet; Wont load pictures

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

问题描述

所以我在Java中做了pretty大型游戏,我打算把它放在我的网站,所以我搞砸它,把它变成一个小程序。它启动并运行完全正常,唯一的问题是,它不会加载任何的图片。这里是我目前做的方式:
(记住,我在Applet类创建的对象这是一个JPanel扩展分类并添加小程序)

在屏幕(JPanel中)类:

 的for(int i = 0; I< tileset_ground.length;我++){
        tileset_ground [I] = frame.loadImage(RES / tileset_ground.png);
        tileset_ground [I] =的createImage(新FilteredImageSource(tileset_ground [I] .getSource(),新构造CropImageFilter(0,26 * I,26,26)));
    }

在框架(小程序)类:

 公众形象的LoadImage(字符串URL){
    返回的getImage(得到codeBase的(),URL);
}


解决方案

我发现,当我试图从JAR文件中加载图像或使用一个小程序,我需要有更多的信息只是/folder/file.name 。通常我现在用这个方法:

这是用于设置标签或其他东西的图​​标。

  jLabel.setIcon(ImageIcon的(的getClass()的getResource(/文件夹/ File.name)));

如果我想回到我会用这个形象:

 形象画像=的ImageIcon(的getClass()的getResource(/文件夹/ File.name)。)的getImage()。

有更好的方法来做到这一点,但我发现,这是很容易和足够好了。

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)

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);
}

解决方案

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天全站免登陆