Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    The default is dispose on close. All of the frames have a default of dispose on close. Because it is never known which frame the player will loose the quiz at, I made the program so that there is a questionNumber counter. I need to dispose whatever question they lost at which is why I do
    Java Code:
    mario1.Theframe[mario1.questionNumber].dispose();
    Here is the code for the questions and then the stuff I had before

    Java Code:
      public  void theQuestions( int questionNumber)
        {
         
            if(questionNumber==1){
            sequencer.stop();
            mario1.Theframe[1] = new QuestionsFrame(null,"smwbullet.jpg","What is the name of this enemy","Banzai Bill","Blurp","Bullet Bill","Goomba",1);
            mario1.Theframe[1].addWindowListener(new mario1.QuestionsFrameListener(2));
        //  mario1.Theframe[1].setBackground(Color.yellow)
            mario1.Theframe[1].QuestionPanel.setBackground(Color.yellow);
            mario1.Theframe[1].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           // mario1.Theframe[1].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mario1.Theframe[1].setVisible(true);
           // .aSequencer1.start();
            }
            
            if(questionNumber==2){
          //  mario1.Theframe[1].sequencer.stop();
            mario1.Theframe[2] = new QuestionsFrame(null,"","this is the second question","Banzai Bill","Blurp","Bullet Bill","Goomba",1);
            mario1.Theframe[2].addWindowListener(new mario1.QuestionsFrameListener(3));
            //mario1.Theframe[2].setBackground(Color.yellow);
            mario1.Theframe[2].QuestionPanel.setBackground(Color.green);
            mario1.Theframe[2].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           // mario1.Theframe[2].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mario1.Theframe[2].setVisible(true);
            }
            
            if(questionNumber==3){
            mario1.Theframe[3] = new QuestionsFrame(null,"","this is the third question"," "," "," "," ",4);
            mario1.Theframe[3].addWindowListener(new mario1.QuestionsFrameListener(4));
            mario1.Theframe[3].setBackground(Color.yellow);
            mario1.Theframe[3].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           // mario1.Theframe[3].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mario1.Theframe[3].setVisible(true);
            }
            
            if(questionNumber==4){
            mario1.Theframe[4] = new QuestionsFrame(null,"","this is the fourth question"," "," ","","",2);
            mario1.Theframe[4].addWindowListener(new mario1.QuestionsFrameListener(5));
            mario1.Theframe[4].setBackground(Color.yellow);
            mario1.Theframe[4].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           // mario1.Theframe[4].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mario1.Theframe[4].setVisible(true);
            }
        }
        
        public static  int getAnswer(int answer){
            return answer;
        }//end of getAnswer method
        
       
      
        
        class AnswerAction extends AbstractAction {
    
        private boolean isCorrect;   
        
        public AnswerAction(String name, boolean isCorrect, int questionNumber) {
            super(name);
            this.isCorrect = isCorrect;
            mario1.questionNumber = questionNumber;
            
        } //end of AnswerAction
    
        public void actionPerformed(ActionEvent e) {
             
             if (isCorrect) {
                  introChime();
                  try{
                    Thread.sleep(1000);
                }
                catch(InterruptedException x){
                    
                }
                  mario1.Theframe[mario1.questionNumber].dispose();
                  
                  theQuestions(questionNumber+=1);
                   System.out.println("Correct");
                   //questionNumber++;
                          
             }   
          
             else if (lives >1){
                  lives-=1;
                  mario1.Theframe[mario1.questionNumber].setVisible(false);
                  mario1.lifeLost[0] = new QuestionsFrame("LIVES X " + lives,"marioisdead.jpg");
                  mario1.lifeLost[0].addWindowListener(new mario1.QuestionsFrameListener(questionNumber));
                  mario1.lifeLost[0].setBackground(Color.yellow);
                //  mario1.lifeLost[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                //  mario1.lifeLost[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  mario1.lifeLost[0].setVisible(true);
                      
                  deathChime();
             }    
                   
                             
             else{
                mario1.Theframe[mario1.questionNumber].addWindowListener(new mario1.QuestionsFrameListener(questionNumber));
               // mario1.Theframe[mario1.questionNumber].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
                //mario1.Theframe[mario1.questionNumber].setVisible(false);
              [COLOR="Red"]  mario1.Theframe[mario1.questionNumber].dispose();[/COLOR]
                mario1.gameOver[0] = new QuestionsFrame("game over","exit","gameover.jpg");
                mario1.gameOver[0].addWindowListener(new mario1.QuestionsFrameListener(mario1.questionNumber));
                mario1.gameOver[0].setBackground(Color.yellow);
               // mario1.gameOver[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mario1.gameOver[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mario1.gameOver[0].setVisible(true);
                gameOverSequencer();
               }  
                  
                // QuestionsFrame.incorrectAnswer();     // .. - you could pass the answer string if required
              // end of else
                 
             // or possibly:         questionManager.handleAnswer(isCorrect);
        } //end of ActionPerformed
        //public  int questionNumber;
    }  //end of AnswerAction class

  2. #22
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    I have no idea what I did but it works now. In that last frame the gameOver one when the button is pressed, instead of disposing the gameOver frame which revealed the one I was having a problem with, I just used System.exit().

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That's not the best way to exit from a GUI. If you'd like, if you zip your code and post it here, I might be able to take a look at it.

  4. #24
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Well, my project is only for personal use, so I am not going to bother posting. I would however like to know why System.exit() is bad to use.

  5. #25
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Sorry to bother again, but I would really like to figure this out without having to resort to using system.exit. Let me walk you through the code I am having problems with. The player starts out with 3 lives, that is not shown here. if they answer correctly the frame disposes and the next question appears, that all works fine. Now if they answer a question wrong we go to the else if. Lives then becomes 2 we set the question Visibility to false and display the lifeLost frame and play deathChime(). This process occurs if lives are greater than 1. So when the player answers a question wrong for the third time, the code goes to the else statement. Here is where the problem is, when I attempt to dispose the frame (It is Never needed again, the player has lost, it is gameOver), it does not dispose. The gameOver frame comes up, but the question frame is still there. I have attempted to dispose of it in two ways, the first is in the else statement itself. The second was with a SwingWorker Object (gameOver()) and neither method worked.




    Java Code:
    class AnswerAction extends AbstractAction {
    
        private boolean isCorrect;   
        
        public AnswerAction(String name, boolean isCorrect, int questionNumber) {
            super(name);
            this.isCorrect = isCorrect;
            mario1.questionNumber = questionNumber;
            
        } //end of AnswerAction
    
        public void actionPerformed(ActionEvent e) {
             
             if (isCorrect) {
                  introChime();
                  try{
                    Thread.sleep(1000);
                }
                catch(InterruptedException x){
                    
                }
                  mario1.Theframe[mario1.questionNumber].dispose();
                  
                  theQuestions(questionNumber+=1);
                   System.out.println("Correct");
                   //questionNumber++;
                          
             }   
          
             else if (lives >1){
                  lives-=1;
                  mario1.Theframe[mario1.questionNumber].setVisible(false);
                  mario1.lifeLost[0] = new QuestionsFrame("LIVES X " + lives,"marioisdead.jpg");
                  mario1.lifeLost[0].addWindowListener(new mario1.QuestionsFrameListener(questionNumber));
                  mario1.lifeLost[0].setBackground(Color.yellow);
                //  mario1.lifeLost[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                //  mario1.lifeLost[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  mario1.lifeLost[0].setVisible(true);
                      
                  deathChime();
             }    
                   
                             
             else{
                mario1.Theframe[mario1.questionNumber].dispose(); 
                mario1.Theframe[mario1.questionNumber].addWindowListener(new mario1.QuestionsFrameListener(questionNumber));
               // mario1.Theframe[mario1.questionNumber].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
               // mario1.Theframe[mario1.questionNumber].setVisible(false);
                
                mario1.gameOver[0] = new QuestionsFrame("game over","exit","gameover.jpg");
                mario1.gameOver[0].addWindowListener(new mario1.QuestionsFrameListener(mario1.questionNumber));
                mario1.gameOver[0].setBackground(Color.yellow);
               // mario1.gameOver[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mario1.gameOver[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                gameOver();
                gameOverSequencer();
               }  
                  
                // QuestionsFrame.incorrectAnswer();     // .. - you could pass the answer string if required
              // end of else
                 
             // or possibly:         questionManager.handleAnswer(isCorrect);
        } //end of ActionPerformed
        //public  int questionNumber;
    }  //end of AnswerAction class
    deathChime
    Java Code:
    public void deathChime(){
             new SwingWorker<Void, Void>(){
             @Override        
             protected Void doInBackground() throws Exception{
                AudioInputStream stream =
                      AudioSystem.getAudioInputStream(
                        new File("C:\\users\\Matt\\Documents\\mariodie.wav"));
     
                      AudioFormat format = stream.getFormat();
                      DataLine.Info info =
                        new DataLine.Info(Clip.class,
                            stream.getFormat());
                      Clip clip = (Clip) AudioSystem.getLine(info);
     
                        clip.open(stream);
                        clip.start();
                   return null;
             }    
           
         
          @Override
          protected void done() {
            // call these guys here, to be excecuted when the do in 
            // background has completed
            try{
              Thread.sleep(2500);  
            }
            catch(InterruptedException d){
                
            }
            mario1.lifeLost[0].setVisible(false);
            mario1.Theframe[mario1.questionNumber].setVisible(true);
          }
        }.execute();
        
              
                
         }
    gameOver()
    Java Code:
    public void gameOver(){
             new SwingWorker<Void, Void>(){
             @Override        
             protected Void doInBackground() throws Exception{
               
                   return null;
             }    
           
         
          @Override
          protected void done() {
            // call these guys here, to be excecuted when the do in 
            // background has completed
            try{
              Thread.sleep(5);  
            }
            catch(InterruptedException d){
                
            }
             // mario1.Theframe[mario1.questionNumber].dispose();
              mario1.gameOver[0].setVisible(true);
          }
        }.execute();
        
              
                
         }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Frame connectivity
    By Shivraj in forum NetBeans
    Replies: 1
    Last Post: 03-17-2009, 01:30 PM
  2. GUI new frame
    By billbo123 in forum New To Java
    Replies: 15
    Last Post: 03-02-2009, 04:24 AM
  3. New button in new frame
    By billbo123 in forum New To Java
    Replies: 0
    Last Post: 03-01-2009, 10:32 PM
  4. frame
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-07-2009, 10:26 AM
  5. Frame to other Frame
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •