Trouble using Thread.sleep()......

Discussion in 'Java' started by Lee Garrington, Oct 1, 2003.

  1. Hello,

    Basically this is my problem. I am programming minesweeper in java. I have
    a panel in the background on which I draw all the mines/numbers etc. and
    then I have an array of buttons that sit on top of them.

    I am developing a solver and need a pause between each move the algorithm
    makes. I know from my past experience that you have to use something along
    the lines of :-

    Graphics g = board.getGraphics();
    board.paintComponent(g);
    g.dispose();

    to get the board to update after each successive move but I seem to be
    having problems getting it working properly.

    - If I use the above to repaint the background then all the buttons
    disappear.
    - If I use the above to repaint all the buttons it doesnt actually repaint
    them, it just goes grey.
    -If I try and repaint both, wherever moves are made by the computer you can
    see the relevant square in the background but none of the other buttons are
    drawn and its just grey instead.

    After completeing the algorithm it all redraws as normal.

    I just want everything to refresh as normal when each move by the computer
    is made, can anyone help me out?

    Thx in advance
     
    Lee Garrington, Oct 1, 2003
    #1
    1. Advertising

  2. In article <3f7b24b7$0$11001$>,
    "Lee Garrington" <> wrote:

    >:Hello,
    >:
    >:Basically this is my problem. I am programming minesweeper in java. I have
    >:a panel in the background on which I draw all the mines/numbers etc. and
    >:then I have an array of buttons that sit on top of them.
    >:
    >:I am developing a solver and need a pause between each move the algorithm
    >:makes. I know from my past experience that you have to use something along
    >:the lines of :-
    >:
    >:Graphics g = board.getGraphics();
    >: board.paintComponent(g);
    >: g.dispose();
    >:
    >:to get the board to update after each successive move but I seem to be
    >:having problems getting it working properly.
    >:
    >:- If I use the above to repaint the background then all the buttons
    >:disappear.
    >:- If I use the above to repaint all the buttons it doesnt actually repaint
    >:them, it just goes grey.
    >:-If I try and repaint both, wherever moves are made by the computer you can
    >:see the relevant square in the background but none of the other buttons are
    >:drawn and its just grey instead.
    >:
    >:After completeing the algorithm it all redraws as normal.
    >:
    >:I just want everything to refresh as normal when each move by the computer
    >:is made, can anyone help me out?
    >:
    >:Thx in advance
    >:


    You should never call paintComponent() directly. You should instead
    always call repaint(), which posts an event that calls paint(), which in
    turn calls paintComponent() (among others). This is all Swing, of
    course. But perhaps this is behind the problem, since you're
    interfering with the normal behavior of the Event Dispatching mechanism.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Oct 1, 2003
    #2
    1. Advertising

  3. Yeah I know I shouldnt directly call it but in the past that is the only way
    I have ever managed to prevent the gui from freezing.

    repaint() just doesnt work. It doesnt repaint anything unfortunately and I
    dont exactly know why.

    Maybe there is another solution to this type of problem?

    "Steve W. Jackson" <> wrote in message
    news:-berlin.de...
    > In article <3f7b24b7$0$11001$>,
    > "Lee Garrington" <> wrote:
    >
    > >:Hello,
    > >:
    > >:Basically this is my problem. I am programming minesweeper in java. I

    have
    > >:a panel in the background on which I draw all the mines/numbers etc. and
    > >:then I have an array of buttons that sit on top of them.
    > >:
    > >:I am developing a solver and need a pause between each move the

    algorithm
    > >:makes. I know from my past experience that you have to use something

    along
    > >:the lines of :-
    > >:
    > >:Graphics g = board.getGraphics();
    > >: board.paintComponent(g);
    > >: g.dispose();
    > >:
    > >:to get the board to update after each successive move but I seem to be
    > >:having problems getting it working properly.
    > >:
    > >:- If I use the above to repaint the background then all the buttons
    > >:disappear.
    > >:- If I use the above to repaint all the buttons it doesnt actually

    repaint
    > >:them, it just goes grey.
    > >:-If I try and repaint both, wherever moves are made by the computer you

    can
    > >:see the relevant square in the background but none of the other buttons

    are
    > >:drawn and its just grey instead.
    > >:
    > >:After completeing the algorithm it all redraws as normal.
    > >:
    > >:I just want everything to refresh as normal when each move by the

    computer
    > >:is made, can anyone help me out?
    > >:
    > >:Thx in advance
    > >:

    >
    > You should never call paintComponent() directly. You should instead
    > always call repaint(), which posts an event that calls paint(), which in
    > turn calls paintComponent() (among others). This is all Swing, of
    > course. But perhaps this is behind the problem, since you're
    > interfering with the normal behavior of the Event Dispatching mechanism.
    >
    > = Steve =
    > --
    > Steve W. Jackson
    > Montgomery, Alabama
     
    Lee Garrington, Oct 1, 2003
    #3
  4. In article <3f7b2976$0$10979$>,
    "Lee Garrington" <> wrote:

    >:Yeah I know I shouldnt directly call it but in the past that is the only way
    >:I have ever managed to prevent the gui from freezing.
    >:
    >:repaint() just doesnt work. It doesnt repaint anything unfortunately and I
    >:dont exactly know why.
    >:
    >:Maybe there is another solution to this type of problem?
    >:
    >:"Steve W. Jackson" <> wrote in message
    >:news:-berlin.de...
    >:> In article <3f7b24b7$0$11001$>,
    >:> "Lee Garrington" <> wrote:
    >:>
    >:> >:Hello,
    >:> >:
    >:> >:Basically this is my problem. I am programming minesweeper in
    >:> >:java. I
    >:have
    >:> >:a panel in the background on which I draw all the mines/numbers
    >:> >:etc. and then I have an array of buttons that sit on top of them.
    >:> >:
    >:> >:I am developing a solver and need a pause between each move the
    >:algorithm
    >:> >:makes. I know from my past experience that you have to use
    >:> >:something
    >:along
    >:> >:the lines of :-
    >:> >:
    >:> >:Graphics g = board.getGraphics();
    >:> >: board.paintComponent(g); g.dispose();
    >:> >:
    >:> >:to get the board to update after each successive move but I seem
    >:> >:to be having problems getting it working properly.
    >:> >:
    >:> >:- If I use the above to repaint the background then all the
    >:> >:buttons disappear. - If I use the above to repaint all the
    >:> >:buttons it doesnt actually
    >:repaint
    >:> >:them, it just goes grey. -If I try and repaint both, wherever
    >:> >:moves are made by the computer you
    >:can
    >:> >:see the relevant square in the background but none of the other
    >:> >:buttons
    >:are
    >:> >:drawn and its just grey instead.
    >:> >:
    >:> >:After completeing the algorithm it all redraws as normal.
    >:> >:
    >:> >:I just want everything to refresh as normal when each move by the
    >:computer
    >:> >:is made, can anyone help me out?
    >:> >:
    >:> >:Thx in advance
    >:> >:
    >:>
    >:> You should never call paintComponent() directly. You should instead
    >:> always call repaint(), which posts an event that calls paint(), which in
    >:> turn calls paintComponent() (among others). This is all Swing, of
    >:> course. But perhaps this is behind the problem, since you're
    >:> interfering with the normal behavior of the Event Dispatching mechanism.
    >:>
    >:> = Steve =
    >:> --
    >:> Steve W. Jackson
    >:> Montgomery, Alabama


    The GUI will freeze if you're doing things on the Event Dispatching
    Thread. You shouldn't do that, in general. Instead, put your
    activities in separate threads, have them draw and do their other work
    as needed, and then have that thread call repaint(). The repaint()
    method does nothing more than post a notice for the EDT to repaint the
    specified component -- which means those events will be coalesced if
    multiples exist. I've had really good luck with GUI responsiveness once
    I learned the secret of doing most everything in a thread other than the
    EDT.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Oct 1, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Miller
    Replies:
    3
    Views:
    3,977
    Stephen Miller
    Jul 2, 2004
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,436
  3. Dmitry Teslenko
    Replies:
    0
    Views:
    627
    Dmitry Teslenko
    Jan 3, 2010
  4. Sam Roberts
    Replies:
    16
    Views:
    381
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    234
    Richard
    May 22, 2007
Loading...

Share This Page