当前位置:编程学习 > JAVA >>

我的主程序界面出不来,求帮忙!!

我在一本书上学习java的俄罗斯方块,敲了里面的代码,可以主程序的界面出不了,主程序的画布没有显示那该有的功能!建位兄弟指点一下具体是错在那了。代码附上,我是在ecplise上运行的。大谢!



请大伙看看,控制面板的画布实现得了,游戏区的的画布就行了!!
--------------------编程问答-------------------- 方块类:
import java.awt.*;
//the class ErsBox is the primitive implement that built
//the ErsBlock,which is represented with color.
public class ErsBox implements Cloneable{
private boolean isColor;
private Dimension size = new Dimension();
/**
 * the constructor of the class ErsBox.
 * @param isColor represent if use the previous-colors color the 
 *        the box or not , true for previous-colors and false for
 *        back-color.
 */
public ErsBox(boolean isColor){
this.isColor = isColor;

}
/**
 * if the box is represented by the previous-color.
 * @return  boolean ,true for using previous-color,
 *                   false for using back-color.
 */
public boolean isColorBox(){
return isColor;

}
/**
 * set the color of the box.
 * @param isColor boolean,true for using previous-color as representation.
 *    false for back-color.
 */
public void setColor(boolean isColor){
this.isColor = isColor;
}
/**
 * get the size of the box.
 * @return Dimension,the size of the box.
 */
public Dimension getSize(){
return size;
}
/**
 * set the size of the box.
 * @param size Dimension,the size of the box.
 */
public void setSize(Dimension size){
this .size = size;
}
/**
 * cover the method of the class Object,Object clone().
 * to create the clone.
 * @return Object,the result of the clone.
 */
public Object clone(){
Object cloned = null;
try
{
cloned = super.clone();
}catch (Exception ex)
{
ex.printStackTrace();
}
return cloned;
}

}


--------------------编程问答-------------------- import java.awt.*;
//the class ErsBox is the primitive implement that built
//the ErsBlock,which is represented with color.
public class ErsBox implements Cloneable{
private boolean isColor;
private Dimension size = new Dimension();
/**
 * the constructor of the class ErsBox.
 * @param isColor represent if use the previous-colors color the 
 *        the box or not , true for previous-colors and false for
 *        back-color.
 */
public ErsBox(boolean isColor){
this.isColor = isColor;

}
/**
 * if the box is represented by the previous-color.
 * @return  boolean ,true for using previous-color,
 *                   false for using back-color.
 */
public boolean isColorBox(){
return isColor;

}
/**
 * set the color of the box.
 * @param isColor boolean,true for using previous-color as representation.
 *    false for back-color.
 */
public void setColor(boolean isColor){
this.isColor = isColor;
}
/**
 * get the size of the box.
 * @return Dimension,the size of the box.
 */
public Dimension getSize(){
return size;
}
/**
 * set the size of the box.
 * @param size Dimension,the size of the box.
 */
public void setSize(Dimension size){
this .size = size;
}
/**
 * cover the method of the class Object,Object clone().
 * to create the clone.
 * @return Object,the result of the clone.
 */
public Object clone(){
Object cloned = null;
try
{
cloned = super.clone();
}catch (Exception ex)
{
ex.printStackTrace();
}
return cloned;
}

} --------------------编程问答-------------------- 俄罗斯方块类:
public class ErsBlock extends Thread {
// one EerBox occupy four rows.
public final static int BOXES_ROWS = 4;
//one EerBox occupy four columns.
public final static int BOXES_COLS = 4;
//
public final static int LEVEL_FLATNESS_GENE = 3;
//
public final static int BETWEEN_LEVELS_DEGRESS_TIME = 50;
//7 style of the block
private final static int BLOCK_KIND_NUMBER = 7;
//4 status of each kinds of the block.
private final static int BLOCK_STATUS_NUMBER = 4;
public static final int[][] STYLES = {
{0x0f00,0x4444,0x0f00,0x4444},//four situation of rectangle-sharp.
{0x04e0,0x0464,0x00e4,0x04c4},//four situation of T-sharp.
{0x4620,0x6c00,0x4620,0x6c00},//four situation of anti-Z-sharp.
{0x2640,0xc600,0x2640,0xc600},//four situation of Z-sharp.
{0x6220,0x1700,0x2230,0x0740},//four situation of 7-sharp.
{0x6440,0x0e20,0x44c0,0x8e00},//four situation of anti-7-sharp.
{0x0660,0x0660,0x0660,0x0660},//four situation of block-sharp.
};

//the canvas which the ErsBlock in.
private GameCanvas  canvas ; 
private ErsBox[][] boxes = new ErsBox[BOXES_ROWS][BOXES_COLS];
private int style,y,x,level;
private boolean pausing = false, moving = true;
/**
 * the constructor ,create a specify ErsBlock.
 * @param style ,the style of the window
 * @param y,row location
 * @param x,column location
 * @param level,the level of the game.
 * @param canvas,the canvas which the ErsBlock in.
 */
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();
}
    /**
     * a cover of the method run() in closs Thread.move the ErsBlock down
     * till it can not move anymore.
     */
