codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Game - illegal combination of modifiers  sashimi at 03:51 on Thursday, April 27, 2006
 

Hi all,

I am in the process of writing a tetris game, it consist of 3 different classes (written below). Whenever I compile the Tetris class, I get the 'illegal combination of modifiers: public and private' message. Can someone help me with this?

------------------------ Tetris Class ------------------------

import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

class Tetris extends Frame {

public static void main (String [] args)
{ Tetris world = new Tetris(); world.show(); }

// data fields
public static final int FrameWidth = 300;
public static final int FrameHeight = 400;
private PieceMover player;

public Tetris () {
setSize (FrameWidth, FrameHeight);
setTitle ("Tetris");

addKeyListener (new keyDown());
player = new PieceMover(this);
player.start();
}

// interface point between two threads
// data shared by event handler and piece mover
private int currentCommand = Piece.Down;

// synchronized method for accessing shared data
public synchronized int getCommand (int nextCommand) {
int oldCommand = currentCommand;
currentCommand = nextCommand;
return oldCommand;
}

public void update (Graphics g)
{ player.paint(sg);}

interface KeyListener extends EventListener {
public void keyTyped (KeyEvent e);
public void keyPressed (KeyEvent e);
public void keyReleased (KeyEvent e);

private class keyDown extends KeyAdapter {
public void keyPressed (KeyEvent e) {
char key = e.getKeyChar();
switch (key) {
case 'g': player.newGame(); break;
case 'j': getCommand(Piece.Left); break;
case 'k': getCommand(Piece.Rotate); break;
case 'l': getCommand(Piece.Right); break;
case 'q': System.exit(0);
}
}
}
}
}

-------------------- Piece Mover Class ----------------------

class PieceMover extends Thread {

private Tetris controller;
private Color table [][];
private Piece currentPiece;
private int score = 0;

public PieceMover (Tetris t) {
controller = t;
table = new Color [15] [30];
currentPiece = null;
newGame();
}

// thread starting point
public void run ()
{ while (dropPiece()) {}}

// other methods
public void newGame () {
for (int i=0; i < 30; i++)
for (int j=0; j < 15; j++)
table[j] = Color.white;
}

public void paint (Graphics g) {
for(int i=0; i<30; i++)
{
for(int j=0; j<15; j++)
{
g.setColor(table[j]);
g.fillRect(20+10*j, 350-10*i, 10, 10);
g.setColor(Color.blue);
g.drawString("Score" + score, 200, 200);
}
}
}

public boolean dropPiece() {
int piecetype = 1 + (int) (7* Math.random());
currentPiece = new Piece (piecetype);
if (! currentPiece.move (Piece.Down, table))
return false;
int command = controller.getCommand (Piece.Down);
while (currentPiece.move (command.table)) {
controller.repaint ();
yield ();
try {
sleep(100);
} catch (InterruptedException e) {}
command = controller.getCommand (Piece.Down);
}

// piece cannot move, check score
checkScore();
return true;
}

private void moveDown (int start) {
for (int i = start; i<30; i ++)
for (int j = 0; j <15; j++)
if (i < 29)
table [j] = table [j][i+1];
else
table [j] = Color.white;
}

private void checkScore () {
for (int i = 0; i <30; i++) {
boolean scored = true;
for (int j = 0; j<15; j++)
if (table [j] == Color.white)
scored = false;
if (scored) {
score +=10;
moveDown(i);
i = i -1; // check row again
}
}
}
}

------------------------ Piece Class -------------------------

import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.color.* ;

class Piece {

private Point [] point;
private Color color;
private int x[];
private int y[];

public static final int Down = 1;
public static final int Left = 2;
public static final int Rotate = 3;
public static final int Right = 4;

public Piece(int type)
{
switch(type){
case 1: //s shape piece
point = new Point[4];
point[0] = new Point(7, 29);
point[1] = new Point(7, 28);
point[2] = new Point(8, 28);
point[3] = new Point(8, 27);
color = Color.GREEN;
break;
case 2: //l shape piece
point = new Point[4];
point[0] = new Point(7, 29);
point[1] = new Point(7, 28);
point[2] = new Point(8, 28);
point[3] = new Point(9, 28);
color = Color.BLUE;
break;
case 3: //s shape piece
point = new Point[4];
point[0] = new Point(8, 29);
point[1] = new Point(7, 28);
point[2] = new Point(8, 28);
point[3] = new Point(7, 27);
color = Color.GREEN;
break;
case 4: //pole shape piece
point = new Point[4];
point[0] = new Point(6, 29);
point[1] = new Point(7, 29);
point[2] = new Point(8, 29);
point[3] = new Point(9, 29);
color = Color.GREEN;
break;
case 5: //cube shape piece
point = new Point[4];
point[0] = new Point(9, 29);
point[1] = new Point(7, 28);
point[2] = new Point(8, 28);
point[3] = new Point(9, 28);
color = Color.GREEN;
break;
case 6: //l shape piece
point = new Point[4];
point[0] = new Point(7, 29);
point[1] = new Point(7, 28);
point[2] = new Point(8, 28);
point[3] = new Point(9, 28);
color = Color.GREEN;
break;
case 7: //odd shape piece
point = new Point[4];
point[0] = new Point(8, 29);
point[1] = new Point(7, 28);
point[2] = new Point(8, 28);
point[3] = new Point(9, 28);
color = Color.GREEN;
break;
}
}

private void erase(Color[][] table)
{
for(int i=0; i<x.length; i++)
table[x][y] = Color.white;
}

private void draw(Color[][] table)
{
for(int i=0; i<x.length; i++)
table[x][y] = color;
}

private boolean testPosition (int x, int y, Color [][] table)
{
if ((x < 0) || (x > 14))
return false;
if ((y < 0) || (y > 29))
return false;
if (table [x][y] != Color.white)
return false;
return true;

}

private boolean testRotate (Color [][] table) {
for (int i = 0; i<x.length; i++) {
int dx = x - x[1];
int dy = y - y[1];
int nx = x[1] + dy;
int ny = y[1] - dx;
if (! testPosition (nx, ny, table))
return false;
}
return true;
}

private boolean testDown (Color [][] table) {
for (int i = 0; i < x.length; i++)
if (! testPosition (x, y-1, table))
return false;
return true;
}

private void moveDown (){
for (int i =0; i < x.length; i++)
y = y -1;
}

public boolean move (int command, Color [][] table)
{
erase(table);
boolean canDoIt = false;
switch (command)
{
case Down: canDoIt = testDown(table); break;
//case Right: canDoIt = testMoveRight(table); break;
//case Left: canDoIt = testMoveLeft(table); break;
case Rotate: canDoIt = testRotate(table); break;
}
if (canDoIt)
switch(command)
{
case Down: moveDown(); break;
//case Right: moveRight(); break;
//case Left: moveLeft(); break;
//case Rotate: moveRotate(); break;
}

return true;
}
}

  Re: Game - illegal combination of modifiers  crwood at 06:48 on Thursday, April 27, 2006
 

The error given by the compiler is:

C:\jexp>javac tetris.java
tetris.java:43: illegal combination of modifiers: public and private
private class keyDown extends KeyAdapter {
^

The error message from the compiler says the trouble is on line 43 in your source file. Count down to line 43 and find "private class keyDown extends KeyAdapter {"
The cause is this:

interface KeyListener extends EventListener {
public void keyTyped (KeyEvent e);
public void keyPressed (KeyEvent e);
public void keyReleased (KeyEvent e);

private class keyDown extends KeyAdapter {
public void keyPressed (KeyEvent e) {
char key = e.getKeyChar();
switch (key) {
case 'g': player.newGame(); break;
case 'j': getCommand(Piece.Left); break;
case 'k': getCommand(Piece.Rotate); break;
case 'l': getCommand(Piece.Right); break;
case 'q': System.exit(0);
}
}
}
}

a private class inside a package-private interface.
Take the class out of the interface and the error message will go away.
Java defines KeyListener as extending java.util.EventListener already so there is not need to include this interface in your Tetris class. You should remove it.

