这只是一道多线程的作业题,要求编写一个在apple内部显示一个蓝色反弹球的程序。应通过一个mousePressed使该球开始运动。在该球撞击applet边框时,它应从边框弹回并以相反的方向运动。
以下是我写的代码:
——————————————————————————————————————————
import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class Ball extends JApplet implements Runnable
{
private boolean upX,upY,bouncing,direction[];
private Thread blueBall;
private int x,y,dx,dy;
private final int maxX=200,maxY=200; public void init()
{
direction=new boolean[]{ true,false };
bouncing=false;
addMouseListener(
new MouseListener()
{
public void mousePressed( MouseEvent event )
{
createBall( event ); } public void mouseClicked( MouseEvent event ){}
public void mouseReleased( MouseEvent event ){}
public void mouseEntered( MouseEvent event ){}
public void mouseExited( MouseEvent event ){} } //end MouseListener );//end addMouseListener setSize(maxX,maxY); }//end init public void createBall ( MouseEvent event )
{
if(blueBall==null)
{
x=event.getX();
y=event.getY();
int n=(int)( Math.random()*2 );
upX=direction[n];
n=(int)( Math.random()*2 );
upY=direction[n];
dx=(int)( Math.random()*5+2 );
dy=(int)( Math.random()*5+2 );
blueBall=new Thread( this );
bouncing=true;
blueBall.start(); }//end if }//end createBall public void stop()
{
blueBall=null; } public void paint( Graphics g )
{
super.paint(g);
if( bouncing==true )
{
g.setColor( Color.BLUE );
g.fillOval( x,y,10,10 ); } }//end paint public void run()
{
while( true )
{
try
{
blueBall.sleep( 20 ); }//end try catch( InterruptedException exception )
{
System.err.println( exception.toString() ); } if( upX==true )
x+=dx;
else
x-=dx; if( upY==true )
y+=dy;
else
y-=dy; if( x<=0||x>=maxX-10 )
{
upX=!upX;
dx=(int)( Math.random()*5+2 ); }//end if
if( y<=0||y>=maxY-10 )
{
upY=!upY;
dy=(int)( Math.random()*5+2 ); }//end if repaint(); }//end while }//end run}
______________________________________________________________________________________
现在的问题是我用IE浏览器(我只有这个浏览器)打开该Apple的HTML文档时下面的提示是:
小应用程序Ball notinited。画面只有一个X,如果把鼠标放入显示框时,下面的提示就会变为:
载入JAVA小应用程序失败。我想请问各位是什么原因?另外打开其他JApplet的HTML没遇到这种问题。
以下是我写的代码:
——————————————————————————————————————————
import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class Ball extends JApplet implements Runnable
{
private boolean upX,upY,bouncing,direction[];
private Thread blueBall;
private int x,y,dx,dy;
private final int maxX=200,maxY=200; public void init()
{
direction=new boolean[]{ true,false };
bouncing=false;
addMouseListener(
new MouseListener()
{
public void mousePressed( MouseEvent event )
{
createBall( event ); } public void mouseClicked( MouseEvent event ){}
public void mouseReleased( MouseEvent event ){}
public void mouseEntered( MouseEvent event ){}
public void mouseExited( MouseEvent event ){} } //end MouseListener );//end addMouseListener setSize(maxX,maxY); }//end init public void createBall ( MouseEvent event )
{
if(blueBall==null)
{
x=event.getX();
y=event.getY();
int n=(int)( Math.random()*2 );
upX=direction[n];
n=(int)( Math.random()*2 );
upY=direction[n];
dx=(int)( Math.random()*5+2 );
dy=(int)( Math.random()*5+2 );
blueBall=new Thread( this );
bouncing=true;
blueBall.start(); }//end if }//end createBall public void stop()
{
blueBall=null; } public void paint( Graphics g )
{
super.paint(g);
if( bouncing==true )
{
g.setColor( Color.BLUE );
g.fillOval( x,y,10,10 ); } }//end paint public void run()
{
while( true )
{
try
{
blueBall.sleep( 20 ); }//end try catch( InterruptedException exception )
{
System.err.println( exception.toString() ); } if( upX==true )
x+=dx;
else
x-=dx; if( upY==true )
y+=dy;
else
y-=dy; if( x<=0||x>=maxX-10 )
{
upX=!upX;
dx=(int)( Math.random()*5+2 ); }//end if
if( y<=0||y>=maxY-10 )
{
upY=!upY;
dy=(int)( Math.random()*5+2 ); }//end if repaint(); }//end while }//end run}
______________________________________________________________________________________
现在的问题是我用IE浏览器(我只有这个浏览器)打开该Apple的HTML文档时下面的提示是:
小应用程序Ball notinited。画面只有一个X,如果把鼠标放入显示框时,下面的提示就会变为:
载入JAVA小应用程序失败。我想请问各位是什么原因?另外打开其他JApplet的HTML没遇到这种问题。
<OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="500" height="500" align="baseline" codebase="http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#Version=1,2,2,0">
<PARAM NAME="code" VALUE="Ball.class">
<PARAM NAME="codebase" VALUE=".">
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2">
<COMMENT>
<EMBED type=
"application/x-java-applet;version=1.2.2"
width="500" height="500" align="baseline"
code="Ball.class" codebase="."
pluginspage="http://java.sun.com/products/plugin/1.2/plugin-install.html">
<NOEMBED>
No Java 2 support for APPLET!!
</NOEMBED>
</EMBED>
</COMMENT>
</OBJECT></body></html>