public void run(){
while( moving ){
try
{
sleep( BETWEEN_LEVELS_DEGRESS_TIME * 
(ErsBlocksGame.MAX_LEVEL - level
+ LEVEL_FLATNESS_GENE)
);
}catch (InterruptedException ie)
{
ie.printStackTrace();
}
/**
 * moving represented that in the waiting time ,that
 * the moving have not been changed.
 */
if( !pausing) moving = (moveTo( y+1,x) && moving);
}

}
/**
 * move the ErsBlock one step to left.
 */
public void moveLeft(){
moveTo(y,x-1);
}
/**
 * move the ErsBlock one step to right.
 */
public void moveRight(){
moveTo(y,x+1);
}
/**
 * move the ErsBlock one step down
 */
public void moveDown(){
moveTo(y+1,x);
}
/**
 * change the form of ErsBlock'sharp.
 */
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;
}
}
}
}
/**
 * pause the slipping of the ErsBlock ,that is to say
 * that pause the game
 */
public void pauseMove(){
pausing = true;
}
/**
 * resume the move of the ErsBlock,that is to Proceed the game.
 */
public void resumeMove(){
pausing = false;
}
/**
 * stop the down-move of the ErsBlock,represented that stop the game.
 */
public void stopMove(){
moving = false;

}
/**
 * erase the ErsBlock from the canvas,and that will be show when
 * the canvas rebuilt next time.
 */
private void erase(){
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);
}
}
}

}
/**
 * if the present ErsBlock can move the the location (newRow, newCol).
 * @param newRow int ,the row that the ErsBlock intend to move.
 * @param newCol int ,the column that the ErsBlock intend to move.
 * @return  boolean, true for can move ,and false negative.
 */
private boolean isMoveAble(int newRow, int newCol){
erase();
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);
/**
 * it cannot make the movement ,if the present location 
 * is not a ErsBlock ,or there is another already exist.
 */
if ( box==null || (box.isColorBox())){
display();
return false;
}
}
}
}
display();
return true;
}
private boolean turnTo(int newStyle) {
if ( !isTurnAble(newStyle) || !moving) return false;
erase();
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;
}

/**
 * move the ErsBlock to the intended position.
 * @param newRow int ,the row location that intend to move.
 * @param newCol int ,the column location that intend to move.
 * @return  boolean,true for moving to the new position successfully
 *                  false for failure.
 *                  
 */
//the word synchronized indicated  that the method is  used by one thread only,
//it locked the method to make sure of that by the system automatically.
private synchronized boolean moveTo(int newRow, int newCol) {
/**
 * return failure if the new position is unable to move or the moving is false.
 */
if(!isMoveAble(newRow, newCol) || !moving) return false;
/**
 * erase the passed sharp.
 */
erase();
// rebuilt the canvas in a new position.
y = newRow;
x = newCol;
display();
canvas.repaint();
return true;

}
/**
 * display the ErsBlock at the present canvas ,that will be show
 * when the canvas rebuilt next time.
 */
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);
}
}
}

}

/**
 * test that the present ErsBlock can transform to the new style that indicated by the 
 * newStyle or not,Basically consider the situations of bounder or blocked by others that
 * can not move anymore.
 * @param newStyle int, the style intended ,is one of the 28 forms in the STYLE.
 * @return boolean,true for changeable ,and false negitive.
 */
private  boolean isTurnAble (int newStyle){
int key = 0x8000;
erase();
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;
}
}
}
}
display();
return true;

}



} --------------------编程问答--------------------
补充:Java ,  Eclipse
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,