  Re: Game - illegal combination of modifiers  sashimi at 15:07 on Thursday, April 27, 2006
 

Thanks crwood! I have removed the KeyListener interface.

There is not syntax errors in my code now, but when i run the Tetris class, I am not able to see the pieces. Is there a problem with my paint method?

-------------- Tetris Class ------------------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

class Tetris extends JFrame {

public static void main (String[] args)
{ Tetris world = new Tetris();
world.setVisible(true); }

// data fields
public static final int FrameWidth = 300;
public static final int FrameHeight = 400;
private PieceMover player;

public Tetris () {
setSize(FrameWidth, FrameHeight);
setTitle("Tetris");

addKeyListener(new keyDown());
player = new PieceMover(this);
player.start();
}

// interface point between two threads
// data shared by event handler and piece mover
private int currentCommand = Piece.Down;

// synchronized method for accessing shared data
public synchronized int getCommand (int nextCommand) {
int oldCommand = currentCommand;
currentCommand = nextCommand;
return oldCommand;
}

public void update (Graphics g)
{ player.paint(g);}


private class keyDown extends KeyAdapter {
public void keyPressed (KeyEvent e) {
char key = e.getKeyChar();
switch (key) {
case 'g': player.newGame(); break;
case 'j': getCommand(Piece.Left); break;
case 'k': getCommand(Piece.Rotate); break;
case 'l': getCommand(Piece.Right); break;
case 'q': System.exit(0);
}
}
}

}

--------------------- Piece Class -------------------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.color.* ;

class Piece {

private Color color;
private int x[];
private int y[];

public static final int Down = 1;
public static final int Left = 2;
public static final int Rotate = 3;
public static final int Right = 4;

public Piece(int type)
{

switch(type)
{
case 1:
x = new int[4];
y = new int[4];
x[0] = 7;
y[0] = 29;
x[1] = 7;
y[1] = 28;
x[2] = 8;
y[2] = 28;
x[3] = 8;
y[3] = 27;


color = Color.green;
break;
case 2:
x = new int[4];
y = new int[4];
x[0] = 7;
y[0] = 29;
x[1] = 7;
y[1] = 28;
x[2] = 7;
y[2] = 27;
x[3] = 7;
y[3] = 26;


color = Color.red;
break;
case 3:
x = new int[4];
y = new int[4];
x[0] = 7;
y[0] = 29;
x[1] = 6;
y[1] = 29;
x[2] = 5;
y[2] = 29;
x[3] = 4;
y[3] = 29;

color = Color.blue;
break;
case 4:
x = new int[4];
y = new int[4];
x[0] = 7;
y[0] = 29;
x[1] = 8;
y[1] = 29;
x[2] = 9;
y[2] = 29;
x[3] = 8;
y[3] = 28;

color = Color.black;
break;
case 5:
x = new int[4];
y = new int[4];
x[0] = 7;
y[0] = 29;
x[1] = 7;
y[1] = 28;
x[2] = 7;
y[2] = 27;
x[3] = 8;
y[3] = 27;

color = Color.yellow;
break;
case 6:
x = new int[4];
y = new int[4];
x[0] = 8;
y[0] = 29;
x[1] = 9;
y[1] = 29;
x[2] = 8;
y[2] = 28;
x[3] = 7;
y[3] = 28;

color = Color.orange;
break;
case 7:
x = new int[4];
y = new int[4];
x[0] = 7;
y[0] = 29;
x[1] = 7;
y[1] = 28;
x[2] = 8;
y[2] = 28;
x[3] = 9;
y[3] = 28;

color = Color.pink;
break; }

}

private void erase(Color[][] table)
{
for(int i=0; i<x.length; i++)
table[x][y] = Color.white;
}

private void draw(Color[][] table)
{
for(int i=0; i<x.length; i++)
table[x][y] = color;
}

private boolean testPosition (int x, int y, Color[][] table)
{
if ((x < 0) || (x > 14))
return false;
if ((y < 0) || (y > 29))
return false;
if (table[x][y] != Color.white)
return false;
return true;

}

private boolean testRotate (Color [][] table) {
for (int i = 0; i<x.length; i++) {
int dx = x - x[1];
int dy = y - y[1];
int nx = x[1] + dy;
int ny = y[1] - dx;
if (! testPosition (nx, ny, table))
return false;
}
return true;
}

private boolean testDown (Color[][] table) {
for (int i = 0; i < x.length; i++)
if (!testPosition(x, y-1, table))
return false;
return true;
}

private void moveDown (){
for (int i =0; i < x.length; i++)
y = y - 1;
}

public boolean move(int command, Color[][] table)
{
erase(table);
boolean canDoIt = false;
switch (command)
{
case Down: canDoIt = testDown(table); break;
//case Right: canDoIt = testMoveRight(table); break;
//case Left: canDoIt = testMoveLeft(table); break;
//case Rotate: canDoIt = testRotate(table); break;
}
if (canDoIt)
switch(command)
{
case Down: moveDown(); break;
//case Right: moveRight(); break;
//case Left: moveLeft(); break;
//case Rotate: moveRotate(); break;
}

return true;
}
}

--------------------- Piece Mover Class ---------------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
class PieceMover extends Thread {

private Tetris controller;
private Color table[][];
private Piece currentPiece;
private int score = 0;

public PieceMover(Tetris t) {
controller = t;
table = new Color[15][30];
currentPiece = null;
newGame();
}

// thread starting point
public void run()
{ while(dropPiece()) {}}

// other methods
public void newGame() {
for (int i=0; i < 30; i++)
for (int j=0; j < 15; j++)
table[j] = Color.white;
}

public void paint (Graphics g) {
for(int i=0; i<30; i++)
{
for(int j=0; j<15; j++)
{
g.setColor(table[j]);
g.fillRect(20+10*j, 350-10*i, 10, 10);
g.setColor(Color.blue);
g.drawString("Score" + score, 200, 200);
}
}
}

public boolean dropPiece() {
int piecetype = 1 + (int)(7* Math.random());
currentPiece = new Piece(piecetype);
if (!currentPiece.move(Piece.Down, table))
return false;
int command = controller.getCommand(Piece.Down);
while (currentPiece.move(command, table)) {
controller.repaint();
yield ();
try {
sleep(100);
} catch (InterruptedException e) {}
command = controller.getCommand(Piece.Down);
}

// piece cannot move, check score
checkScore();
return true;
}

private void moveDown (int start) {
for (int i = start; i<30; i ++)
for (int j = 0; j <15; j++)
if (i < 29)
table [j] = table [j][i+1];
else
table [j] = Color.white;
}

private void checkScore () {
for (int i = 0; i <30; i++) {
boolean scored = true;
for (int j = 0; j<15; j++)
if (table [j] == Color.white)
scored = false;
if (scored) {
score +=10;
moveDown(i);
i = i -1; // check row again
}
}
}

}



