import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ErsBlocksGame extends JFrame {
public final static int PER_LINE_SCORE = 100;
public final static int PER_LEVEL_SCORE = PER_LINE_SCORE * 20;
public final static int MAX_LEVEL = 10;
public final static int DEFAULT_LEVEL = 5;
private GameCanvas canvas;
private ErsBlock block;
private boolean playing = false;
private ControlPanel ctrlPanel;
private JMenuBar bar = new JMenuBar();
private JMenu
mGame = new JMenu("Game"),
mControl = new JMenu("Control"),
mWindowStyle = new JMenu("WindowStyle"),
mInfo = new JMenu("Information");
private JMenuItem
miNewGame = new JMenuItem("New Game"),
miSetBlockColor = new JMenuItem("Set Block Color ..."),
miSetBackColor = new JMenuItem("Set Background Color ..."),
miTurnHarder = new JMenuItem("Turn up the level"),
miTurnEasier = new JMenuItem("Turn down the level"),
miExit = new JMenuItem("Eixt"),
miPlay = new JMenuItem("Play"),
miPause = new JMenuItem("Pause"),
miResume = new JMenuItem("Resume"),
miStop = new JMenuItem("Stop"),
miAuthor = new JMenuItem("Author : [email protected]"),
miSourceInfo = new JMenuItem("You can get the source code / document by email");
private JCheckBoxMenuItem
miAsWindows = new JCheckBoxMenuItem("Windows"),
miAsMotif = new JCheckBoxMenuItem("Motif"),
miAsMetal = new JCheckBoxMenuItem("Metal", true);
public ErsBlocksGame(String title) {
super(title);
setSize(315, 392);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((scrSize.width - getSize().width) / 2,
(scrSize.height - getSize().height) / 2);
createMenu();
Container container = getContentPane();
container.setLayout(new BorderLayout(6, 0));
canvas = new GameCanvas(20, 12);
ctrlPanel = new ControlPanel(this);
container.add(canvas, BorderLayout.CENTER);
container.add(ctrlPanel, BorderLayout.EAST);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
stopGame();
System.exit(0);
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
canvas.fanning();
}
});
setVisible(true);
canvas.fanning();
}
public void reset() {
ctrlPanel.reset();
canvas.reset();
}
public boolean isPlaying() {
return playing;
}
public ErsBlock getCurBlock() {
return block;
}
public GameCanvas getCanvas() {
return canvas;
}
public void playGame() {
play();
ctrlPanel.setPlayButtonEnable(false);
miPlay.setEnabled(false);
ctrlPanel.requestFocus();
}
private void play() {
reset();
playing = true;
Thread thread = new Thread(new Game());
thread.start();
}
public void pauseGame() {
if (block != null) block.pauseMove();
ctrlPanel.setPauseButtonLabel(false);
miPause.setEnabled(false);
miResume.setEnabled(true);
}
public void resumeGame() {
if (block != null) block.resumeMove();
ctrlPanel.setPauseButtonLabel(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.requestFocus();
}
public void stopGame() {
playing = false;
if (block != null) block.stopMove();
miPlay.setEnabled(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.setPlayButtonEnable(true);
ctrlPanel.setPauseButtonLabel(true);
}
public int getLevel() {
return ctrlPanel.getLevel();
}
public void setLevel(int level) {
if (level < 11 && level > 0) ctrlPanel.setLevel(level);
}
public int getScore() {
if (canvas != null) return canvas.getScore();
return 0;
}
public int getScoreForLevelUpdate() {
if (canvas != null) return canvas.getScoreForLevelUpdate();
return 0;
}
public boolean levelUpdate() {
int curLevel = getLevel();
if (curLevel < MAX_LEVEL) {
setLevel(curLevel + 1);
canvas.resetScoreForLevelUpdate();
return true;
}
return false;
}
private void reportGameOver() {
JOptionPane.showMessageDialog(this, "Game Over!");
}
private void createMenu() {
bar.add(mGame);
bar.add(mControl);
bar.add(mWindowStyle);
bar.add(mInfo);
mGame.add(miNewGame);
mGame.addSeparator();
mGame.add(miSetBlockColor);
mGame.add(miSetBackColor);
mGame.addSeparator();
mGame.add(miTurnHarder);
mGame.add(miTurnEasier);
mGame.addSeparator();
mGame.add(miExit);
mControl.add(miPlay);
mControl.add(miPause);
mControl.add(miResume);
mControl.add(miStop);
mWindowStyle.add(miAsWindows);
mWindowStyle.add(miAsMotif);
mWindowStyle.add(miAsMetal);
mInfo.add(miAuthor);
mInfo.add(miSourceInfo);
setJMenuBar(bar);
miPause.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK));
miResume.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
miNewGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stopGame();
reset();
setLevel(DEFAULT_LEVEL);
}
});
miSetBlockColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Color newFrontColor =
JColorChooser.showDialog(ErsBlocksGame.this,
"Set color for block", canvas.getBlockColor());
if (newFrontColor != null)
canvas.setBlockColor(newFrontColor);
}
});
miSetBackColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Color newBackColor =
JColorChooser.showDialog(ErsBlocksGame.this,
"Set color for block", canvas.getBackgroundColor());
if (newBackColor != null)
canvas.setBackgroundColor(newBackColor);
}
});
miTurnHarder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int curLevel = getLevel();
if (curLevel < MAX_LEVEL) setLevel(curLevel + 1);
}
});
miTurnEasier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int curLevel = getLevel();
if (curLevel > 1) setLevel(curLevel - 1);
}
});
miExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
miPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
playGame();
}
});
miPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
pauseGame();
}
});
miResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resumeGame();
}
});
miStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stopGame();
}
});
miAsWindows.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
setWindowStyle(plaf);
canvas.fanning();
ctrlPanel.fanning();
miAsWindows.setState(true);
miAsMetal.setState(false);
miAsMotif.setState(false);
}
});
miAsMotif.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
setWindowStyle(plaf);
canvas.fanning();
ctrlPanel.fanning();
miAsWindows.setState(false);
miAsMetal.setState(false);
miAsMotif.setState(true);
}
});
miAsMetal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
setWindowStyle(plaf);
canvas.fanning();
ctrlPanel.fanning();
miAsWindows.setState(false);
miAsMetal.setState(true);
miAsMotif.setState(false);
}
});
}
private void setWindowStyle(String plaf) {
try {
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
}
}
private class Game implements Runnable {
public void run() {
int col = (int) (Math.random() * (canvas.getCols() - 3));
int style = ErsBlock.STYLES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
while (playing) {
if (block != null) {
if (block.isAlive()) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
continue;
}
}
checkFullLine();
if (isGameOver()) {
miPlay.setEnabled(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.setPlayButtonEnable(true);
ctrlPanel.setPauseButtonLabel(true);
reportGameOver();
return;
}
block = new ErsBlock(style, -1, col, getLevel(), canvas);
block.start();
col = (int) (Math.random() * (canvas.getCols() - 3));
style = ErsBlock.STYLES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
ctrlPanel.setTipStyle(style);
}
}
public void checkFullLine() {
for (int i = 0; i < canvas.getRows(); i++) {
int row = -1;
boolean fullLineColorBox = true;
for (int j = 0; j < canvas.getCols(); j++) {
if (!canvas.getBox(i, j).isColorBox()) {
fullLineColorBox = false;
break;
}
}
if (fullLineColorBox) {
row = i--;
canvas.removeLine(row);
}
}
}
private boolean isGameOver() {
for (int i = 0; i < canvas.getCols(); i++) {
ErsBox box = canvas.getBox(0, i);
if (box.isColorBox()) return true;
}
return false;
}
}
public static void main(String[] args) {
new ErsBlocksGame("Russia Blocks by ZGX");
}
}
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
class GameCanvas extends JPanel {
private Color backColor = Color.black, frontColor = Color.orange;
private int rows, cols, score = 0, scoreForLevelUpdate = 0;
private ErsBox[][] boxes;
private int boxWidth, boxHeight;
public GameCanvas(int rows, int cols) {
this.rows = rows;
this.cols = cols;
boxes = new ErsBox[rows][cols];
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boxes[i][j] = new ErsBox(false);
}
}
setBorder(new EtchedBorder(
EtchedBorder.RAISED, Color.white, new Color(148, 145, 140)));
}
public GameCanvas(int rows, int cols,
Color backColor, Color frontColor) {
this(rows, cols);
this.backColor = backColor;
this.frontColor = frontColor;
}
public void setBackgroundColor(Color backColor) {
this.backColor = backColor;
}
public Color getBackgroundColor() {
return backColor;
}
public void setBlockColor(Color frontColor) {
this.frontColor = frontColor;
}
public Color getBlockColor() {
return frontColor;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public int getScore() {
return score;
}
public int getScoreForLevelUpdate() {
return scoreForLevelUpdate;
}
public void resetScoreForLevelUpdate() {
scoreForLevelUpdate -= ErsBlocksGame.PER_LEVEL_SCORE;
}
public ErsBox getBox(int row, int col) {
if (row < 0 || row > boxes.length - 1
|| col < 0 || col > boxes[0].length - 1)
return null;
return (boxes[row][col]);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(frontColor);
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
g.setColor(boxes[i][j].isColorBox() ? frontColor : backColor);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
}
}
}
public void fanning() {
boxWidth = getSize().width / cols;
boxHeight = getSize().height / rows;
}
public synchronized void removeLine(int row) {
for (int i = row; i > 0; i--) {
for (int j = 0; j < cols; j++)
boxes[i][j] = (ErsBox) boxes[i - 1][j].clone();
}
score += ErsBlocksGame.PER_LINE_SCORE;
scoreForLevelUpdate += ErsBlocksGame.PER_LINE_SCORE;
repaint();
}
public void reset() {
score = 0;
scoreForLevelUpdate = 0;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++)
boxes[i][j].setColor(false);
}
repaint();
}
}
import java.awt.*;
class ErsBox implements Cloneable {
private boolean isColor;
private Dimension size = new Dimension();
public ErsBox(boolean isColor) {
this.isColor = isColor;
}
public boolean isColorBox() {
return isColor;
}
public void setColor(boolean isColor) {
this.isColor = isColor;
}
public Dimension getSize() {
return size;
}
public void setSize(Dimension size) {
this.size = size;
}
public Object clone() {
Object cloned = null;
try {
cloned = super.clone();
} catch (Exception ex) {
ex.printStackTrace();
}
return cloned;
}
}
class ErsBlock extends Thread {
public final static int BOXES_ROWS = 4;
public final static int BOXES_COLS = 4;
public final static int LEVEL_FLATNESS_GENE = 3;
public final static int BETWEEN_LEVELS_DEGRESS_TIME = 50;
private final static int BLOCK_KIND_NUMBER = 7;
private final static int BLOCK_STATUS_NUMBER = 4;
public final static int[][] STYLES = {
{0x0f00, 0x4444, 0x0f00, 0x4444},
{0x04e0, 0x0464, 0x00e4, 0x04c4},
{0x4620, 0x6c00, 0x4620, 0x6c00},
{0x2640, 0xc600, 0x2640, 0xc600},
{0x6220, 0x1700, 0x2230, 0x0740},
{0x6440, 0x0e20, 0x44c0, 0x8e00},
{0x0660, 0x0660, 0x0660, 0x0660},
};
private GameCanvas canvas;
private ErsBox[][] boxes = new ErsBox[BOXES_ROWS][BOXES_COLS];
private int style, y, x, level;
private boolean pausing = false, moving = true;
public ErsBlock(int style, int y, int x, int level, GameCanvas canvas) {
this.style = style;
this.y = y;
this.x = x;
this.level = level;
this.canvas = canvas;
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((style & key) != 0);
boxes[i][j] = new ErsBox(isColor);
key >>= 1;
}
}
display();
}
public void run() {
while (moving) {
try {
sleep(BETWEEN_LEVELS_DEGRESS_TIME
* (ErsBlocksGame.MAX_LEVEL - level + LEVEL_FLATNESS_GENE));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
if (!pausing) moving = (moveTo(y + 1, x) && moving);
}
}
public void moveLeft() {
moveTo(y, x - 1);
}
public void moveRight() {
moveTo(y, x + 1);
}
public void moveDown() {
moveTo(y + 1, x);
}
public void turnNext() {
for (int i = 0; i < BLOCK_KIND_NUMBER; i++) {
for (int j = 0; j < BLOCK_STATUS_NUMBER; j++) {
if (STYLES[i][j] == style) {
int newStyle = STYLES[i][(j + 1) % BLOCK_STATUS_NUMBER];
turnTo(newStyle);
return;
}
}
}
}
public void pauseMove() {
pausing = true;
}
public void resumeMove() {
pausing = false;
}
public void stopMove() {
moving = false;
}
private void earse() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(i + y, j + x);
if (box == null) continue;
box.setColor(false);
}
}
}
}
private void display() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(y + i, x + j);
if (box == null) continue;
box.setColor(true);
}
}
}
}
private boolean isMoveAble(int newRow, int newCol) {
earse();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(newRow + i, newCol + j);
if (box == null || (box.isColorBox())) {
display();
return false;
}
}
}
}
display();
return true;
}
private synchronized boolean moveTo(int newRow, int newCol) {
if (!isMoveAble(newRow, newCol) || !moving) return false;
earse();
y = newRow;
x = newCol;
display();
canvas.repaint();
return true;
}
private boolean isTurnAble(int newStyle) {
int key = 0x8000;
earse();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if ((newStyle & key) != 0) {
ErsBox box = canvas.getBox(y + i, x + j);
if (box == null || box.isColorBox()) {
display();
return false;
}
}
key >>= 1;
}
}
display();
return true;
}
private boolean turnTo(int newStyle) {
if (!isTurnAble(newStyle) || !moving) return false;
earse();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((newStyle & key) != 0);
boxes[i][j].setColor(isColor);
key >>= 1;
}
}
style = newStyle;
display();
canvas.repaint();
return true;
}
}
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
class ControlPanel extends JPanel {
private JTextField
tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),
tfScore = new JTextField("0");
private JButton
btPlay = new JButton("Play"),
btPause = new JButton("Pause"),
btStop = new JButton("Stop"),
btTurnLevelUp = new JButton("Turn hard"),
btTurnLevelDown = new JButton("Turn easy");
private JPanel plTip = new JPanel(new BorderLayout());
private TipPanel plTipBlock = new TipPanel();
private JPanel plInfo = new JPanel(new GridLayout(4, 1));
private JPanel plButton = new JPanel(new GridLayout(5, 1));
private Timer timer;
private ErsBlocksGame game;
private Border border = new EtchedBorder(
EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));
public ControlPanel(final ErsBlocksGame game) {
setLayout(new GridLayout(3, 1, 0, 4));
this.game = game;
plTip.add(new JLabel("Next block"), BorderLayout.NORTH);
plTip.add(plTipBlock);
plTip.setBorder(border);
plInfo.add(new JLabel("Level"));
plInfo.add(tfLevel);
plInfo.add(new JLabel("Score"));
plInfo.add(tfScore);
plInfo.setBorder(border);
tfLevel.setEditable(false);
tfScore.setEditable(false);
plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);
add(plTip);
add(plInfo);
add(plButton);
addKeyListener(new ControlKeyListener());
btPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.playGame();
}
});
btPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (btPause.getText().equals(new String("Pause"))) {
game.pauseGame();
} else {
game.resumeGame();
}
}
});
btStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.stopGame();
}
});
btTurnLevelUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level < ErsBlocksGame.MAX_LEVEL)
tfLevel.setText("" + (level + 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level > 1)
tfLevel.setText("" + (level - 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
plTipBlock.fanning();
}
});
timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tfScore.setText("" + game.getScore());
int scoreForLevelUpdate =
game.getScoreForLevelUpdate();
if (scoreForLevelUpdate >= ErsBlocksGame.PER_LEVEL_SCORE
&& scoreForLevelUpdate > 0)
game.levelUpdate();
}
});
timer.start();
}
public void setTipStyle(int style) {
plTipBlock.setStyle(style);
}
public int getLevel() {
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
} catch (NumberFormatException e) {
}
return level;
}
public void setLevel(int level) {
if (level > 0 && level < 11) tfLevel.setText("" + level);
}
public void setPlayButtonEnable(boolean enable) {
btPlay.setEnabled(enable);
}
public void setPauseButtonLabel(boolean pause) {
btPause.setText(pause ? "Pause" : "Continue");
}
public void reset() {
tfScore.setText("0");
plTipBlock.setStyle(0);
}
public void fanning() {
plTipBlock.fanning();
}
private class TipPanel extends JPanel {
private Color backColor = Color.darkGray, frontColor = Color.lightGray;
private ErsBox[][] boxes =
new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];
private int style, boxWidth, boxHeight;
private boolean isTiled = false;
public TipPanel() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++)
boxes[i][j] = new ErsBox(false);
}
}
public TipPanel(Color backColor, Color frontColor) {
this();
this.backColor = backColor;
this.frontColor = frontColor;
}
public void setStyle(int style) {
this.style = style;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isTiled) fanning();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
Color color = (((key & style) != 0) ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
key >>= 1;
}
}
}
public void fanning() {
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}
private class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (!game.isPlaying()) return;
ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode()) {
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
}
}
}
}