Home > Back-end >  Keyboard keys to monitor trigger methods are run by EDT? That in this method performs to redraw the
Keyboard keys to monitor trigger methods are run by EDT? That in this method performs to redraw the

Time:03-20

In the process of learning JAVA, try to write a tetris, created a method of goDown control block moves down, fixed time was achieved by the new thread sleep down once, when the keys to monitor to the keyboard press the s key, will perform a goDown method,
GoDown method call a clearLine method, is to determine a line all square, sometimes several disappeared after the flashing (flashing is by removing trip, redrawn, thread to sleep 100 milliseconds, add the trip, redrawn, thread to sleep 100 milliseconds, cycle times)


Appears a little problem here, if the goDown method is a new thread calls, the flashing effect can be achieved, no problem, but if the goDown method is s key by call, it will run on the EDT, resulting in the goDown performed before, will not to redraw, scintillation effect is not possible, if I keep keys can invoke the goDown is there a way to solve this problem? Code I also put on:

 @ Override 
Public void keyTyped KeyEvent (e) {


If (um participant etKeyChar ()=='w') {

}
If (um participant etKeyChar ()=='s') {
if(! IsOver) {
GoDown ();
}

}
If (um participant etKeyChar ()=='d') {
GoRight ();
}
If (um participant etKeyChar ()=='a') {
GoLeft ();
}


If (isOver) {
if(! IsScoreBoard) {
IsScoreBoard=true;
ShowScoreBoard ();
}
The else {
IsScoreBoard=false;
NewGame ();
}

}

}


//button listener
@ Override
Public void keyPressed KeyEvent (e) {


}

//release the listener
@ Override
Public void keyReleased KeyEvent (e) {

}


Public void runDownThread () {
Thread t=new Thread () {
Public void the run () {

while(! IsOver) {
GoDown ();
repaint();

Try {
Thread.sleep (2000/speed);
} catch InterruptedException (e) {}

}


}

};
t.start();

}

Public void goLeft () {
Boolean isAbleToGoLeft=true;
For (Coords cubeCoords: tetriminos) {
If (cubeCoords getX () & lt; Cs=| | landedCubes. The contains (new Coords (cubeCoords. GetX () - cs, cubeCoords. GetY ()))) {
IsAbleToGoLeft=false;
}
}

If (isAbleToGoLeft) {
for(int i=0; I & lt; Tetriminos. The size (); I++) {
Tetriminos. Get (I). ChangeX (cs);
If (landedCubes. The contains (new Coords (tetriminos. Get (I), getX (), tetriminos. Get (I), getY () + cs))
| | tetriminos. Get (I). GetY () & gt;={fieldHeight)
IsLanded=true;
} else {
IsLanded=false;
}
//tetriminos. Get (I). Print ();
}
}
repaint();
}

Public void goRight () {
Boolean isAbleToGoRight=true;
For (Coords cubeCoords: tetriminos) {
If (cubeCoords getX () & gt;=fieldWidth | | landedCubes. The contains (new Coords (cubeCoords. GetX () + cs, cubeCoords. GetY ()))) {
IsAbleToGoRight=false;
}
}
If (isAbleToGoRight) {
for(int i=0; I & lt; Tetriminos. The size (); I++) {
Tetriminos. Get (I). ChangeX (cs);
If (landedCubes. The contains (new Coords (tetriminos. Get (I), getX (), tetriminos. Get (I), getY () + cs))
| | tetriminos. Get (I). GetY () & gt;={fieldHeight)
IsLanded=true;
} else {
IsLanded=false;
}
//tetriminos. Get (I). Print ();
}
}
repaint();
}



Public void clearLine () {

Tetriminos. The clear ();


for(int i=0; I & lt; 5; I++) {
For (int yCoord: clearedLineYCoordsList) {
LandedCubes. RemoveAll (lineCoordsMap. Get (yCoord));
}
repaint();


Try {
Thread.sleep (100);
} catch InterruptedException (e) {}

For (int yCoord: clearedLineYCoordsList) {
LandedCubes. AddAll (lineCoordsMap. Get (yCoord));
}
repaint();


Try {
Thread.sleep (100);
} catch InterruptedException (e) {}


}

For (int yCoord: clearedLineYCoordsList) {
LandedCubes. RemoveAll (lineCoordsMap. Get (yCoord));
}

For (Coords cubeCoords: landedCubes) {

Int aboveLinesCount=0;
For (int yCoord: clearedLineYCoordsList) {

If (cubeCoords getY () & lt; YCoord) {
AboveLinesCount++;
}
}
* aboveLinesCount cubeCoords. ChangeY (cs);
}
repaint();
Try {
Thread.sleep (500);
} catch InterruptedException (e) {}



}


Public void goDown () {


If (isLanded) {

LandedCubes. AddAll (tetriminos);

For (Coords cubeCoords: tetriminos) {

If (landedCubes. ContainsAll (lineCoordsMap. Get (cubeCoords. GetY ()))) {
ClearedLineYCoordsList. Add (cubeCoords getY ());
}
}
if(! ClearedLineYCoordsList. IsEmpty ()) {
ClearLine ();
ClearedLineYCoordsList. The clear ();

}

GetNewTetriminos ();
IsLanded=false;
For (Coords cubeCoords: tetriminos) {
If (landedCubes. The contains (new Coords (cubeCoords. GetX (), cubeCoords. GetY ()))) {
IsOver=true;
}
If (landedCubes. The contains (new Coords (cubeCoords. GetX (), cubeCoords. GetY () + cs))) {
IsLanded=true;
}
}
repaint();
return;
}
for(int i=0; I & lt; Tetriminos. The size (); I++) {
Tetriminos. Get (I). ChangeY (cs);
}


For (Coords cubeCoords: tetriminos) {
If (cubeCoords getY ()==fieldHeight | | landedCubes. The contains (new Coords (cubeCoords. GetX (), cubeCoords. GetY () + cs))) {
IsLanded=true; nullnullnullnullnullnullnull
  • Related