2013-03-10 72 views
0

這是我第一次在此論壇中。我用java timer.My程序有問題顯示的圖像在不同time.this是我的代碼當我在定時器上調用isRunning()方法時,圖像不會顯示

//TIMER 
    //creo timer per il tempo di visualizzazione di ogni singola immagine 
    timerDisplay = new Timer(this.timeOfImage * MILLE, this); 
    timerDisplay.setRepeats(false); 
    timerDisplay.setActionCommand("blackScreen"); 

    //timer tra le immagini 
    timerBetweenImages = new Timer(this.timeBetwImage * MILLE,this); 
    timerBetweenImages.setRepeats(false); 
    timerBetweenImages.setActionCommand("image"); 
    //timer prima della scelta 
    timerBeforeChoice = new Timer(this.timeBeforeChoice * MILLE,this); 
    timerBeforeChoice.setRepeats(false); 
    timerBeforeChoice.setActionCommand("Choice"); 
    //tempo di scelta 
    timerChoice = new Timer(this.timeOfChoice * MILLE,this); 
    timerChoice.setRepeats(false); 
    timerChoice.setActionCommand("End"); 


    public void actionPerformed(ActionEvent e) { 
    // tempo tra immagini consecutive 
    if (e.getActionCommand().equals("blackScreen")) { 

     label.setIcon(null); 
     if (index != numOfImages) { 
     timerBetweenImages.start(); 



     } else { 

      label.setIcon(null); 
      timerBeforeChoice.start(); 
     } 
    //to see the images 
    } else if (index < numOfImages && e.getActionCommand().equals("image")){ 

     label.setIcon(image[index]); 

     index++; 
     //visualizza 

     timerDisplay.start(); 

     state(); 

    } else if (e.getActionCommand().equals("Choice")) { 

     contentPane.remove(label); 
     //modifico il layout per inserire tanti pannelli quanto sono le immagini 
     contentPane.setLayout(new GridLayout(1, this.numOfImages)); 
     repaint(); 
     validate(); 
     //creo tanti pannelli quante sono le immagini da visualizzare 



      //creo pannello 
      JPanel panel1 = new JPanel(); 
      panel1.setBackground(Color.BLACK); 
      panel1.setLayout(new GridLayout(2,1)); 
      //aggiungo immagine e checkbox al pannello 
      JLabel lblNewLabel_1 = new JLabel(""); 
      lblNewLabel_1.setBackground(Color.BLACK); 
      lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); 
      lblNewLabel_1.setIcon(image[i]); 
      panel1.add(lblNewLabel_1); 
      i++; 
      chckbxNewCheckBox = new JCheckBox("Immagine " + 1); 
      chckbxNewCheckBox.setBackground(Color.BLACK); 
      chckbxNewCheckBox.setForeground(Color.WHITE); 
      chckbxNewCheckBox.setHorizontalAlignment(SwingConstants.CENTER); 
      chckbxNewCheckBox.setFont(new Font("Tahoma", Font.BOLD, 14)); 
      panel1.add(chckbxNewCheckBox); 
      contentPane.add(panel1); 
     //creo pannello 
      JPanel panel2 = new JPanel(); 
      panel2.setBackground(Color.BLACK); 
      panel2.setLayout(new GridLayout(2,1)); 
      //aggiungo immagine e checkbox al pannello 
      JLabel lblNewLabel_2 = new JLabel(""); 
      lblNewLabel_2.setBackground(Color.BLACK); 
      lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER); 
      lblNewLabel_2.setIcon(image[i]); 
      panel2.add(lblNewLabel_2); 
      i++; 
      chckbxNewCheckBox2 = new JCheckBox("Immagine " + 2); 
      chckbxNewCheckBox2.setBackground(Color.BLACK); 
      chckbxNewCheckBox2.setForeground(Color.WHITE); 
      chckbxNewCheckBox2.setHorizontalAlignment(SwingConstants.CENTER); 
      chckbxNewCheckBox2.setFont(new Font("Tahoma", Font.BOLD, 14)); 
      panel2.add(chckbxNewCheckBox2); 
      //i++; 
      //aggiungo pannello al contentPane 
      contentPane.add(panel2); 
      repaint(); 
      validate(); 
      chckbxNewCheckBox2.addItemListener(this); 
     chckbxNewCheckBox.addItemListener(this); 
     state(); 

    } else if (e.getActionCommand().equals("End")) { 

     contentPane.removeAll(); 
     repaint(); 
     validate(); 
     index = this.numOfImages; 
     //System.out.println(index); 
     this.numOfImages = this.numOfImages + number; 
     //System.out.println(numOfImages); 
     contentPane.setLayout(new GridLayout(1, 1)); 
     contentPane.add(label); 
     if (numOfImages <= image.length) { 
      timerDisplay.start(); 
     } else { 
      //CHIUDI CONNESSIONE 
      Edk.INSTANCE.EE_EmoStateFree(eState); 
      Edk.INSTANCE.EE_EmoEngineEventFree(eEvent); 
      Edk.INSTANCE.EE_EngineDisconnect(); 
      dispose(); 
     } 

    } 

