ballapplet.java

来自「ATM自动提款机模拟程序 可以提款」· Java 代码 · 共 81 行

JAVA
81
字号
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class BallApplet extends Applet
{
	private static final int WIDTH=50;  //圆形的宽高
	private static final int HEIGHT=50;
	private int x=0;  //x坐标
	private int y=0;  //y坐标
	private boolean XSure=true;  //XSure为true x坐标变量变动 为false y坐标变动
	private boolean YSure=false; //XYSure为true y坐标变量变动 为false x坐标变动
	
	public void init()
	{
		this.setSize(600,600); //这句有没有都没所谓 由html定义
	}
	public void paint(Graphics g)
	{	
		g.setColor(Color.PINK);
		g.fillOval(x,y,WIDTH,HEIGHT); //圆形填充颜色
		try 
		{
			Thread.sleep(100);    //每睡眠100毫秒之后调用checkXy检查xy坐标值
		}
		catch (Exception ex) {}
		checkXY();
		repaint();
	}
	private void checkXY()
	{
		/**********************************************************/
		if(XSure==true && YSure==false && y == 0) //四个边界的xy坐标判断
		{
			x+=10;
			if(x > 550)
			{
				x=550;
				y+=10;
				XSure=false;
				YSure=true;
			}
			
		}
		/**********************************************************/
		if(	XSure==false && YSure==true && x == 550)
		{
			y+=10;
			if(y > 550)
			{
				y=550;
				x-=10;
				XSure=true;
				YSure=false;
			}
		}
		/**********************************************************/
		if( XSure==true && YSure==false && y == 550)
		{
			x-=10;
			if(x < 0)
			{
				x=0;
				y-=10;
				XSure=false;
				YSure=true;
			}
		}
		/**********************************************************/
		if( XSure==false && YSure==true &&  x == 0)
		{
			y-=10;
			if(y < 0)
			{
				y=0;   //这里是最后一个边界 也就是左下到左上的滚动
				x=0;   //这里如果调用x-=10 那么到了左上会有点异常 圆形会多了一步缩位
				XSure=true;
				YSure=false;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?