javafx - How to change ImageView on KeyPressed -


i having difficulty trying solve this, trying figure out how change imageview imageview when press key on keyboard have no idea how to, have looked around everywhere can think of haven't got clue.

heres code, character class:

import java.io.ioexception; import java.io.inputstream; import java.nio.file.files; import java.nio.file.paths;  import javafx.animation.animation; import javafx.application.application; import javafx.geometry.rectangle2d; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.input.keycode; import javafx.scene.paint.color; import javafx.stage.stage; import javafx.util.duration;   public class character extends application{  private static final int columns  =  3; private static final int count    =  3; private static final int width    = 48; private static final int height   = 50; imageview imgview;  public void start(stage primarystage) throws exception {     group root = new group();      imgview = characterstill();     root.setonkeypressed(e -> {         if(e.getcode().equals(keycode.right)){             try {                 imgview = characterwalking();             } catch (ioexception ex) {}         }     });     root.setonkeyreleased(e -> {         if(e.getcode().equals(keycode.right)){             try {                 imgview = characterstill();             } catch (ioexception ex) {}         }     });      root.getchildren().add(imgview);     scene scene = new scene(root, menu.width, menu.height, color.greenyellow);     primarystage.setscene(scene);     primarystage.show(); } public imageview characterstill() throws ioexception{     inputstream = files.newinputstream(paths.get("c:\\users\\javier\\desktop\\stickhero\\stillninja.png"));     image characterstill = new image(is);     imageview stillview = new imageview(characterstill);      return stillview; } public imageview characterwalking() throws ioexception{     final inputstream = files.newinputstream(paths.get("c:\\users\\javier\\desktop\\stickhero\\walkingninja.png"));     final image characterwalking = new image(is);     imageview charview = new imageview(characterwalking);      charview.setviewport(new rectangle2d(0, 0, width, height));      final animation animation = new animationgen(charview, duration.millis(300), count, columns, 0, 0, width, height);     animation.setcyclecount(animation.indefinite);     animation.play();      return charview; } } 

in java, objects accessed via reference. important keep distinction between object in memory, , reference object, clear.

when execute

new imageview(characterstill); 

a new imageview object created in memory. when (effectively) do

imgview = new imageview(characterstill); 

you assign variable imgview reference object. can think of reference memory location of object (though in reality doesn't have implemented way).

what happens in code @ initialization following:

imgview = new imageview(characterstill); root.getchildren().add(imgview); 

so imgview contains reference imageview object displaying characterstill image. pass reference child list of root, child list of root contains reference same imageview object.

now, when user presses right key, (effectively) execute

imgview = new imageview(characterwalking); 

this creates new imageview object in memory, , assigns reference (think: memory location of new object) variable imgview.

however, child list of root hasn't changed: still contains original reference first imageview object. nothing changes in ui.

you fix replacing child list of root newly created imageview, i.e.:

root.setonkeypressed(e -> {     if(e.getcode().equals(keycode.right)){         try {             imgview = characterwalking();             root.getchildren().setall(imgview);         } catch (ioexception ex) {}     } }); 

however, not efficient. on every key press, create brand new object in memory, image freshly loaded hard drive. discard previous object , put new object in ui.

a better way use single imageview object, , replace image displays. can calling

imgview.setimage(...); 

to make things more efficient, instead of loading image disk every time, load images @ startup , reuse them. code looks like

import java.io.ioexception; import java.io.inputstream; import java.nio.file.files; import java.nio.file.paths;  import javafx.animation.animation; import javafx.application.application; import javafx.geometry.rectangle2d; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.input.keycode; import javafx.scene.paint.color; import javafx.stage.stage; import javafx.util.duration;   public class character extends application{      private static final int columns  =  3;     private static final int count    =  3;     private static final int width    = 48;     private static final int height   = 50;     private imageview imgview;     private image characterstill ;     private image characterwalking ;     private animation animation ;      public void start(stage primarystage) throws exception {          imgview = new imageview();         characterstill = loadcharacterstill();         characterwalking = loadcharacterwalking();         imgview.setviewport(new rectangle2d(0, 0, width, height));          animation = new animationgen(charview, duration.millis(300), count, columns, 0, 0, width, height);         animation.setcyclecount(animation.indefinite);          imgview.setimage(characterstill);          group root = new group();          root.setonkeypressed(e -> {             if(e.getcode().equals(keycode.right)){                 imgview.setimage(characterwalking);                 animation.play();             }         });         root.setonkeyreleased(e -> {             if(e.getcode().equals(keycode.right)){                 imgview.setimage(characterstill);                 animation.stop();             }         });          root.getchildren().add(imgview);         scene scene = new scene(root, menu.width, menu.height, color.greenyellow);         primarystage.setscene(scene);         primarystage.show();     }      public image loadcharacterstill() throws ioexception{         inputstream = files.newinputstream(paths.get("c:\\users\\javier\\desktop\\stickhero\\stillninja.png"));         image characterstill = new image(is);         return characterstill ;     }      public image loadcharacterwalking() throws ioexception{         final inputstream = files.newinputstream(paths.get("c:\\users\\javier\\desktop\\stickhero\\walkingninja.png"));         final image characterwalking = new image(is);         return characterwalking ;     }  } 

obviously, since don't have access images or animationgen class, haven't tested this; should give idea though.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -