⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.java

📁 Java变的几个小程序
💻 JAVA
字号:
// Ball.java

import java.awt.*;

// Class representing the MODEL of a ball

public class Ball

{

 /////////////////////////// VARIABLES //////////////

 // Ball is circle, with centre (x,y) and radius "radius"

          int x;

          int y;

          int radius;

// each time ball moves it moves (dx, dy)

          int dx;

          int dy;

 // colour of ball is "color" (note we use US spelling)

          Color color;

///////////////////////// METHODS //////////////

// initialise new ball object

public Ball (int newX, int newY, int newRadius, int newXMotion, int newYMotion)

{ 
	x = newX;
	y = newY;
	radius = newRadius;

	color = Color.blue;	// default colour is blue

	dx = newXMotion;
	dy = newYMotion;
 }

// display the ball (using location and color variables)

public void paint (Graphics g) 

{ 
	g.setColor(color);
	g.fillOval(x,y,radius, radius);
 }

// set the location of the centre of the ball

public void setXY(int newX, int newY)

{ 
	x = newX;
	y = newY;
 }

public int getX()

{ 
	return x;
}

public int getY()

{ 
	return y;
}

public int getXMotion()

{ 
	return dx;
}

public int getYMotion()

{ 
	return dy;
}

public void setColor( Color newColor ) 

{ 
	color = newColor;
}

public void setMotion( int newXMotion, int newYMotion )

{ 
	dx = newXMotion;
	dy = newYMotion;
}

public void move()

{ 
	x = x + dx;
	y = y + dy;
}

} // end of class definition

⌨️ 快捷键说明

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