  Re: Game - illegal combination of modifiers  crwood at 16:56 on Thursday, April 27, 2006
 

Looking at the "run" method in your PieceMover class the while loop condition is the return value of the "dropPiece" method. In the "dropPiece" method I put in a "System.out.println" statement after the second line and it is called only once. So the expression in the first "if" statement is evaluating to true.
If you post more code please change the to [j] in your loops. The forum software interprets them as italic tags and eliminates them making reconstruction a chore.

<Added>

If you post more code please change the [i] to [j] in your loops. The forum software interprets them as italic tags and eliminates them making reconstruction a chore.

<Added>

hack, hack...
If you post more code please change the [i] to [j] in your loops. The forum software interprets them as italic tags and eliminates them making reconstruction a chore.

  Re: Game - illegal combination of modifiers  sashimi at 22:56 on Friday, April 28, 2006
 

Hi crwood,

Can you show me how to rectify this problem? I'm going nowhere from here.

Thank you.

  Re: Game - illegal combination of modifiers  crwood at 07:52 on Saturday, April 29, 2006
 

Some recommended changes:
Tetris class:
1 - add "setDefaultCloseOperation(EXIT_ON_CLOSE);" in the constructor
2 - replace the "update" method with

public void paint (Graphics g)
{
player.paint(g);
}

public void update(Graphics g)
{
paint(g);
}

this will make the painting smoother/flicker-free
Piece class:
1 - change the access modifier of the "draw" method to "public"
2 - the instantiation of x and y can be removed from each "case" and placed above the switch statement
PieceMover class:
1 - replace the while loop in the "run" method with the statement "dropPiece();"
2 - add the statement "currentPiece.draw(table);" after each call to "currentPiece.move(command, table)" -> after the "if" statement and at the top of the while loop.
this was the main problem - not drawing

