Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在浏览器中将Java类作为小程序运行_Java_Browser_Embed_Japplet - Fatal编程技术网

如何在浏览器中将Java类作为小程序运行

如何在浏览器中将Java类作为小程序运行,java,browser,embed,japplet,Java,Browser,Embed,Japplet,我有一个弹跳球类,它扩展了GraphicsProgram。我希望能够嵌入到我的博客。根据帖子,我可以使用JavaWebStart来实现这个目的。但我是编程新手,我的程序非常小。所以我想继续使用Javaapplet。但是要使用JavaApplet,我需要将类扩展为JApplet。正如我提到的,我的bouncingball类扩展了GraphicsProgram。所以我不能将其用作Java小程序。 要确保任何类都可以在浏览器中作为Java小程序实现和运行,具体需要什么 /** * This prog

我有一个弹跳球类,它扩展了GraphicsProgram。我希望能够嵌入到我的博客。根据帖子,我可以使用JavaWebStart来实现这个目的。但我是编程新手,我的程序非常小。所以我想继续使用Javaapplet。但是要使用JavaApplet,我需要将类扩展为JApplet。正如我提到的,我的bouncingball类扩展了GraphicsProgram。所以我不能将其用作Java小程序。 要确保任何类都可以在浏览器中作为Java小程序实现和运行,具体需要什么

/**
 * This program graphically simulates a bouncing ball.
 */

package Lecture_Examples;

import acm.graphics.GOval;
import acm.program.GraphicsProgram;

public class BouncingBall extends GraphicsProgram{

    private static final long serialVersionUID = -1154542047805606083L;

    /**
     * Diameter of the ball
     */
    private static final int DIAM_BALL = 30;

    /**
     * Amount of velocity to be increased as a result of gravity
     */
    private static final int GRAVITY = 3;

    /**
     * Animation delay or pause between the ball moves
     */
    private static final int DELAY = 50;

    /**
     * Initial x and  y pos of the ball
     */
    private static final int X_START = DIAM_BALL /2;
    private static final int Y_START = DIAM_BALL /2;

    /**
     * X velocity
     */
    private static final double X_VEL = 5;

    /**
     * Amount of Y velocity reduction when the ball bounces
     */
    private static final double BOUNCE_REDUCE = 0.9;

    /**
     * starting x and y velocities
     */
    private double xVel = X_VEL;
    private double yVel = 0.0;  //starting from rest

    /*
     * ivar
     */
    private GOval ball;

    public void run(){
        setUp();    //set's up initial pos of the ball

        //simulation ends when the ball goes off right hand of the screen
        while(ball.getX() < getWidth()){
            moveBall();
            checkForCollision();
            pause(DELAY);
        }
    }
    /**
     * Checks if the ball touches the floor of the canvas and then update velocity and position of the ball
     */
    private void checkForCollision() {
        //determine if the ball has dropped below the floor
        if(ball.getY() > (getHeight() - DIAM_BALL)){
            //Change the ball's y velocity to bounce it up the floor with bounce_reduce effect
            yVel = -yVel *BOUNCE_REDUCE;

            //assuming that the ball will move an amount above the floor
            //equal to the amount  it would have dropped below the floor. This will
            //sure that the ball doesn't bulge to the floor while bouncing.
            double diff = ball.getY() - (getHeight() - DIAM_BALL);
            ball.move(0, -2*diff);
        }
    }

    /**
     * Moves the ball
     */

    private void moveBall() {
        yVel += GRAVITY;    //Add gravity to produce moment to or away from the floor
        ball.move(xVel, yVel);
    }

    /**
     *Creates and place the ball
     */
    private void setUp() {
        ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);   //Creates teh ball
        ball.setFilled(true);   //fill black
        add(ball);  //add to canvas
    }
}
/**
*该程序以图形方式模拟弹跳球。
*/
包装讲座和例子;
导入acm.graphics.GOval;
导入acm.program.GraphicsProgram;
公共类BouncingBall扩展图形程序{
私有静态最终长serialVersionUID=-1154542047805606083L;
/**
*球的直径
*/
私人静态最终内径球=30;
/**
*由于重力而增加的速度量
*/
私人静态最终整数重力=3;
/**
*球移动之间的动画延迟或暂停
*/
专用静态最终整数延迟=50;
/**
*球的初始x和y位置
*/
私人静态最终整数X_开始=直径球/2;
私人静态最终整数Y_开始=直径球/2;
/**
*X速度
*/
专用静态最终双X_等级=5;
/**
*球反弹时Y速度减少的量
*/
私人静态最终双反弹=0.9;
/**
*起始x和y速度
*/
专用双X电平=X_电平;
private double yVel=0.0;//从rest开始
/*
*伊瓦尔
*/
私人政府舞会;
公开募捐{
setUp();//设置球的初始位置
//当球离开屏幕右侧时,模拟结束
while(ball.getX()(getHeight()-DIAM_ball)){
//更改球的y速度,使其在地板上反弹,并产生“反弹”效果
yVel=-yVel*反弹\u减少;
//假设球将在地板上方移动一定量
//等于它掉到地板下的量。这将
//确保球在弹跳时不会凸起到地板上。
double diff=ball.getY()-(getHeight()-直径球);
球移动(0,-2*diff);
}
}
/**
*移动球
*/
私有void moveBall(){
yVel+=重力;//添加重力以产生到地板或远离地板的力矩
球。移动(xVel,yVel);
}
/**
*创建并放置球
*/
私有无效设置(){
ball=新的GOval(X_开始,Y_开始,直径球,直径球);//创建球
ball.setFilled(true);//填充黑色
添加(球);//添加到画布
}
}

您可以从
JApplet
再创建一个扩展类,并在这个新创建的类中使用上述类。您可以使用这个类创建并启动一个applet。我将上面的类嵌套在一个扩展JApplet的类中,但是我没有得到一个反弹。它只是打开了一个小程序。我必须指定类的运行顺序吗?嵌套类显示的是一个空白屏幕,而不是弹跳球。是否只有上层类(hello类)运行而不是弹跳球?