JApplet display

I am not sure what is wrong with my program. I think it might be a problem with threads, even though I am not creating Threads. What I have is three different classes to implement the game Minesweeper. One is an extended JButton class MineButton that is used for the 81 ( 9 x 9 ) buttons. The second is a an extended Container class MineField that contains the MineButtons. Then I have the class Minesweeper that contains the MineField object. I have a lot of it working. Now what I am trying to do is to have the Minewseeper class accesss static information from the MineButton class. Here, I have no idea how I should do it. After (I believe) the field and buttons are initialized and ready for display. I send the Minesweeper object into an infinite loop to constantly update the information from the MineButton classes. When I do this the Applet pops up but doesn't display its contents. And the showStatus displays zero. Which is what it should be. Thanks for any help.

Here is the Minesweeper class

    public class Minesweeper extends JApplet
    {
        MineField field;
        public void init()
        {
            field = new MineField(9,9);
            getContentPane().add(field);
            setSize(field.getSize());
        }

        public void start()
        {  
            // trying to fix my problem.  Wondering if not ready for display
            field.setNumbers();
            while(!field.initialized());


            while(true)
            {
                showStatus(MineButton.flagCount + "");
            }
        }
    }

Answers


The least you could do would be to add some delay between refreshing the status. The way it is now you are taking the whole CPU and, besides of busy waiting being a very bad practice, the swing thread is unable to jump in and draw your stuff.

while(true) {
    showStatus(MineButton.flagCount + "");

     try {
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        Logger.getLogger(Str.class.getName()).log(Level.SEVERE, null, ex);
    }
}

A better solution would be to set up a timer, so instead of the while loop:

int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        showStatus(MineButton.flagCount + "");   // need to make sure that you can call showStatus
    }
};

new javax.swing.Timer(delay, taskPerformer).start();

Need Your Help

Switch-Case block not executing as intended?

c# switch-statement

In the following code snippet, what I intend is on every timer tick event, it should fall in 'next' case of the switch statement.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.