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

    Default Help frame is not displaying.

    Ok I don't know if I am missing something obvious here but I think this code should work the way I want and it does not. You can see in the code that I set Theframe to false and then I create a new Object (lifeLost) and attempt to set that to display true. However lifeLost does not display and Theframe remains on the screen. What I want is for Theframe to remain hidden (setVisible to false), and lifeLost setVisible to true, until function deathChime() Completes. See I am creating a quiz game and when a question is answered wrong I want to hide the question frame, bring up a life lost frame, play a wav file (deathChime()), then hide the life lost and bring up the same question again.

    Java Code:
    else {
                  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();
                  
                  
                     
                  mario1.lifeLost[0].setVisible(false);
                  mario1.Theframe[mario1.questionNumber].setVisible(true);
                
    
             } // end of else
    Here is the code for the deathChime()
    Java Code:
    public void deathChime(){
             try{
               
                 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();
                      //  Thread.sleep(50);
                Thread.sleep(2400);
             }
             catch(InterruptedException a){
             }catch(Exception w){
              w.printStackTrace();  
             }

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

    Default

    Your problem appears to be a threading issue as you are freezing the thread which displays the GUI, in effect completely freezing your application. The solution will depend on if this is this a Swing application (JFrame) or AWT (Frame)?

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

    Default

    I am using Swing. The reason I need to use Thread.sleep is because when I get lifeLost to dsiplay, I need it to stay on the screen for the duration of the sound clip played by deathChime().

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

    Default

    The reason I need to use Thread.sleep is because when I get lifeLost to dsiplay, I need it to stay on the screen for the duration of the sound clip played by deathChime().
    Add to gammaman's Reputation
    Any time you call Thread.sleep or call a long time-consuming process on the EDT, the event dispatch thread, which is the main thread that Swing uses to paint the GUI and interact with the user, again you put your entire application to sleep. Changes in components will not occur, buttons will freeze, the application will essentially be put to sleep.

    Trust me, that last thing that you "need" to do is to call Thread.sleep here.

    You may wish to use a SwingWorker here so that background processes can be done on a background thread. I'll get a link to the tutorial to you in a second...

    Here it is: Concurrency in Swing
    Please read this as it will solve your problem (if you are using Java 1.6 that is).

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

    Default

    By the way, I am not using threads to display the JFrame, I just need to use Thread.sleep().

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

    Default

    Thanks, you can ignore my last post.

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

    Default

    For instance:
    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
            mario1.lifeLost[0].setVisible(false);
            mario1.Theframe[mario1.questionNumber].setVisible(true);
          }
        }.execute();
    
      }
    Also, rather than showing and hiding windows, would your app run smoother if you swapped JPanels via a CardLayout?

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

    Default

    I don't know, let me give you the full source code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.sound.midi.*;
    import java.io.*;
    import java.net.*;
    import java.io.File;
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.DataLine;
    
    public  class mario1{
           
        public static void main(String[]args){
              new mario1().run();
        } // end of main
        
        public  void run(){ 
            
            try{
            Sequence sequence = MidiSystem.getSequence(new File("C:\\users\\Matt\\Documents\\mario09.mid"));
            Sequence sequence1 = MidiSystem.getSequence(new File("C:\\users\\Matt\\Documents\\M3gover.mid"));
            // Create a sequencer for the sequence
            Sequencer sequencer = MidiSystem.getSequencer();
            Sequencer sequencer1 = MidiSystem.getSequencer();
            sequencer.open();
            sequencer1.open();
            sequencer.setSequence(sequence);
            sequencer1.setSequence(sequence1);     
            // Start playing the first sequence
              Theframe = new QuestionsFrame[9];
            
              Theframe[0] = new QuestionsFrame(sequencer,"start","smwtitle.jpg",1);
              Theframe[0].addWindowListener(new QuestionsFrameListener(1));
              Theframe[0].setBackground(Color.blue);
              Theframe[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              Theframe[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Theframe[0].setVisible(true);
              sequencer.start();
             
            } // end of try                   
                    
           catch (IOException e) {
        }  catch (MidiUnavailableException e) {
        }  catch (InvalidMidiDataException e) {
        } 
            
             
           
              
        
           
                  
        }
        
       
      
            
        // WindowListener to tell main application when a frame closes
       static class QuestionsFrameListener extends WindowAdapter{
             int questionNumber;
             
          
        // constructor taking the question number
        QuestionsFrameListener(int questionNumber) {
            this.questionNumber = questionNumber;
        }
    
        // when frame closes, notify the main application method
        
            @Override
        public void windowClosed(WindowEvent e) {
          QuestionFrameClosed(questionNumber);
        }
        
         
        
          //end of QuestionsFrameListener 
                      
           
          public  void QuestionFrameClosed(int questionNumber) {
            
        // display next frame if not finished yet
            if (questionNumber <= mario1.Theframe.length) {
                mario1.Theframe[questionNumber].setVisible(true);
            //Theframe[questionNumber].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               
            }
            else {
            // no more questions
            }
            
        }// end of questionFrameClosedMethod  
         }
            public static  QuestionsFrame Theframe[] = new QuestionsFrame[9];
            public static  QuestionsFrame gameOver[] = new QuestionsFrame[1];
            public static  QuestionsFrame lifeLost[] = new QuestionsFrame[1];
            public  String theSequence;
         
                    
            public static int questionNumber;
      
        } // end of mario1
        
     
    class QuestionsFrame extends JFrame
    {
        public static int lives = 3;    
        public QuestionsFrame(Sequencer theSequencer,String anIcon,String q,String b1,String b2,String b3,String b4,int a)
        {
            
           //this.frame = frame;
          // this.counter = counter;
           answer = a;
           theicon = anIcon;
          // mario1.aSequencer1 = theSequencer;
           
           //Theframe = Theframe;
          //  Question = q;
            Question = q;
            B1 = b1;
            B2 = b2;
            B3 = b3;
            B4 = b4;
            
             //size the frame
            setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
            cp = getContentPane();
            cp.setBackground(Color.white);
              
            //create Icon object and set its location
           ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" + theicon);
           // ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
            JButton button1 = new JButton(new AnswerAction(B1,answer ==1,questionNumber));  //create button
            JButton button2 = new JButton(new AnswerAction(B2,answer ==2,questionNumber));
            JButton button3 = new JButton(new AnswerAction(B3,answer==3,questionNumber));
            JButton button4 = new JButton(new AnswerAction(B4,answer==4,questionNumber));
              
            ImagePanel = new JPanel();
            ImagePanel.setBackground(Color.white);
            
            QuestionPanel = new JPanel();
          //  QuestionPanel.setBackground(Color.BLUE);
            
            add(ImagePanel,BorderLayout.NORTH);
            
            newLabel  =  new JLabel();
            newLabel1 = new  JLabel();
            //add the icon to the label
           // newLabel.setLocation(29,40);
           // newLabel1.setLocation(80,80);
            newLabel.setPreferredSize((new Dimension(600,450)));
            newLabel.setIcon(icon);
           // newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
           // newLabel1.setIcon(icon1);
                
            ImagePanel.add(newLabel);
            ImagePanel.add(newLabel1);
                           
            QuestionPanel.add(button1);  // adding button to frame
            QuestionPanel.add(button2);
            QuestionPanel.add(button3);
            QuestionPanel.add(button4);
            
            add(QuestionPanel,BorderLayout.SOUTH);
            
            QuestionLabel = new JLabel(Question,JLabel.CENTER); 
            add(QuestionLabel,BorderLayout.CENTER);
        }//end of QuestionFrame Contstructor
        
        public QuestionsFrame(Sequencer theSequencer,String b1,String anIcon,int a)
        {
          // this.frame = frame;
        //   this.counter = counter;
           theicon = anIcon;
           this.sequencer = theSequencer;
           
           
           answer = a;        
           B1 = b1;
                   
           //size the frame
           setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
           cp = getContentPane();
           cp.setBackground(Color.white);
           
         
            //create Icon object and set its location
           ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" +theicon);
           // ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
           JButton button = new JButton(new AnswerAction(B1,answer==1,questionNumber));  //create button
          // JButton button1 = new JButton(new ImageIcon("C:\\users\\Matt\\Documents\\star.jpg")); 
         
           
           
            ImagePanel = new JPanel();
            ImagePanel.setBackground(Color.white);
            
            QuestionPanel = new JPanel();
          //  QuestionPanel.setBackground(Color.BLUE);
            
            add(ImagePanel,BorderLayout.NORTH);
            
            newLabel  =  new JLabel();
            newLabel1 = new  JLabel();
            //add the icon to the label
           // newLabel.setLocation(29,40);
           // newLabel1.setLocation(80,80);
            newLabel.setPreferredSize((new Dimension(170,500)));
            newLabel.setIcon(icon);
           // newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
           // newLabel1.setIcon(icon1);
                
            ImagePanel.add(newLabel);
            ImagePanel.add(newLabel1);
                           
            QuestionPanel.add(button);  // adding button to frame
           // QuestionPanel.add(button1);   
            
            add(QuestionPanel,BorderLayout.SOUTH);
            
            QuestionLabel = new JLabel(Question,JLabel.CENTER); 
            add(QuestionLabel,BorderLayout.CENTER);
        }//end of QuestionFrame Contstructor
        
        
        
        
         public QuestionsFrame(Sequencer theSequencer, String q, String b1,String anIcon)
        {
          // this.frame = frame;
        //   this.counter = counter;
           theicon = anIcon;
           this.sequencer1 = theSequencer;
           Question = q;
           
                   
           B1 = b1;
                   
           //size the frame
           setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
           cp = getContentPane();
           cp.setBackground(Color.white);
           
         
            //create Icon object and set its location
           ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" +theicon);
           // ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
           JButton button = new JButton("Exit");  //create button
          // JButton button1 = new JButton(new ImageIcon("C:\\users\\Matt\\Documents\\star.jpg")); 
         
           
           
            ImagePanel = new JPanel();
            ImagePanel.setBackground(Color.white);
            
            QuestionPanel = new JPanel();
          //  QuestionPanel.setBackground(Color.BLUE);
            
            add(ImagePanel,BorderLayout.NORTH);
            
            newLabel  =  new JLabel();
            newLabel1 = new  JLabel();
            //add the icon to the label
           // newLabel.setLocation(29,40);
           // newLabel1.setLocation(80,80);
            newLabel.setPreferredSize((new Dimension(650,500)));
            newLabel.setIcon(icon);
           // newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
           // newLabel1.setIcon(icon1);
                
            ImagePanel.add(newLabel);
            ImagePanel.add(newLabel1);
                           
            QuestionPanel.add(button);  // adding button to frame
           // QuestionPanel.add(button1);   
            
            add(QuestionPanel,BorderLayout.SOUTH);
            
            QuestionLabel = new JLabel(Question,JLabel.CENTER); 
            add(QuestionLabel,BorderLayout.CENTER);
        }//end of QuestionFrame Contstructor
         
        public QuestionsFrame(String q, String anIcon){
             // this.frame = frame;
        //   this.counter = counter;
           theicon = anIcon;
          
           Question = q;
           
                   
         
                   
           //size the frame
           setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
           cp = getContentPane();
           cp.setBackground(Color.white);
           
         
            //create Icon object and set its location
           ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" +theicon);
           // ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
          
         
           
           
            ImagePanel = new JPanel();
            ImagePanel.setBackground(Color.black);
            
            QuestionPanel = new JPanel();
          //  QuestionPanel.setBackground(Color.BLUE);
            
            add(ImagePanel,BorderLayout.NORTH);
            
            newLabel  =  new JLabel();
            newLabel1 = new  JLabel();
            //add the icon to the label
           // newLabel.setLocation(29,40);
           // newLabel1.setLocation(80,80);
            newLabel.setPreferredSize((new Dimension(300,500)));
            newLabel.setIcon(icon);
           // newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
           // newLabel1.setIcon(icon1);
                
            ImagePanel.add(newLabel);
            ImagePanel.add(newLabel1);
                           
          
            
            add(QuestionPanel,BorderLayout.SOUTH);
            
            QuestionLabel = new JLabel(Question,JLabel.CENTER); 
            add(QuestionLabel,BorderLayout.CENTER);
            
        } 
         
         
        public void introChime(){
            try{   
              AudioInputStream stream =
                      AudioSystem.getAudioInputStream(
                        new File("C:\\users\\Matt\\Documents\\smw_coin.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();
                      //  Thread.sleep(50);
            }          
             catch (Exception e) {
            e.printStackTrace();
        }            
        }
        
         public void deathChime(){
             try{
               
                 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();
                  //  Thread.sleep(50);
             }
            
             catch(Exception w){
              w.printStackTrace();  
             }
        }
         
        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].setVisible(false);
                  
                  theQuestions(questionNumber+=1);
                   System.out.println("Correct");
                   //questionNumber++;
                          
             }   
          
             else {
                  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);
                  
                  try{
                      mario1.lifeLost[0].setVisible(true);
                      
                  Thread.sleep(2400);
                  //mario1.Theframe[;mario1.questionNumber].setVisible(false);
                  deathChime();
                  }
                  catch(InterruptedException k){
                      
                  }
                  
                     
                  mario1.lifeLost[0].setVisible(false);
                  //mario1.Theframe[mario1.questionNumber].setVisible(true);
                
               if(lives==0){
               // sequencer1.start();   
                mario1.Theframe[questionNumber].setVisible(false);   
                mario1.gameOver[0] = new QuestionsFrame(sequencer1,"game over","exit","gameover.jpg");
                mario1.gameOver[0].addWindowListener(new mario1.QuestionsFrameListener(5));
                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);   
               }  
                  
                // 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
    
        
        
        
        //instance variables
        public JPanel QuestionPanel;
        public JPanel ImagePanel;
        public JLabel QuestionLabel;
        public JLabel newLabel;
        public JLabel label;
        public JLabel newLabel1;
        public String theicon;
        public String buttonIcon;
       // public static  QuestionsFrame Theframe[] = new QuestionsFrame[9];
      //  public int theFrame;
        public  String theSequence;
        public static int questionNumber;
       // public  QuestionsFrame frame;
        public int   counter;
        public int answer;
        public String Question;
        public  String B1,B2,B3,B4;
        public   Sequence sequence;
        public   Sequence sequence1;
        public   Sequencer  sequencer1;
       public    Sequencer sequencer;
      //  public   Sequencer  aSequencer1;
        public Container cp;
        public static final int DEFAULT_WIDTH = 1000;
        public static final int DEFAULT_HEIGHT = 700;
       } //end of QuestionFrame class

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

    Default

    Java Code:
        catch (IOException e) {
        }
        catch (MidiUnavailableException e) {
        }
        catch (InvalidMidiDataException e) {
        }
    Sorry, but this code makes me cry a little bit. Please don't do this. You should never throw out exceptions as you do here as if your program has an exception, you'll never know it. At least print a stack trace.

    And yes, your program would benefit greatly by getting rid of the window hiding and showing and instead swapping JPanels in a single JFrame using CardLayout.
    Last edited by Fubarable; 07-19-2009 at 02:53 PM.

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

    Default

    why what is wrong with it? By the way, I think SwingWorker requires the class to be abstract, which means I would need to rework all of my code.

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

    Default

    Quote Originally Posted by gammaman View Post
    why what is wrong with it?
    Please see changes to my post above.

    By the way, I think SwingWorker requires the class to be abstract, which means I would need to rework all of my code.
    Not so. This only means that you have declared and initialized a SwingWorker object without implementing its necessary methods. It needs a doInBackground declared in the least.

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

    Default

    Thanks, would you mind giving me an idea how I would use cardLayout? You do not have to use my Code, I just want a small idea.

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

    Default

    Quote Originally Posted by gammaman View Post
    Thanks, would you mind giving me an idea how I would use cardLayout? You do not have to use my Code, I just want a small idea.
    I can do better than that: How to use CardLayout

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

    Default

    I am sorry, I am really new to Java, especially swing, how do I use the SwingWorker code you gave me? I get an error

    <anonymous QuestionsFrame$1> is not abstract and does not override abstract method doInBackground().

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

    Default

    I really can't say without spending quite a bit of time with your code, time that I'm afraid I don't have as I have to go to work right now (I'm on call). I recommend that you go through the SwingWorker tutorial whose link I have given you (it was called "Concurrency in Swing"), which should give you a much deeper insight into using SwingWorker than I can give you. Best of luck!

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

    Default

    Ok thanks.

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

    Default

    I have one more problem. In the else statement at the bottom of this code, I am trying to dispose a frame but it does not dispose, it just stays on the screen and then creates the next frame, how can I get the Theframe to dispose?

    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.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

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

    Default

    I would recommend against JFrames coming and going like this. If it were my app, I'd use a CardLayout or other technique to swap views without swapping windows.

    That being said, what is the defaultCloseOperation set for this JFrame?

    Also, you do know of course that the first line here will be ignored:
    Java Code:
    mario1.lifeLost[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mario1.lifeLost[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    since when the value of the defaultCloseOperation is set to EXIT_ON_CLOSE, it replaces DISPOSE_ON_CLOSE. Choose close option here, stick with it, and get rid of the other.

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

    Default

    Ok thanks but that was not really my question. I was asking about the else clause, the first line in the else clause is not working. I know this because the when I dispose mario1.gameOver[] via a button, the other frame, Theframe[] is still there.



    Java Code:
     else{
               
                mario1.Theframe[mario1.questionNumber].dispose();
                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();
               }

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

    Default

    Quote Originally Posted by gammaman View Post
    Ok thanks but that was not really my question.
    This question of mine addressed your question:
    Quote Originally Posted by Fubarable
    That being said, what is the defaultCloseOperation set for this JFrame?
    meaning the JFrame that is not being disposed properly. Your answer to this may help us figure out what's not working.

    You may need to simplify your code in order to isolate the problem.

Page 1 of 2 12 LastLast

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
  •