//Maral Honarbin


import java.awt.*;

public class Hour18 extends java.applet.Applet implements Runnable {
//creat an applet java class called Hour 18 with implements Runnable

	Image ball;
	float current = (float) 0;
	Thread runner;
	int xPosition = 10;
		//create an int called x position
	int xMove = 1;
	int yPosition =-1;
	int ballHeight = 102;
	int ballWidth = 111;
	int height;
	Image workspace;
	Graphics offscreen;

	public void init() {
		
		workspace = createImage(size().width,size().height);
		offscreen = workspace.getGraphics() ;
		setBackground(Color.black);
		ball = getImage(getCodeBase(), "Maral.bmp");

	}

	public void paint(Graphics screen) {
		height = size().height - ballHeight;
		if (yPosition == -1)
		    yPosition = height;
		offscreen.setColor(Color.black);
		offscreen.fillRect(0,0,size().width,size().height);
		offscreen.drawImage(ball,
			(int) xPosition,
			(int) yPosition,
			this);
		screen.drawImage(workspace, 0, 0, this);
			//draw an image called workspace on point (0,0)
	}

	public void start() {
		if (runner == null) {
		    runner = new Thread (this);
		    runner.start();
	}
}

	public void run() {
		while (true) {
			repaint();
			current += (float) 0.1;
			if (current > 3)
			current = (float) 0;
		xPosition += xMove;
		if (xPosition > (size().width - 111))
		    xMove *= -1;
		if (xPosition < 1)
                    xMove *= -1;
		double bounce = Math.sin(current) * height;
			//the ball moving in the function of sin
		yPosition = (int) (height - bounce);
		try { Thread.sleep(200); }
		catch (InterruptedException e) { }
	}

}

	public void stop() {
		if (runner != null) {
	   	    runner.stop();
	            runner = null;
	}
}

	public void update(Graphics screen) {
		paint(screen);
	}
}