0

我有一个显示背景图像的 java 代码。(pizza1.png)我希望它在我按下箭头键时改变(案例 HRcEvent.VK_RIGHT :) 但我不知道输入什么所以它会重绘图像,它改变了图像路径的字符串,但它没有不要重绘它。有人可以帮助我吗?

public class HelloTVXlet implements Xlet, UserEventListener, ResourceClient, HBackgroundImageListener{

   public String afbeelding = "pizza1.png";

    private HScreen screen;
    private HBackgroundDevice bgDevice;
    private HBackgroundConfigTemplate bgTemplate;
    private HStillImageBackgroundConfiguration bgConfiguration;
    private HBackgroundImage agrondimg = new HBackgroundImage(afbeelding);

    public void notifyRelease(ResourceProxy proxy){}
    public void release(ResourceProxy proxy){}
    public boolean requestRelease(ResourceProxy proxy, Object requestData){
    return false;
    }

    public void imageLoaded (HBackgroundImageEvent e){
    try{
    bgConfiguration.displayImage(agrondimg);
    }
    catch (Exception s){
    System.out.println(s.toString());
    }
    }

    public void imageLoadFailed(HBackgroundImageEvent e){
    System.out.println("Image kan niet geladen worden");
    }




    private XletContext actueleXletContext;
    public static HScene scene;
    int waardex = 20;
    int waardey = 20;
    // x; y; 
//   public static MijnComponent mc = new MijnComponent("sterren.PNG");
    // debuggen of niet ?
    private boolean debug=true;

    public HelloTVXlet(){

    }


    public void initXlet(XletContext context) throws XletStateChangeException {

      screen = HScreen.getDefaultHScreen();
      bgDevice = screen.getDefaultHBackgroundDevice();
      if(bgDevice.reserveDevice(this)){

          System.out.println("BackgroundImage device ahs been reserved");
      }
      else{

          System.out.println("Background image device cannot be reserved");

      } 

      bgTemplate = new HBackgroundConfigTemplate();
      bgTemplate.setPreference(HBackgroundConfigTemplate.STILL_IMAGE, HBackgroundConfigTemplate.REQUIRED);

      bgConfiguration = (HStillImageBackgroundConfiguration)bgDevice.getBestConfiguration(bgTemplate);

      try{
      bgDevice.setBackgroundConfiguration(bgConfiguration);
      }catch(java.lang.Exception e){
      System.out.println(e.toString());
      }      




      if(debug) System.out.println("Xlet initialiseren");
      this.actueleXletContext = context;

      HSceneTemplate sceneTemplate = new HSceneTemplate();

      sceneTemplate.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new HScreenDimension(1.0f, 1.0f), HSceneTemplate.REQUIRED);

      sceneTemplate.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new HScreenPoint(0.0f,0.0f), HSceneTemplate.REQUIRED);

      scene = HSceneFactory.getInstance().getBestScene(sceneTemplate);




    }


    public void startXlet () throws XletStateChangeException {



    agrondimg.load(this);
    if(debug) System.out.println("Xlet starten");

    EventManager manager = EventManager.getInstance();
    UserEventRepository repository = new UserEventRepository("voorbeeld");

    repository.addKey( org.havi.ui.event.HRcEvent.VK_RIGHT);
    repository.addKey( org.havi.ui.event.HRcEvent.VK_LEFT);

    manager.addUserEventListener(this, repository);


    scene.validate();
    scene.setVisible(true);
    }

    public void pauseXlet() {

    }

    public void destroyXlet(boolean unconditional)throws XletStateChangeException {
     agrondimg.flush();
    }

    public void userEventReceived(org.dvb.event.UserEvent e){

        if(e.getType() == KeyEvent.KEY_PRESSED){
        System.out.println("pushed button");
        switch (e.getCode()){


            case HRcEvent.VK_RIGHT:
                System.out.println("vk right");

                if(afbeelding == "pizza1.png"){
                afbeelding = "pizza2.png";

                System.out.println("1 naar 2");

                }else

                if(afbeelding == "pizza2.png"){
                afbeelding = "pizza3.png";

                System.out.println("2 naar 3");
                }else


                if(afbeelding == "pizza3.png"){
                afbeelding = "pizza4.png";

                System.out.println("3 naar 4");
                }else


                if(afbeelding == "pizza4.png"){
                afbeelding = "pizza1.png";

                System.out.println("4 naar 1");
                };


                break;

            case HRcEvent.VK_LEFT:
                System.out.println("vk left");

                break;


        }
        }

    }


}
4

1 回答 1

0

我认为您必须创建一个新的 HBackgroundImage 对象,因为一旦实例化文件,您就无法更改该文件。尝试:

agrondimg = new HBackgroundImage(afbeelding);
agrondimg.load(this);
try{
    bgConfiguration.displayImage(agrondimg);
} catch (Exception s){
    s.printStackTrace();
}

在 userEventReceived() 方法的末尾。

于 2012-10-26T13:11:06.287 回答