preloading images in Java

女生的网名这么多〃 提交于 2019-12-24 07:28:32

问题


So, I'm programming a basic TowerDefense game. It's coming together great, but I'm stuck trying to preload images. What my predicament is is that when I call the paint method for my Canvas class (it extends JPanel) it opens a thread called "Image Fetcher x" where 'x' is a number starting with 0 and going up for multiple instances of the thread. As far as I understand, this thread is taking the images from my image variables, locating them on the disk, loading them into RAM for my game to display, and then drawing them.
This works fine, however, in the middle of the game, there are moments where nothing appears because the draw method I call is attempting to draw an image which hasn't been loaded. Gameplay is still effective, but the visual representation is messed up because the player basically sees a grass block shooting an enemy. Then all of a sudden the image loads and is drawn and looks normal.

This is fine and all, but I want to be able to pre-load all the resources (and possibly make a loading screen so the player knows that the game is loading) so that the non-loaded images don't destroy the illusion of gameplay - if that makes sense. I have tried using a MediaTracker, that didn't work. I've researched this and can't seem to find anything anywhere on this.

My current code consists of arrays of images from an image map. Each image in an array is drawn on the canvas according to certain identifiers which tell the game which tile/tower to draw. I am using the java.awt.Image class to store my images. So, is there any way to preload my images so that they don't have to load mid-gameplay?
Thanks!

Edit:
Code:

for(int y=0; y<tileMap.length; y++) {
    for(int x=0; x<tileMap[y].length; x++) {
        tileMap[y][x] = new ImageIcon("res/tileMap.png").getImage();
        tileMap[y][x] = createImage(new FilteredImageSource(tileMap[y][x].getSource(), new CropImageFilter(x * Room.blockSize, y * Room.blockSize, Room.blockSize, Room.blockSize))); 
    }
}

public void paintcomponent(Graphics g) {
    g.drawImage(tileMap[1][2], 5, 7, null);
}

That's a simplified version, I have about 1500 lines so I don't think you want to see all that, lol. Basically, I just create the image variables and then draw them. At the point of drawing them, an Image Fetcher thread is initiated, loads the image and then disappears after the image has finished loading. While the image is loading (and the Image Fetcher thread is active) the drawImage method draws nothing since the image isn't preloaded into RAM. However, once it's done loading the image, the drawImage method actually draws the correct image and the Image Fetcher thread is no longer active. Is there a way to either initiate the Image Fetching process so that this all happens before starting the program?
I've also tried just drawing everything at the beginning, that didn't work since the program runs too fast for the Image Fetchers to successfully load every image before the user plays. idk, the Image Fetchers are complicated. I just need to know how to preload images into RAM.

来源:https://stackoverflow.com/questions/53806134/preloading-images-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!