  Re: Game - illegal combination of modifiers  sashimi at 02:40 on Sunday, April 30, 2006
 

Hi crwood,

Thank you so much for your guidance. I managed to see the piece, however only one piece appear everytime I run the Tetris class. I'm not able to detect what's wrong. Pls advise.

--------------- Tetris -----------------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

class Tetris extends JFrame {

// the main method
public static void main (String[] args)
{ Tetris world = new Tetris();
world.setVisible(true); }

// data fields
public static final int FrameWidth = 300;
public static final int FrameHeight = 400;
private PieceMover player;

// constructor - creating new object
public Tetris () {
setSize(FrameWidth, FrameHeight);
setTitle("Tetris");

addKeyListener(new keyDown());
player = new PieceMover(this);
player.start();

setDefaultCloseOperation(EXIT_ON_CLOSE);
}

// interface point between two threads
// data shared by event handler and piece mover

// Piece moving down as the default movement
private int currentCommand = Piece.Down;

// synchronized method for accessing shared data
public synchronized int getCommand (int nextCommand) {
int oldCommand = currentCommand;
currentCommand = nextCommand;
return oldCommand;
}

// key press handler - execute a command when there's a key pressed
private class keyDown extends KeyAdapter {
public void keyPressed (KeyEvent e) {
char key = e.getKeyChar();
switch (key) {
// assignment of a command to a key
case 'g': player.newGame(); break;
case 'j': getCommand(Piece.Left); break;
case 'k': getCommand(Piece.Rotate); break;
case 'l': getCommand(Piece.Right); break;
case 'q': System.exit(0);
}
}
}

// show interface
public void paint (Graphics g)
{
player.paint(g);
}

// to avoid fickle on screen, smooth display
public void update (Graphics g)
{ paint(g);}
}

------------- Piece Class -----------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.color.* ;

public class Piece {

// data fields
private Color color;
private int x[];
private int y[];

// moves
public static final int Down = 1;
public static final int Left = 2;
public static final int Rotate = 3;
public static final int Right = 4;

// constructor - creation of new piece object
public Piece(int type)
{
// instantiation x and y
x = new int[4];
y = new int[4];

switch(type)
{
case 1:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 8; y[2] = 28;
x[3] = 8; y[3] = 27;
color = Color.green;
break;

case 2:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 7; y[2] = 27;
x[3] = 7; y[3] = 26;
color = Color.red;
break;

case 3:
x[0] = 7; y[0] = 29;
x[1] = 6; y[1] = 29;
x[2] = 5; y[2] = 29;
x[3] = 4; y[3] = 29;
color = Color.blue;
break;

case 4:
x[0] = 7; y[0] = 29;
x[1] = 8; y[1] = 29;
x[2] = 9; y[2] = 29;
x[3] = 8; y[3] = 28;
color = Color.black;
break;

case 5:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 7; y[2] = 27;
x[3] = 8; y[3] = 27;
color = Color.yellow;
break;

case 6:
x[0] = 8; y[0] = 29;
x[1] = 9; y[1] = 29;
x[2] = 8; y[2] = 28;
x[3] = 7; y[3] = 28;
color = Color.orange;
break;

case 7:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 8; y[2] = 28;
x[3] = 9; y[3] = 28;
color = Color.pink;
break; }
}

public void erase(Color[][] table)
{
for(int k=0; k<x.length; k++)
table[x[k]][y[k]] = Color.white;
}

public void draw(Color[][] table)
{
for(int k=0; k<x.length; k++)
table[x[k]][y[k]] = color;
}

private boolean testPosition (int x, int y, Color[][] table)
{
if ((x < 0) || (x > 14))
return false;
if ((y < 0) || (y > 29))
return false;
if (table[x][y] != Color.white)
return false;
return true;
}

private boolean testRotate (Color [][] table)
{
for (int k=0; k<x.length; k++)
{
int dx = x[k] - x[1];
int dy = y[k] - y[1];
int nx = x[k] + dy;
int ny = y[k] + dx;
if (! testPosition (nx, ny, table))
return false;
}
return true;
}

private boolean testDown (Color[][] table)
{
for (int k=0; k<x.length; k++)
if (!testPosition(x[k], y[k]-1, table))
return false;
return true;
}

private boolean testMoveRight (Color[][] table)
{
for (int k=0; k<x.length; k++)
if (!testPosition(x[k], x[k]+1, table))
return false;
return true;
}

private boolean testMoveLeft (Color[][] table)
{
for (int k=0; k<x.length; k++)
if (!testPosition(x[k], x[k]-1, table))
return false;
return true;
}

private void moveDown()
{
for (int k =0; k<x.length; k++)
y[k] = y[k] - 1;
}

private void moveRight()
{
for (int k=0; k<x.length; k++)
x[k] = x[k] + 1;
}

private void moveLeft()
{
for (int k=0; k<x.length; k++)
x[k] = x[k] - 1;
}

private void moveRotate()
{
for (int k=0; k<x.length; k++)
{
int[] temp = {};
temp[k] = x[k];
x[k] = -y[k];
y[k] = temp[k];
}
}

public boolean move(int command, Color[][] table)
{
erase(table);
boolean canDoIt = false;
switch (command)
{
case Down: canDoIt = testDown(table); break;
case Right: canDoIt = testMoveRight(table); break;
case Left: canDoIt = testMoveLeft(table); break;
case Rotate: canDoIt = testRotate(table); break;
}

if (canDoIt)
switch(command)
{
case Down: moveDown(); break;
case Right: moveRight(); break;
case Left: moveLeft(); break;
case Rotate: moveRotate(); break;
}

draw(table);
if (command == Down)
return canDoIt;
return true;
}
}

---------- PieceMover Class ---------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
class PieceMover extends Thread {

private Tetris controller;
private Color table[][];
private Piece currentPiece;
private int score = 0;

// constructor - create new object
public PieceMover(Tetris t) {
controller = t;
table = new Color[15][30];
currentPiece = null;
newGame();
}

// thread starting point
public void run()
{ dropPiece(); }

// other methods
public void newGame() {
for (int i=0; i < 30; i++)
for (int j=0; j < 15; j++)
table[j] = Color.white;
}

// paint method - to draw interface
public void paint (Graphics g) {
for(int i=0; i<30; i++)
{
for(int j=0; j<15; j++)
{
g.setColor(table[j]);
g.fillRect(20+10*j, 350-10*i, 10, 10);
g.setColor(Color.blue);
g.drawString("Score" + score, 200, 200);
}
}
}

private boolean dropPiece() {
int piecetype = 1 + (int)(7* Math.random());
currentPiece = new Piece(piecetype);
if (! currentPiece.move(Piece.Down, table))
return false;
currentPiece.draw(table);
int command = controller.getCommand(Piece.Down);
currentPiece.draw(table);
while (currentPiece.move(command, table)) {
controller.repaint();
yield ();
try
{
sleep(100);
}
catch (InterruptedException e)
{
}
command = controller.getCommand(Piece.Down);
}

// call checkScore method when piece cannot move
checkScore();
return true;
}

private void checkScore ()
{
for (int i = 0; i <30; i++)
{
boolean scored = true;
for (int j = 0; j<15; j++)
if (table [j] == Color.white)
scored = false;

// increment of 10 points when scored
if (scored) {
score +=10;
moveDown(i);
i = i -1; // check row again
}
}
}

// moveDown method - copy colour from the row above
private void moveDown (int start)
{
for (int k = start; k<30; k ++)
for (int j = 0; j <15; j++)
if (k < 29)
table [j][k] = table [j][k+1];
else
table [j][k] = Color.white;
}

}

