FastImageProvider
- import javax.microedition.lcdui.*;
- import java.util.*;
- public class FastImageProvider implements Runnable{
- public Hashtable images=new Hashtable(500);
- public String[] locatorList={"/intro/0.PNG","/intro/1.PNG","/intro/2.PNG","/intro/3.PNG"};
- public Hashtable loadedScreens=new Hashtable(30);
- public String taskName;
- public String strArg1;
- public FastImageProvider parent;
- public Image getImage(String locator){
- if(images.containsKey(locator)){
- return (Image)images.get(locator);
- }else{
- storeImage(locator);
- return getImage(locator);
- }
- }public void storeImage(String locator){
- try{
- images.put(locator, Image.createImage(locator));
- }catch(Exception e){}
- }public void storeManyImages(String[] locators){
- for(int i=0;i<locators.length;i+=1){
- storeImage(locators[i]);
- }
- }public void loadScreen(String screen){
- if(!loadedScreens.containsKey(screen)){
- for(int i=0;i<locatorList.length;i+=1){
- storeImage("/img/"+screen+locatorList[i]);
- }
- loadedScreens.put(screen,screen);
- }
- }public void loadScreenAsync(String screen){
- if(!loadedScreens.containsKey(screen)){
- new Thread(new FastImageProvider(this,"loadScreen",screen)).start();
- }
- }
- public void run(){
- if(taskName=="loadScreen"){
- if(!parent.loadedScreens.containsKey(strArg1)){
- for(int i=0;i<locatorList.length;i+=1){
- try{
- parent.images.put("/img/"+strArg1+locatorList[i], Image.createImage("/img/"+strArg1+locatorList[i]));
- }catch(Exception e){}
- }
- parent.loadedScreens.put(strArg1,strArg1);
- }
- }
- }public FastImageProvider(FastImageProvider parent, String taskName, String strArg1){
- this.taskName=taskName;
- this.strArg1=strArg1;
- this.parent=parent;
- }public FastImageProvider(){}
- }
При повторном запросе на изображение выдаётся кешированное, что ускоряет приложение по сравнению с Image.createImage каждый раз