然後,我有這樣的方法:

public void state(){ 
    while(timerDisplay.isRunning()){ 

    state = Edk.INSTANCE.EE_EngineGetNextEvent(eEvent); 
    // New event needs to be handled 

    if (state == EdkErrorCode.EDK_OK.ToInt()) { 

     int eventType = Edk.INSTANCE.EE_EmoEngineEventGetType(eEvent); 
     Edk.INSTANCE.EE_EmoEngineEventGetUserId(eEvent, userID); 

     // Log the EmoState if it has been updated 
     if (eventType == Edk.EE_Event_t.EE_EmoStateUpdated.ToInt()) { 

      Edk.INSTANCE.EE_EmoEngineEventGetEmoState(eEvent, eState); 
      //tempo 
      float timestamp = EmoState.INSTANCE.ES_GetTimeFromStart(eState); 
      //user ID 
      int user = userID.getValue(); 

      //valori emotional 
      float shortExcitement = EmoState.INSTANCE.ES_AffectivGetExcitementShortTermScore(eState); 
      float longExcitement = EmoState.INSTANCE.ES_AffectivGetExcitementLongTermScore(eState); 
      float egagement = EmoState.INSTANCE.ES_AffectivGetEngagementBoredomScore(eState); 
      float frustation = EmoState.INSTANCE.ES_AffectivGetFrustrationScore(eState); 
      float meditation = EmoState.INSTANCE.ES_AffectivGetMeditationScore(eState); 
      if(option == 1){ 
       output.println(timestamp + ";" + user + ";" + shortExcitement + ";" + 
         longExcitement + ";" + egagement + ";" + frustation + ";" + meditation + ";"); 
      } else if (option == 2){ 
       output.println(timestamp + ";" + user + ";" + shortExcitement + ";" + 
         longExcitement + ";" + egagement + ";" + frustation + ";" + meditation + ";"); 
      } 


     } 

    } 
    else if (state != EdkErrorCode.EDK_NO_EVENT.ToInt()) { 
     output.println("Internal error in Emotiv Engine!"); 
     //messaggio di errore 
     String message2 = "Internal error in Emotiv Engine!"; 
      JOptionPane.showMessageDialog(new JFrame(), message2, "Dialog", 
      JOptionPane.ERROR_MESSAGE); 
    } 
    } 
} 

的問題是在我的state()方法,當我請致電timerDisplay.isRunning()該程序無法顯示圖像。事實上,我希望state()運行,直到timerDisplay完成。對不起,我的英語不好...... :)

+0

這是因爲你的阻塞事件調度線程。狀態方法的目的是什麼? – MadProgrammer 2013-03-10 21:35:04

+0

它在.csv文件中標記Affectiv值。它的工作原理(在csv文件中寫入值),但只有一次,如果我刪除while(timerDisplay.isRunning())。但是,如果我使用我發佈的代碼,應用程序無法顯示任何圖像。如何解決此問題問題?我的應用需要更改哪些內容? – user2154739 2013-03-10 21:56:24

回答

2

你的state方法似乎是阻塞事件調度線程。這可以防止處理任何事件(包括繪畫和計時器事件),使其看起來好像應用程序已停止。

這是因爲Swing是單線程環境。這意味着所有與UI的交互必須在事件分派線程的上下文中執行。這也意味着你應該在另一個線程上執行所有阻塞或耗時的處理。

看看SwingWorker。這將允許您在後臺線程中處理您的「狀態」事件,但也有易於使用的方法將任何更新重新同步到UI/EDT。

也有一個讀通過Concurrency in Swing瞭解更多信息

+0

+1我真的很喜歡你的答案。 – 2013-03-10 22:16:41

+0

@ Eng.Fouad當有權利;) – MadProgrammer 2013-03-10 22:22:06

+0

好的,謝謝!我試圖以這種方式修改我的應用程序。我明天嘗試,然後我會寫在這裏,如果我有一些問題:)非常感謝 – user2154739 2013-03-10 23:40:00

相關問題