  Re: Game - illegal combination of modifiers  sashimi at 02:40 on Sunday, April 30, 2006
 

Hi crwood,

Thank you so much for your guidance. I managed to see the piece, however only one piece appear everytime I run the Tetris class. I'm not able to detect what's wrong. Pls advise.

--------------- Tetris -----------------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

class Tetris extends JFrame {

// the main method
public static void main (String[] args)
{ Tetris world = new Tetris();
world.setVisible(true); }

// data fields
public static final int FrameWidth = 300;
public static final int FrameHeight = 400;
private PieceMover player;

// constructor - creating new object
public Tetris () {
setSize(FrameWidth, FrameHeight);
setTitle("Tetris");

addKeyListener(new keyDown());
player = new PieceMover(this);
player.start();

setDefaultCloseOperation(EXIT_ON_CLOSE);
}

// interface point between two threads
// data shared by event handler and piece mover

// Piece moving down as the default movement
private int currentCommand = Piece.Down;

// synchronized method for accessing shared data
public synchronized int getCommand (int nextCommand) {
int oldCommand = currentCommand;
currentCommand = nextCommand;
return oldCommand;
}

// key press handler - execute a command when there's a key pressed
private class keyDown extends KeyAdapter {
public void keyPressed (KeyEvent e) {
char key = e.getKeyChar();
switch (key) {
// assignment of a command to a key
case 'g': player.newGame(); break;
case 'j': getCommand(Piece.Left); break;
case 'k': getCommand(Piece.Rotate); break;
case 'l': getCommand(Piece.Right); break;
case 'q': System.exit(0);
}
}
}

// show interface
public void paint (Graphics g)
{
player.paint(g);
}

// to avoid fickle on screen, smooth display
public void update (Graphics g)
{ paint(g);}
}

------------- Piece Class -----------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.color.* ;

public class Piece {

// data fields
private Color color;
private int x[];
private int y[];

// moves
public static final int Down = 1;
public static final int Left = 2;
public static final int Rotate = 3;
public static final int Right = 4;

// constructor - creation of new piece object
public Piece(int type)
{
// instantiation x and y
x = new int[4];
y = new int[4];

switch(type)
{
case 1:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 8; y[2] = 28;
x[3] = 8; y[3] = 27;
color = Color.green;
break;

case 2:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 7; y[2] = 27;
x[3] = 7; y[3] = 26;
color = Color.red;
break;

case 3:
x[0] = 7; y[0] = 29;
x[1] = 6; y[1] = 29;
x[2] = 5; y[2] = 29;
x[3] = 4; y[3] = 29;
color = Color.blue;
break;

case 4:
x[0] = 7; y[0] = 29;
x[1] = 8; y[1] = 29;
x[2] = 9; y[2] = 29;
x[3] = 8; y[3] = 28;
color = Color.black;
break;

case 5:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 7; y[2] = 27;
x[3] = 8; y[3] = 27;
color = Color.yellow;
break;

case 6:
x[0] = 8; y[0] = 29;
x[1] = 9; y[1] = 29;
x[2] = 8; y[2] = 28;
x[3] = 7; y[3] = 28;
color = Color.orange;
break;

case 7:
x[0] = 7; y[0] = 29;
x[1] = 7; y[1] = 28;
x[2] = 8; y[2] = 28;
x[3] = 9; y[3] = 28;
color = Color.pink;
break; }
}

public void erase(Color[][] table)
{
for(int k=0; k<x.length; k++)
table[x[k]][y[k]] = Color.white;
}

public void draw(Color[][] table)
{
for(int k=0; k<x.length; k++)
table[x[k]][y[k]] = color;
}

private boolean testPosition (int x, int y, Color[][] table)
{
if ((x < 0) || (x > 14))
return false;
if ((y < 0) || (y > 29))
return false;
if (table[x][y] != Color.white)
return false;
return true;
}

private boolean testRotate (Color [][] table)
{
for (int k=0; k<x.length; k++)
{
int dx = x[k] - x[1];
int dy = y[k] - y[1];
int nx = x[k] + dy;
int ny = y[k] + dx;
if (! testPosition (nx, ny, table))
return false;
}
return true;
}

private boolean testDown (Color[][] table)
{
for (int k=0; k<x.length; k++)
if (!testPosition(x[k], y[k]-1, table))
return false;
return true;
}

private boolean testMoveRight (Color[][] table)
{
for (int k=0; k<x.length; k++)
if (!testPosition(x[k], x[k]+1, table))
return false;
return true;
}

private boolean testMoveLeft (Color[][] table)
{
for (int k=0; k<x.length; k++)
if (!testPosition(x[k], x[k]-1, table))
return false;
return true;
}

private void moveDown()
{
for (int k =0; k<x.length; k++)
y[k] = y[k] - 1;
}

private void moveRight()
{
for (int k=0; k<x.length; k++)
x[k] = x[k] + 1;
}

private void moveLeft()
{
for (int k=0; k<x.length; k++)
x[k] = x[k] - 1;
}

private void moveRotate()
{
for (int k=0; k<x.length; k++)
{
int[] temp = {};
temp[k] = x[k];
x[k] = -y[k];
y[k] = temp[k];
}
}

public boolean move(int command, Color[][] table)
{
erase(table);
boolean canDoIt = false;
switch (command)
{
case Down: canDoIt = testDown(table); break;
case Right: canDoIt = testMoveRight(table); break;
case Left: canDoIt = testMoveLeft(table); break;
case Rotate: canDoIt = testRotate(table); break;
}

if (canDoIt)
switch(command)
{
case Down: moveDown(); break;
case Right: moveRight(); break;
case Left: moveLeft(); break;
case Rotate: moveRotate(); break;
}

draw(table);
if (command == Down)
return canDoIt;
return true;
}
}

---------- PieceMover Class ---------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
class PieceMover extends Thread {

private Tetris controller;
private Color table[][];
private Piece currentPiece;
private int score = 0;

// constructor - create new object
public PieceMover(Tetris t) {
controller = t;
table = new Color[15][30];
currentPiece = null;
newGame();
}

// thread starting point
public void run()
{ dropPiece(); }

// other methods
public void newGame() {
for (int i=0; i < 30; i++)
for (int j=0; j < 15; j++)
table[j] = Color.white;
}

// paint method - to draw interface
public void paint (Graphics g) {
for(int i=0; i<30; i++)
{
for(int j=0; j<15; j++)
{
g.setColor(table[j]);
g.fillRect(20+10*j, 350-10*i, 10, 10);
g.setColor(Color.blue);
g.drawString("Score" + score, 200, 200);
}
}
}

private boolean dropPiece() {
int piecetype = 1 + (int)(7* Math.random());
currentPiece = new Piece(piecetype);
if (! currentPiece.move(Piece.Down, table))
return false;
currentPiece.draw(table);
int command = controller.getCommand(Piece.Down);
currentPiece.draw(table);
while (currentPiece.move(command, table)) {
controller.repaint();
yield ();
try
{
sleep(100);
}
catch (InterruptedException e)
{
}
command = controller.getCommand(Piece.Down);
}

// call checkScore method when piece cannot move
checkScore();
return true;
}

private void checkScore ()
{
for (int i = 0; i <30; i++)
{
boolean scored = true;
for (int j = 0; j<15; j++)
if (table [j] == Color.white)
scored = false;

// increment of 10 points when scored
if (scored) {
score +=10;
moveDown(i);
i = i -1; // check row again
}
}
}

// moveDown method - copy colour from the row above
private void moveDown (int start)
{
for (int k = start; k<30; k ++)
for (int j = 0; j <15; j++)
if (k < 29)
table [j][k] = table [j][k+1];
else
table [j][k] = Color.white;
}

}

<Added>

Sorry... pls ignore the PieceMover class above.

----------------- PieceMover Class -----------
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
class PieceMover extends Thread {

private Tetris controller;
private Color table[][];
private Piece currentPiece;
private int score = 0;

// constructor - create new object
public PieceMover(Tetris t) {
controller = t;
table = new Color[15][30];
currentPiece = null;
newGame();
}

// thread starting point
public void run()
{ dropPiece(); }

// other methods
public void newGame() {
for (int k=0; k < 30; k++)
for (int j=0; j < 15; j++)
table[j][k] = Color.white;
}

// paint method - to draw interface
public void paint (Graphics g) {
for(int k=0; k<30; k++)
{
for(int j=0; j<15; j++)
{
g.setColor(table[j][k]);
g.fillRect(20+10*j, 350-10*k, 10, 10);
g.setColor(Color.blue);
g.drawString("Score" + score, 200, 200);
}
}
}

private boolean dropPiece() {
int piecetype = 1 + (int)(7* Math.random());
currentPiece = new Piece(piecetype);
if (! currentPiece.move(Piece.Down, table))
return false;
currentPiece.draw(table);
int command = controller.getCommand(Piece.Down);
currentPiece.draw(table);
while (currentPiece.move(command, table)) {
controller.repaint();
yield ();
try
{
sleep(100);
}
catch (InterruptedException e)
{
}
command = controller.getCommand(Piece.Down);
}

// call checkScore method when piece cannot move
checkScore();
return true;
}

private void checkScore ()
{
for (int k = 0; k <30; k++)
{
boolean scored = true;
for (int j = 0; j<15; j++)
if (table [j][k] == Color.white)
scored = false;

// increment of 10 points when scored
if (scored) {
score +=10;
moveDown(k);
k = k -1; // check row again
}
}
}

// moveDown method - copy colour from the row above
private void moveDown (int start)
{
for (int k = start; k<30; k ++)
for (int j = 0; j <15; j++)
if (k < 29)
table [j][k] = table [j][k+1];
else
table [j][k] = Color.white;
}

}

