我试图在java上创建一个图像,但它不会加载

时间:2015-03-28 22:30:40

标签: java

我试图为我的游戏创建图像,但图像由于某种原因不会加载。 任何人都可以帮助我吗?

这里只是Image loader类代码

package gold;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoader 
{
    public static BufferedImage LoadImage(String path)
    {
        try {
            return ImageIO.read(ImageLoader.class.getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return null;
    }

其余部分是图像应加载的类,但由于某种原因不会。

package gold;

import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Game implements Runnable
{
    private Thread thread;
    private Display display;
    public int width, height; 
    private boolean running = false;
    public String title; 
    private BufferStrategy buf;
    private Graphics d;
    private BufferedImage testImage;
    public Game(String title, int width, int height)
    {
        this.width = width; 
        this.height = height; 
        this.title = title;
        display = new Display(title, width, height);
    }
    private void init()
    {
        display = new Display(title, width, height);
        testImage = ImageLoader.LoadImage("/textures/download.jpg");

    }
    private void tick()
    {

    }
    private void render()
    {
        buf = display.getCanvas().getBufferStrategy();
        if(buf==null)
        {
            display.getCanvas().createBufferStrategy(2);
            return;
        }
        d = buf.getDrawGraphics();
        d.clearRect(0,0, width, height);
        //draw here
        d.drawImage(testImage, 20, 20, null);
        //end Drawing
        buf.show();
        d.dispose();
    }
    public void run()
    {
        init();
        while(running)
        {
            tick();
            render();
        }   

    stop();
    }
    public synchronized void start()
    {
        if(running=true)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }
    public synchronized void stop()
    {
    if(!running)
        return;
    running = false;
    try
    {
        thread.join();
    }catch(InterruptedException e)
    {
        e.printStackTrace();
    }
    }

}

0 个答案:

没有答案