How to display an images instantly in Java Applet? -


i created simple memory game in java applet. have problem cards. images need load time during first appearance. how fix that? need show image instantly after card flip.

i display loading screen until application isn't in appstates.ready or appstates.wait_for_start state dosen't help.

memo.cs - main class images loading

public class memo extends japplet {      //...         public void init() {         //...            try {             swingutilities.invokeandwait(new runnable() { @override public void run() { creategui(); }});         }          catch(exception e) {             e.printstacktrace();         }     }      private void creategui() {         final model model = new model(...);         final view view = new view(model);          getcontentpane().add(view, borderlayout.center);         setbackground(backgroundcolor);         setpreferredsize(new dimension(width, height));          model.setloading(loadimages(loadingpath, format, 1));         model.setcardsimages(loadimages(cardimagepath, format, 13));         //...         model.setappstate(appstates.process);          model.startnewgame();         view.repaint();     }      private image[] loadimages(string path1, string path2, int count) {         image[] imgs = new image[count];                 for(int = 0; < count; ++i) {             imgs[i] = getimage(getcodebase(), path1 + + path2);         }         return imgs;     } } 

model.cs - hold images , application state, init board

public class model {      //...      private appstates appstate;     private image[] cardsimages;      public model(...) {         //...         appstate = appstates.init;     }      public void startnewgame() {         setappstate(appstates.process);          //... - init board - table images' id          setappstate(appstates.wait_for_start);     }      public void setcardsimages(image[] cardsimages) {         this.cardsimages = cardsimages;     }      public image getcardsimage(int v) {         return cardsimages[v];     }      public appstates getappstate() {         return appstate;     }      public void setappstate(appstates appstate) {         this.appstate = appstate;         //...     }      //... } 

view.cs - display board

public class view extends jpanel {      //...      private model model;      public view(model model) {         this.model = model;         //...     }      public void paintcomponent(graphics g) {         super.paintcomponent(g);         graphics2d g2d = (graphics2d) g;         if(model != null) {             switch (model.getappstate()) {              //...              case wait_for_start:                         case ready:                 //...                 drawboard(g2d, model.getboard(), model.getstates(), model.getfronttypes());                 break;              }         }         repaint();     }      private void drawboard(graphics2d g2d, int[][] board, int[][] states, int[][] fronttypes) {         if(board != null && states != null && board.length > 0 && states.length > 0) {             for(int x = 0; x < board.length; x++) {                 for(int y = 0; y < board[x].length; y++) {                       if(states[x][y] != model.hide) {                         image img = null;                          //...                         img = model.getcardsimage(board[x][y]);                          g2d.drawimage(                                 img,                                 model.getfirstcardx() + x * model.getcarddistance() + x * model.getcardwidth(),                                  model.getfirstcardy() + y * model.getcarddistance() + y * model.getcardheight(),                                  model.getcardwidth(),                                  model.getcardheight(),                                 null);                          //...                     }                 }             }         }     } } 

from javadocs..

public image getimage(url url)

..

this method returns immediately, whether or not image exists. when applet attempts draw image on screen, data loaded.

use imageio.read(url) instead. method 'block' (it stop @ code line) until image read. when app. goes use image, has been loaded.

another way around problem (not quite way wanted) change..

g2d.drawimage(     img,     model.getfirstcardx() + x * model.getcarddistance() + x * model.getcardwidth(),      model.getfirstcardy() + y * model.getcarddistance() + y * model.getcardheight(),      model.getcardwidth(),      model.getcardheight(),     null); 

to..

g2d.drawimage(     img,     model.getfirstcardx() + x * model.getcarddistance() + x * model.getcardwidth(),      model.getfirstcardy() + y * model.getcarddistance() + y * model.getcardheight(),      model.getcardwidth(),      model.getcardheight(),     this); 

if image loaded asynchronously (e.g. using applet.getimage(url)) gui notified repaint more becomes available.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -