So I'm trying to code Tetris in Java and I'm using JLabels for each piece. I'm using a Runnable timer to have the pieces move down, and then I use if statements with an executor to control when the piece should stop moving. The problem I have is how I should program the pieces so that when they interact with each other they stop moving down and trigger the executor shutdown. I know that I want to program the block to stop when the color that is a block ahead of it isn't black, signifying that there is a block there, but I need help figuring that out technically.
For Example:
Runnable timer = new Runnable() {
public void run() {
if((825 - (piece.getSize().getHeight() + 40 + piece.getY()) < 1)) {
executor.shutdown();
block();
time();
}
else if(piece2 != null && (825 - (piece2.getSize().getHeight() + 40 + piece2.getY()) < 1)) {
executor.shutdown();
block();
time();
}
}
executor.scheduleAtFixedRate(timer, 2, 1, TimeUnit.SECONDS);
}
piece2 is referring to the other piece and is only used in shapes where I used to pieces to configure it.
I tried to make a block that tested the color in front of the other block but I had no way of doing that seeing as how I either only tested the background color or the color of the actual block. In which case I would need a way to adapt the block in front of the actual piece to the color that's present whether it be the background or the block, but I don't know how to do that.
Some suggestions: