eclipse Applet运行之后显示未初始化程序
程序如下:package javaApplet;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.JFrame;
public class HelloWorld extends Applet
implements MouseMotionListener{
Rectangle2D[]p=new Rectangle2D.Double[3];
Image buff;
Graphics2D ga,gb;
double dx,dy;
int selection=-1;
public void init(){
buff=createImage(getWidth(),getHeight());
gb=(Graphics2D)buff.getGraphics();
p[0]=new Rectangle2D.Double(150,50,5,5);
p[1]=new Rectangle2D.Double(60,200,5,5);
p[2]=new Rectangle2D.Double(240,200,5,5);
addMouseMotionListener(this);
addMouseListener( (MouseListener) this);
}
public void paint(Graphics g){
if(ga==null)
ga=(Graphics2D)getGraphics();
drawScreen();
}
public void drawScreen() {
// TODO Auto-generated method stub
gb.clearRect(0,0,getWidth(),getHeight());
GeneralPath 易做图=new GeneralPath();
易做图.moveTo((float)p[0].getX(),(float)p[0].getY());
易做图.lineTo((float)p[1].getX(),(float)p[1].getY());
易做图.lineTo((float)p[2].getX(),(float)p[2].getY());
易做图.closePath();
gb.setPaint(new Color(204,205,255));
gb.fill(易做图);
gb.setPaint(new Color(0,0,0));
gb.draw(易做图);
gb.setPaint(Color.red);
for(int i=0;i<p.length;i++)
gb.fill(p[i]);
gb.drawImage(buff, 0, 0, this);
}
public void mousePressed(MouseEvent me){
int mx=me.getX(),my=me.getY();
for(int i=0;i<p.length;i++)
if(p[i].contains(mx,my)){
dx=mx-(int)p[i].getX();dy=my-(int)p[i].getY();
selection=i;
break;
}
}
public void mouseDragged(MouseEvent me) {
// TODO Auto-generated method stub
if(selection>=0&&selection<=p.length){
int mx=me.getX(),my=me.getY();
p[selection].setFrame(mx-dx,my-dy,5,5);
drawScreen();
}
}
public void mouseReleased(MouseEvent me){
selection=-1;
}
public void mouseMoved(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseCilicked(MouseEvent me){}
public static void main(String[] args){
HelloWorld applet = new HelloWorld();
JFrame frame = new JFrame("PinTu");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(650,520);
applet.init();
applet.start();
frame.setVisible(true);
}
}
错误如下:
java.lang.ClassCastException: javaApplet.HelloWorld cannot be cast to java.awt.event.MouseListener
at javaApplet.HelloWorld.init(HelloWorld.java:26)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
请问哪里错了呢?能详解一下吗?
Applet,未初始化 --------------------编程问答-------------------- 使用事件的时候需要继承ActionListener --------------------编程问答-------------------- public class HelloWorld extends ActionListener Applet
implements MouseMotionListener这样吗
补充:Java , Eclipse