代码如下,编译出现提示,但能通过,运行出现错误
package playgame;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
interface CharacterSource{
public void addCharacterListener(CharacterListener cl);
public void removeCharacterListener(CharacterListener cl);
public void nextCharacter();
}
interface CharacterListener{
public void newCharacter(CharacterEvent ce);
}
class CharacterEvent{
public CharacterSource source;
public int character;
public CharacterEvent(CharacterSource cs,int c){
source=cs;
character=c;
}
}
class CharacterEventHandler{
private Vector listeners=new Vector();
public void addCharacterListener(CharacterListener cl){
listeners.add(cl);
}
public void removeCharacterListener(CharacterListener cl){
listeners.remove(cl);
}
public void fireNewCharacter(CharacterSource source,int c){
CharacterEvent ce=new CharacterEvent(source,c);
CharacterListener[] cl=(CharacterListener[] )listeners.toArray(new CharacterListener[0]);
for(int i=0;i <cl.length;i++){
cl[i].newCharacter(ce);
}
}
}
class CharacterDisplayCanvas extends JComponent implements CharacterListener{
protected FontMetrics fm;
protected char[] tmpChar=new char[1];
protected int fontHeight;
public CharacterDisplayCanvas(){
setFont(new Font( "Monospaced ",Font.BOLD,18));
fm=Toolkit.getDefaultToolkit().getFontMetrics(getFont());
fontHeight=fm.getHeight();
}
public CharacterDisplayCanvas(CharacterSource cs){
this();
setCharacterSource(cs);
}
public void setCharacterSource(CharacterSource cs){
cs.addCharacterListener(this);
}
public Dimension preferredsize(){
return new Dimension(fm.getMaxAscent()+10,fm.getMaxAdvance()+10);
}
public synchronized void newCharacter(CharacterEvent ce){
tmpChar[0]=(char)ce.character;
repaint();
}
protected synchronized void paintComponent(Graphics gc){
Dimension d=getSize();
gc.clearRect(0,0,d.width,d.height);
if(tmpChar[0]==0)return;
int charWidth=fm.charWidth((int)tmpChar[0]);
gc.drawChars(tmpChar,0,1,(d.width-charWidth)/2,fontHeight);
}
}
class RandomCharacterGenerator extends Thread implements CharacterSource{
static char[] chars;
static String charArray= "abcdefghijklmnopqrstuvwxyz ";
static{
chars=charArray.toCharArray();
}
Random random;
CharacterEventHandler handler;
public RandomCharacterGenerator(){
random=new Random();
handler=new CharacterEventHandler();
}
public int getPauseTime(){
return (int)(Math.max(1000,5000*random.nextDouble()));
}
public void addCharacterListener(CharacterListener cl){
handler.addCharacterListener(cl);
}