异常信息,一般是什么原因产生下面错误的呢?java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:306)
at java.lang.Class.newInstance(Class.java:259)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:567)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:496)
at sun.applet.AppletPanel.run(AppletPanel.java:293)
at java.lang.Thread.run(Thread.java:536)
载入:无法实例化 RussiaTetris.class。
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:306)
at java.lang.Class.newInstance(Class.java:259)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:567)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:496)
at sun.applet.AppletPanel.run(AppletPanel.java:293)
at java.lang.Thread.run(Thread.java:536)
载入:无法实例化 RussiaTetris.class。
/*
* Created on 2005-9-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author wanghs
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;public abstract class RussiaTetris extends Applet implements Runnable { Thread thread; private Image offImag;// buf for image private Graphics offG;//buf final int BaseX = 20; final int BaseY = 20; final int BlockSize = 20; //define all the imformations show in the Game
final String INFO_READY = "S key is the Start to play"; final String INFO_PAUSE = "P key is Continue to play"; final String INFO_GAMEOVER = "The game is OVER!!!"; byte SHAPE[][][][] = //[造型索引][旋转索引][4][坐标]
{ {//造型一 { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },//旋转1
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },//旋转2
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } },//旋转3
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } } //旋转4 }, {//造型二
{ { -1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 } },//旋转1
{ { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } },//旋转2
{ { -1, 0 }, { 0, 0 }, { 0, 1 }, { 1, 1 } },//旋转3
{ { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } } //旋转4 }, {//造型三
{ { 1, 0 }, { 0, 0 }, { 0, 1 }, { -1, 1 } },//旋转1
{ { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } },//旋转2
{ { 1, 0 }, { 0, 0 }, { 0, 1 }, { -1, 1 } },//旋转3
{ { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } } //旋转4 }, {//造型四
{ { 0, 0 }, { 1, 0 }, { 2, 1 }, { 0, 1 } },//旋转1
{ { 0, -2 }, { 0, -1 }, { 0, 0 }, { 1, 0 } },//旋转2
{ { -2, 0 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },//旋转3
{ { -1, 0 }, { 0, 0 }, { 0, 1 }, { 0, 2 } } //旋转4 }, {//造型五
{ { -2, 0 }, { -1, 0 }, { 0, 0 }, { 0, 1 } },//旋转1
{ { 0, 0 }, { 1, 0 }, { 0, 1 }, { 0, 2 } },//旋转2
{ { 0, -1 }, { 0, 0 }, { 1, 0 }, { 2, 0 } },//旋转3
{ { 0, -2 }, { 0, -1 }, { 0, 0 }, { -1, 0 } } //旋转4 }, {//造型六
{ { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } },//旋转1
{ { 0, -1 }, { 0, 0 }, { 0, 1 }, { 1, 0 } },//旋转2
{ { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },//旋转3
{ { -1, 0 }, { 0, 0 }, { 0, -1 }, { 0, 1 } } //旋转4 }, {//造型七
{ { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } },//旋转1
{ { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } },//旋转2
{ { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } },//旋转3
{ { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } } //旋转4 } }; // define all the values used in the game
final int[] Speeds = { 60, 50, 40, 30, 20, 10, 5, 1 };//speed byte[][] Ground = new byte[10][20];//the gound is 10*20 byte[][] NextShape = new byte[4][2];//store the next shape int CurrentX;//the current X int CurrentY;//the current Y int CurrentShapeIndex;//the current shape index int CurrentTurnIndex;//the current TurnIndex int CurrentColorIndex;//the current color index int NextShapeIndex;// int GameSpeed = 0;// int SpeedVar = 0;//the daly count int FlashSpeed = 60;//the infor flash speed int FlashVar = 0;//the flash daly count int ClearTotalLine = 0;//clear all the lines Color[] Colors = { Color.black, Color.red, Color.green, Color.blue,
Color.magenta, Color.yellow, Color.orange, Color.pink }; //define all the states in the Game
final int READY = 0; final int GAMING = 1; final int PAUSE = 2; final int GAMEOVER = 3; private int CurrentMode = READY; String InfoStr = new String(INFO_READY); /**
* @throws java.awt.HeadlessException
*/
public RussiaTetris() throws HeadlessException {
super();
System.out.print("test");
} public void init() {
offImag = createImage(getSize().width, getSize().height);
offG = offImag.getGraphics();
setBackground(Color.black);
} public void MakeNextShape() {
NextShapeIndex = (int) (Math.random() * 70) / 10;
NextShape = SHAPE[NextShapeIndex][0];
} public void ClearGround() {
for (int i = 0; i < 10; i++)
for (int j = 0; j < 20; j++)
Ground[i][j] = 0;
CurrentMode = READY;
ClearTotalLine = 0;
} public void StartGame() {
ClearGround();
CurrentShapeIndex = (int) (Math.random() * 70) / 10;
CurrentX = 4;
CurrentY = 0;
CurrentMode = GAMING;
CurrentColorIndex = (int) (Math.random() * 70) / 10 + 1;
MakeNextShape(); } public void Draw_A_Block(Graphics g, int x, int y, int colorIndex)//draw a
// block
{
g.setColor(Colors[colorIndex]);
g.fill3DRect(BaseX + x * BlockSize, BaseY + y * BlockSize, BlockSize,
BlockSize, true);
g.fill3DRect(BaseX + x * BlockSize + 1, BaseY + y * BlockSize + 1,
BlockSize - 2, BlockSize - 2, true); } public void drawShape(Graphics g, int x, int y, int shapeindex,
int trunindex)//set
// the
// shape
// of
// the
// block
{
for (int i = 0; i < 4; i++) {
if (y + SHAPE[shapeindex][trunindex][i][1] >= 0)//don't draw a
// block that
// isn't in the domain
{
Draw_A_Block(g, x + SHAPE[shapeindex][trunindex][i][0], y
+ SHAPE[shapeindex][trunindex][i][1], CurrentColorIndex);
}
} }
public void drawGround(Graphics g) {
g.setColor(new Color(255, 255, 255));
if (CurrentMode == READY)
g.drawString("+/-:set the speed ", BaseX, BaseY - 3);
if (CurrentMode == GAMING)
g.drawString("->/<-:shift,and |:trun ", BaseX, BaseY - 3);
g.drawRect(BaseX - 1, BaseY - 1, BlockSize * 10, BlockSize * 20);
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 20; j++) {
if (Ground[i][j] != 0)
Draw_A_Block(g, i, j, Ground[i][j]);
}
}
g.setColor(Color.lightGray);
g.drawRect(BaseX + BlockSize * 10, BaseY - 1, BlockSize * 2 + BlockSize
/ 2 + BlockSize / 2, 20);
//show all the related informations
g.drawString("TETRIS", BaseX + BlockSize * 10 + 8, BaseY + 14);
g.drawString("The number of shocking", BaseX + BlockSize * 10 + 5,
BaseY + 3);
g.drawString(String.valueOf(ClearTotalLine),
BaseX + BlockSize * 10 + 5, BaseY * 4);
g
.drawString("The current speed", BaseX + BlockSize * 10 + 5,
BaseY * 6);
g.drawString(String.valueOf(GameSpeed), BaseX + BlockSize * 10 + 5,
BaseY * 7);
g.drawString("[email protected]", BaseX, BaseY + BlockSize * 21
- 10);
} public void drawNextBlock(Graphics g) {
g.setColor(Color.lightGray);
g.drawString("Next Block", BaseX + 11 * BlockSize - BlockSize / 2,
BaseY + 17 * BlockSize);
g.drawRect(BaseX + BlockSize * 10, BaseY + BlockSize * 18 - BlockSize
/ 2, BlockSize * 2 + BlockSize / 2, BlockSize * 20);
g.setColor(Color.blue);
for (int i = 0; i < 4; i++) {
g.fill3DRect(BaseX + 11 * BlockSize + NextShape[i][0] * BlockSize
/ 2, BaseY + 18 * BlockSize + NextShape[i][1] * BlockSize
/ 2, BlockSize / 2, BlockSize / 2, true);
}
} public void drawInfo(Graphics g) {
g.setColor(Color.white);
Font old = g.getFont();
g.setFont(new Font("宋体", 0, 24));
g.drawString(InfoStr, BaseX + 5 * BlockSize - InfoStr.length() * 7,
BaseY + 10 * BlockSize);
g.setFont(old);
} public void paint(Graphics g) {
offG.clearRect(0, 0, getSize().width, getSize().height);
drawGround(offG);
switch (CurrentMode) {
case READY:
case PAUSE:
case GAMEOVER:
drawInfo(offG);
break;
case GAMING:
drawShape(offG, CurrentX, CurrentY, CurrentShapeIndex,
CurrentTurnIndex);
drawNextBlock(offG);
break;
}
if (offG != null)
g.drawImage(offImag, 0, 0, this);
} public void update(Graphics g) {
paint(g);
} public void start() {
thread = new Thread(this);
thread.start();
} public void stop() {
thread = null;
} public void run() {
Thread current = Thread.currentThread();
while (thread == current) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
} if (CurrentMode == GAMING) {
if (SpeedVar++ > Speeds[GameSpeed]) {
SpeedVar = 0;
DownIt();
repaint();
} else if (FlashVar++ > FlashSpeed) {
FlashVar = 0;
switch (CurrentMode) {
case READY:
if (InfoStr.equals(""))
InfoStr = INFO_READY;
else
InfoStr = "";
break;
case PAUSE:
if (InfoStr.equals(""))
InfoStr = INFO_PAUSE;
else
InfoStr = "";
break;
case GAMEOVER:
if (InfoStr.equals(""))
InfoStr = INFO_GAMEOVER;
else
InfoStr = "";
break;
}
repaint();
}
}
}
} public boolean CanMoveTo(int shape, int turn, int x, int y) {
boolean result = true;
turn %= 4;
for (int i = 0; i < 4; i++) {
if ((x + SHAPE[shape][turn][i][0] < 0)
|| (x + SHAPE[shape][turn][i][0] > 9)) {
result = false;
break;
} if (y + SHAPE[shape][turn][i][1] > 19) {
result = false;
break;
}
if (Ground[x + SHAPE[shape][turn][i][0]][y
+ SHAPE[shape][turn][i][1]] != 0) {
result = false;
break;
}
}
return result;
}
public synchronized void DownIt() {
if (CanMoveTo(CurrentShapeIndex, CurrentTurnIndex, CurrentX,
CurrentY + 1)) {
CurrentY++;
} else {//the block has gone to the bottem
if (CurrentY == 0)
CurrentMode = GAMEOVER;
else { //put the current block into the Ground
for (int i = 0; i < 4; i++)
Ground[CurrentX
+ SHAPE[CurrentShapeIndex][CurrentTurnIndex][i][1]][CurrentY
+ SHAPE[CurrentShapeIndex][CurrentTurnIndex][i][1]] = (byte) CurrentColorIndex;
CurrentX = 4;
CurrentY = 0;
CurrentShapeIndex = NextShapeIndex;
CurrentColorIndex = (int) (Math.random() * 70) / 10 + 1;
MakeNextShape();
CurrentTurnIndex = 0;
//check whether there is a line of block
int total;
for (int y = 19; y >= 0; y--) {
total = 0;
for (int x = 0; x < 10; x++)
if (Ground[x][y] != 0)
total++;
if (total == 10) {
for (int i = 0; i < 10; i++)
for (int j = y; j > 0; j--)
Ground[i][j] = Ground[i][j - 1];
y++;
ClearTotalLine++;
if (ClearTotalLine % 50 == 0) {
GameSpeed++;
if (GameSpeed > 9)
GameSpeed = 9;
} } }//end of For
}//of else
}//of else
} public boolean keyDown(Event e, int Key) {
switch (CurrentMode) {
case READY:
if (Key == Integer.parseInt("s") || Key == Integer.parseInt("S"))
StartGame();
if (Key == Integer.parseInt("+")) {
GameSpeed++;
if (GameSpeed > 9)
GameSpeed = 9;
}
if (Key == Integer.parseInt("-")) {
GameSpeed--;
if (GameSpeed < 0)
GameSpeed = 0;
}
break;
case PAUSE:
if (Key == Integer.parseInt("p") || Key == Integer.parseInt("P"))
CurrentMode = GAMING;
break;
case GAMEOVER:
CurrentMode = READY;//any key for ready
break;
case GAMING:
if (Key == Integer.parseInt("p") || Key == Integer.parseInt("P"))
CurrentMode = PAUSE;
if (Key == Integer.parseInt("r") || Key == Integer.parseInt("R"))
CurrentMode = READY;
if (Key == Event.LEFT
&& CanMoveTo(CurrentShapeIndex, CurrentTurnIndex,
CurrentX - 1, CurrentY))
CurrentX--;
if (Key == Event.RIGHT
&& CanMoveTo(CurrentShapeIndex, CurrentTurnIndex,
CurrentX + 1, CurrentY))
CurrentX++;
if (Key == Event.UP
&& CurrentY > 1
&& CanMoveTo(CurrentShapeIndex, CurrentTurnIndex + 1,
CurrentX, CurrentY)) {
CurrentTurnIndex++;
CurrentTurnIndex %= 4;
}
if (Key == Event.DOWN)
DownIt();
repaint();
break;
}
return true;
}}