2012-10-26 6 views
0

배경 이미지를 보여주는 자바 코드가 있습니다. (pizza1.png) 화살표 키 (사례 HRcEvent.VK_RIGHT :)를 누르면 변경할 수 있습니다. 그러나 이미지를 다시 그릴 것이므로 입력 할 내용을 알지 못해 이미지 경로의 문자열을 변경하지만 그럴 수는 없습니다. 그것을 다시 그리지 마라. 누군가 나를 도울 수 있니?Java HbackgroundImage 다시 칠하기

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; 


     } 
     } 

    } 


} 

답변

0

새로운 HBackgroundImage 객체는 인스턴스화 된 이후에는 변경할 수 없으므로 작성해야한다고 생각합니다. 시도해보십시오.

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

userEventReceived() 메소드 끝 부분.

+0

오류가 발생합니다. bgConfiguration.displayImage (agrondimg); 시도 캐치가 필요합니다 –

+0

하지만 try catch를 추가 한 후에 작동합니다. 대단히 감사합니다! –

+0

Ok, 로깅을 추가하기 전에 내 try catch, printStackTrace()가 유용 할 수 있습니다. – Link19

관련 문제