0

下面是 CMD 在使用 JarSplice 将我的 LWJGL 编译为可运行 Jar 后告诉我的内容。

C:\Users\Rose>java -jar C:\Users\Rose\Desktop\Test\FrikFatJar.jar
Error: java.io.FileNotFoundException: res\350.png (The system cannot find the pa
th specified)
loading glyphs -1
glyphs loaded
loading glyphs -1
glyphs loaded
Exception in thread "main" java.lang.NullPointerException
        at frik.entity.Image.draw(Image.java:164)
        at frik.screen.Menu.draw(Menu.java:44)
        at frik.main.FrikMolder.updateContent(FrikMolder.java:77)
        at frik.main.FrikMolder.isRunning(FrikMolder.java:54)
        at frik.main.MainClass.<init>(MainClass.java:36)
        at frik.main.MainClass.main(MainClass.java:74)

我追溯了这个问题,它与使用java.io.File而不是有关InputStream(),但问题是,我正在使用InputStream().

这是来自我的图像类。

public void init() {


    try{
        texture = TextureLoader.getTexture(textForm, new FileInputStream(textName), true);
    }catch(IOException e){
        System.out.println("Error: "+e);
    }
    rotation = 0;
    if(width != height&&width % 2 != 0&&height % 2 != 0){
        textForm = "PNG";
        textName = "res/128.png";
        JOptionPane.showMessageDialog(null, "The width and height of the Image should be equal.", "Wrong image size!", JOptionPane.WARNING_MESSAGE);
        init();
    }
    timer = new Timer();
}

public void draw() {
    if(ExtraBind){          
        glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
    }else{
        texture.bind();
    }
    glColor4f(c4f[0], c4f[1], c4f[2], c4f[3]);;

    glPushMatrix();

    glTranslatef(x + width /2, y + height /2, 0);
    glRotatef(rotation, 0, 0, 1f);
    glTranslatef(-x - width /2, -y - height /2, 0);

    glBegin(GL_QUADS);

        glTexCoord2f(0,0);
        glVertex2f(x, y);

        glTexCoord2f(1,0);
        glVertex2f(x + width,y);

        glTexCoord2f(1,1);
        glVertex2f(x + width, y + height);


        glTexCoord2f(0,1);
        glVertex2f(x, y + height);

    glEnd();

    glPopMatrix();
    glBindTexture(GL_TEXTURE_2D, 0);
}

这是我试图从我的菜单类加载的图像

IMG = new Image("PNG", "res/350.png", 0,0);

谢谢你的时间。

4

1 回答 1

0

我修好了,这是我在init()

public void init() {


    try{    
        if(Special == true){

            texture = TextureLoader.getTexture(textForm, loadBufferedImage(textName), true);
        }else{
            texture = TextureLoader.getTexture(textForm, new FileInputStream(textName), true);
        }
    }catch(IOException e){
        System.out.println("Error: "+e);
    }
    Special = true;
    rotation = 0;
    if(width != height&&width % 2 != 0&&height % 2 != 0){
        textForm = "PNG";
        textName = "/img/128.png";
        Special = false;
        JOptionPane.showMessageDialog(null, "The width and height of the Image should be equal.", "Wrong image size!", JOptionPane.WARNING_MESSAGE);
        init();
    }

我添加了这个从 jar 文件加载的方法。

private InputStream loadBufferedImage(String string)
{
    try
    {
        BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write( bi, "PNG", baos);
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        return is;
    } catch (IOException e)
    {
        System.out.println("Error: "+e);
    }
    return null;
}
于 2013-09-10T09:06:18.350 回答