aspose file tools*
The moose likes Java in General and the fly likes inner class and this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "inner class and this" Watch "inner class and this" New topic
Author

inner class and this

ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I have a question concerning inner class and "this" qualifier. I got the following message from JVM. What does it mean and what should I do about it?
^
Tetris.java:164: shape is inherited from Tetris.Shape and hides variable in outer class Tetris. An explicit 'this' qualifier must be used to select the desired instance.
shape[0][2]=new Cell(pos+2, ht);
Thanks.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Got some code to go with this??


"JavaRanch, where the deer and the Certified play" - David O'Meara
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167

class Tetris extends Applet {
Cell[][] wholeBlocs;
int score;
Shape shape;
Color color[]={Color.red, Color.green, Color.blue, Color.yellow, Color.pink};
Tetris(int w, int h){
wholeBlocs = new Cell[w][h];
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
wholeBlocs[i][j].setX(i);
wholeBlocs[i][j].setY(j);
}
}
}
public void init(){
Panel p = new Panel();
p.setLayout(new GridLayout(wholeBlocs.length, wholeBlocs[0].length));
for(int i = 0; i < wholeBlocs.length; i++){
for(int j = 0; j < wholeBlocs[0].length; j++){
p.add(wholeBlocs[i][j]);
}
}
add(p);
}
class Cell extends Applet{
int x;
int y;
boolean on;
Color color;
Cell(){};
Cell(int x, int y){
this.x = x;
this.y = y;
color = Color.black;
}

public void init(){setBackground(Color.black);};
public void paint(Graphics g){
setBackground(color);
}
public boolean equals(Cell aCell){
return (x==aCell.getX())&&(y==aCell.getY());
}
public void setColor(Color color){
this.color=color;
}
public void setX(int xn){
x = xn;
}
public void setY(int yn){
y = yn;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void off(){
on = false;
}
public void on(){
on = true;
}
public boolean isOn(){
return on;
}
}
class Shape{
int rotateTimes;
int pos, ht;
Cell shape[][];
Cell blocs[];
Shape(){};
Shape(int p, int h){
blocs=null;
pos=p;
ht=h;
}
public void turnOff(){
for(int i = 0; i < blocs.length; i++){
blocs[i].setColor(Color.black);
blocs[i].repaint();
}
}
public void paint(Color color){
for(int i = 0; i < blocs.length; i++){
blocs[i].setColor(color);
blocs[i].repaint();
}
}
public boolean ifTouch(){ }
Cell[] generalRotate(int type){return null;};
public void rtMoveBloc(){
//TO BE OVERRIDDEN IN EACH SHAPE
}
public void ltMoveBloc(int type){
}
public void setPos(int p){
}
public void setHt(int h){
}
public void rtRotateBloc(){
}
public void ltRotateBloc(){
}
public void moveDown(){
}
}
class ShapeA extends Shape{ //Shape A

Cell[] generalRotate(int type){
switch(type){
case 0:{
shape[0] = new Cell[4];
shape[0][0]=new Cell(pos, ht);
shape[0][1]=new Cell(pos+1, ht);
shape[0][2]=new Cell(pos+2, ht);
shape[0][3]=new Cell(pos, ht-1);
return shape[0];
}
case 1:{
shape[1]=new Cell[4];
shape[1][0]=new Cell(pos+1, ht-1);
shape[1][1]=new Cell(pos+1, ht);
shape[1][2]=new Cell(pos+1, ht+1);
shape[1][3]=new Cell(pos, ht-1);
return shape[1];
}
case 2:{
shape[2]=new Cell[4];
shape[2][0]=new Cell(pos+1+1, ht-1+1);
shape[2][1]=new Cell(pos+1, ht);
shape[2][2]=new Cell(pos+2-1-1, ht);
shape[2][3]=new Cell(pos+2, ht-1);
return shape[2];
}
case 3:{
shape[3]=new Cell[4];
shape[3][0]=new Cell(pos+1+1-1, ht-1+1+1);
shape[3][1]=new Cell(pos+1, ht);
shape[3][2]=new Cell(pos+2-1-1+1, ht-1);
shape[3][3]=new Cell(pos+2, ht+1);
return shape[3];
}
}
return null;
}

ShapeA(int p, int h){
super(p, h);
blocs = generalRotate(rotateTimes%4);
}
public void rtMoveBloc(){
}//end of class
class ShapeB extends Shape{
Cell[] generalRotate(int type){
switch(type%2){
case 0:{
shape[0] = new Cell[4];
shape[0][0]=new Cell(pos, ht);
shape[0][1]=new Cell(pos+1, ht);
shape[0][2]=new Cell(pos+2, ht);
shape[0][3]=new Cell(pos+3, ht);
return shape[0];
}
case 1:{
shape[1]=new Cell[4];
shape[1][0]=new Cell(pos+2, ht-1);
shape[1][1]=new Cell(pos+2, ht);
shape[1][2]=new Cell(pos+2, ht+1);
shape[1][3]=new Cell(pos+2, ht+2);
return shape[1];
}
}
return null;
}
ShapeB(int p, int h){
pos = p;
ht = h;
blocs = generalRotate(rotateTimes%4);
}
public void rtMoveBloc(){
}//end of class
class ShapeC extends Shape{
Cell[] generalRotate(int type){
shape[0] = new Cell[4];
shape[0][0]=new Cell(pos, ht-1);
shape[0][1]=new Cell(pos+1, ht-1);
shape[0][2]=new Cell(pos, ht);
shape[0][3]=new Cell(pos+1, ht);
return shape[0];
}
ShapeC(int p, int h){
pos = p;
ht = h;
blocs = generalRotate(rotateTimes%4);
}
public void rtMoveBloc(){
}//end of class
class ShapeD extends Shape{
Cell[] generalRotate(int type){
switch(type%2){
case 0:{
shape[0] = new Cell[4];
shape[0][0]=new Cell(pos, ht-1);
shape[0][1]=new Cell(pos, ht);
shape[0][2]=new Cell(pos+1, ht);
shape[0][3]=new Cell(pos+1, ht+1);
return shape[0];
}
case 1:{
shape[1]=new Cell[4];
shape[1][0]=new Cell(pos, ht);
shape[1][1]=new Cell(pos+1, ht);
shape[1][2]=new Cell(pos+1, ht-1);
shape[1][3]=new Cell(pos+2, ht-1);
return shape[1];
}
}
return null;
}
ShapeD(int p, int h){
pos = p;
ht = h;
blocs = generalRotate(rotateTimes%4);
}
public void rtMoveBloc(){
}
public void placeBlocs(){
}
void randomRelease(){
int type = (int)(Math.random()*10%4);
int pos = (int)(Math.random()*10);
switch(type){
case 0:{
shape = new ShapeA(pos, 0);
}
break;
case 1:{
shape = new ShapeB(pos, 0);
}
break;
case 2:{
shape = new ShapeC(pos, 0);
}
break;
case 3:{
shape = new ShapeD(pos, 0);
}
break;
}
placeBlocs();
}
}
Hi, skeletal code is still quite long. But I think the heirachy is clear. Thanks for any help!
 
Consider Paul's rocket mass heater.
 
subject: inner class and this