  Re: Game - illegal combination of modifiers  crwood at 20:08 on Sunday, April 30, 2006
 

Seems that the Piece types aren't able to be added to the grid because they are all hard–coded. It was easier to start over than to de/reconstruct your app.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;

public class T
{
public static void main(String[] args)
{
TetrisPanel tetrisPanel = new TetrisPanel();
TMover mover = new TMover(tetrisPanel);
tetrisPanel.addMouseListener(new TListener(mover));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(tetrisPanel);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class TetrisPanel extends JPanel
{
int rows = 15;
int cols = 30;
Color[][] grid;
Random seed;
final int PAD = 20;

public TetrisPanel()
{
grid = new Color[rows][cols];
seed = new Random();
clearGrid();
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new Color(200,220,240));
double w = getWidth();
double h = getHeight();
double xInc = (w-2*PAD)/cols;
double yInc = (h-2*PAD)/rows;
// draw grid
double x1 = PAD, y1 = PAD, x2 = w-PAD, y2 = h-PAD;
for(int row = 0; row <= rows; row++)
{
g2.draw(new Line2D.Double(x1, y1, x2, y1));
y1 += yInc;
}
y1 = PAD;
for(int col = 0; col <= cols; col++)
{
g2.draw(new Line2D.Double(x1, y1, x1, y2));
x1 += xInc;
}
// draw tokens
for(int row = 0; row < rows; row++)
{
x1 = PAD; y1 = PAD + row * yInc;
for(int col = 0; col < cols; col++)
{
g2.setPaint(grid[row][col]);
g2.fill(new Rectangle2D.Double(x1+1, y1+1, xInc-2, yInc-2));
x1 += xInc;
}
}
}

public void clearGrid()
{
for(int row = 0; row < grid.length; row++)
for(int col = 0; col < grid[0].length; col++)
grid[row][col] = Color.white;
repaint();
}

public void next()
{
int row = seed.nextInt(rows);
int col = seed.nextInt(cols);
int type = 1 + seed.nextInt(7);
//System.out.println("row = " + row + "\tcol = " + col +
// "\ttype = " + type);
addToken(row, col, type);
}

private void addToken(int row, int col, int type)
{
Color color = Color.white;
int[] x = new int[4], y = new int[4];
switch(type)
{
case 1:
x[0] = col+1;
y[0] = row;
x[1] = col;
y[1] = row;
x[2] = col;
y[2] = row+1;
x[3] = col-1;
y[3] = row+1;
color = Color.green;
break;
case 2:
x[0] = col;
y[0] = row;
x[1] = col;
y[1] = row-1;
x[2] = col;
y[2] = row-2;
x[3] = col;
y[3] = row-3;
color = Color.red;
break;
case 3:
x[0] = col;
y[0] = row;
x[1] = col-1;
y[1] = row;
x[2] = col-2;
y[2] = row;
x[3] = col-3;
y[3] = row;
color = Color.blue;
break;
case 4:
x[0] = col;
y[0] = row;
x[1] = col+1;
y[1] = row;
x[2] = col+2;
y[2] = row;
x[3] = col+1;
y[3] = row-1;
color = Color.black;
break;
case 5:
x[0] = col;
y[0] = row;
x[1] = col;
y[1] = row-1;
x[2] = col;
y[2] = row-2;
x[3] = col+1;
y[3] = row-2;
color = Color.yellow;
break;
case 6:
x[0] = col;
y[0] = row;
x[1] = col+1;
y[1] = row;
x[2] = col;
y[2] = row-1;
x[3] = col-1;
y[3] = row-1;
color = Color.orange;
break;
case 7:
x[0] = col;
y[0] = row;
x[1] = col;
y[1] = row-1;
x[2] = col+1;
y[2] = row-1;
x[3] = col+2;
y[3] = row-1;
color = Color.pink;
break;
default:
System.out.println("unspecified state: " + type);
}
if(isInBounds(x, y) && isSpaceClear(x, y))
{
for(int j = 0; j < x.length; j++)
grid[y[j]][x[j]] = color;
repaint();
}
}

private boolean isInBounds(int[] x, int[] y)
{
for(int j = 0; j < x.length; j++)
{
if(!isInBounds(x[j], y[j]))
return false;
}
return true;
}

private boolean isInBounds(int x, int y)
{
if(x < 0 || x > cols-1 || y < 0 || y > rows-1)
return false;
return true;
}

private boolean isSpaceClear(int[] x, int[] y)
{
for(int row = 0; row < y.length; row++)
for(int col = 0; col < x.length; col++)
if(grid[y[row]][x[col]] != Color.white)
return false;
return true;
}
}

class TListener extends MouseAdapter
{
TMover mover;

public TListener(TMover tm)
{
mover = tm;
}

public void mousePressed(final MouseEvent e)
{
if(e.getClickCount() > 1)
{
mover.stop();
((TetrisPanel)e.getSource()).clearGrid();
}
else if(mover.isRunning())
mover.stop();
else
mover.start();
}
}

class TMover implements Runnable
{
TetrisPanel tetrisPanel;
Thread mover;
boolean keepTrying;
final int delay = 220;

public TMover(TetrisPanel tp)
{
tetrisPanel = tp;
keepTrying = false;
}

public void run()
{
while(keepTrying)
{
try
{
Thread.sleep(delay);
}
catch(InterruptedException ie)
{
System.err.println("Mover interrupt: " + ie.getMessage());
}
tetrisPanel.next();
}
}

public void start()
{
if(!keepTrying)
{
keepTrying = true;
mover = new Thread(this);
mover.setPriority(Thread.NORM_PRIORITY);
mover.start();
}
}

public void stop()
{
keepTrying = false;
mover = null;
}

public boolean isRunning()
{
return keepTrying;
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Help !! -- Array Declaration --
•  loading external code
•  submit button
•  SQL stored procedure error
•  Needs Java script help
•  Problem with XML
•  RMI server problem
•  Re: help please for newbie to java and i/o
•  Help !! -- Array Declaration --


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006