- 1、本文档共10页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
绘制随机小球来回弹动java代码
Java小球碰撞代码
绘制小球随机位置,随机颜色(自定义颜色)来回碰撞。这个代码只有上下左右四个方向,所以反弹也是固定的45°角。
首先是定义小球(Ball类)
package picture;
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
private int x,y;//分清定义是左上角顶点坐标还是圆心点坐标(圆心坐标)
private int rSize;//半径
private Color color;
private int speed;
private int orientation;
private BallPanel panel;//获得小球所属的面板
public static final int RIGHT_DOWN=0;
public static final int RIGHT_ON=1;
public static final int LEFT_DOWN=2;
public static final int LEFT_ON=3;
public Ball(){
}
public Ball(int x, int y, int rSize, Color color, int speed, int orientation) {
super();
this.x = x;
this.y = y;
this.rSize = rSize;
this.color = color;
this.speed = speed;
this.orientation = orientation;
}
public void draw(Graphics g){
g.setColor(color);
g.fillArc(x-rSize, y-rSize/*左上角定点坐标*/, 2*rSize, 2*rSize, 0, 360);
}//定义小球绘画的逻辑
public void move(){//小球处在不同方向时移动所改变的坐标值
switch(orientation){
case RIGHT_DOWN:
x+=speed;y+=speed;
if(x+rSize =panel.getWidth()){
this.orientation = LEFT_DOWN;
}
if(y+rSize =panel.getHeight()){
this.orientation = RIGHT_ON;
}
break;
case RIGHT_ON:
x+=speed;y-=speed;
if(x+rSize =panel.getWidth()){
this.orientation = LEFT_ON;
}
if(y-rSize = 0 ){
this.orientation = RIGHT_DOWN;
}
break;
case LEFT_DOWN:
x-=speed;y+=speed;
if(x-rSize = 0 ){
this.orientation = RIGHT_DOWN;
}
if(y+rSize =panel.getHeight()){
this.orientation = LEFT_ON;
}
break;
case LEFT_ON:
x-=speed;y-=speed;
if(x-rSize = 0 ){
this.orientation = RIGHT_ON;
}
if(y-rSize = 0){
this.orientation = LEFT_DOWN;
}
break;
}
/*碰到边界事件处理*/
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getrSize() {
return rSize;
}
public void setrSize(int rSize) {
this.rSize = rSize;
}
public Color getColor(
您可能关注的文档
最近下载
- 上海市外籍人口空间分布历史变迁研究.pdf
- 至为芯科技IP5356规格书资料.pdf
- 学习党的二十届三中全会精神应知应会知识测试题AB卷(附答案).docx VIP
- 进位制-(公开课).ppt VIP
- 船舶电气作业安全操作规程.docx
- DB37T5267-2023钢丝网架(片)板现浇混凝土复合保温体系应用技术标准 .docx VIP
- 2024安全生产标准化管理体系新旧版本对比汇总版.docx
- 国家电网计算机类笔试学习资料-计算机网络.pdf VIP
- 人教版初中数学第十四章《整式的乘法与因式分解》解答题提高训练 (39)(含答案解析).docx VIP
- 公务员考试常识习题(带答案).doc VIP
文档评论(0)