JAVA实现的六色球
这是JAVA中的一个作业,
效果图:
画框中共有六个球,它们碰到墙之后能够反弹,而且相互碰撞之后能相互碰撞。
要用到的知识:
1. awt画图,要把球画出来
public void draw(Graphics g){
Color c = g.getColor(); g.setColor(Color.red);
g.fillOval(x, y, d, d);
g.setColor(c);
}
2. 线程对球的重画
class MyThread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
源代码:
1. 球(Ball)类:
import java.awt.*;
public class Ball {
public static final int d = 30;//球的直径是30
int x=100,y=100;//球所在的位置
private double direction = 225;//小球的运动方向的度数表示,0-360度
//球的编号
private int id;
BallClient bc;
public Ball(int x, int y, double direction, int id,BallClient bc) {
super();
this.x = x;
this.y = y;
this.direction = direction;
this.id = id;
this.bc = bc;
}
public void draw(Graphics g){
Color c = g.getColor();
if(id==1)
g.setColor(Color.red);
if(id==2)
g.setColor(Color.blue);
if(id==3)
g.setColor(Color.green);
if(id==4)
g.setColor(Color.black);
if(id==5)
g.setColor(Color.pink);
if(id==6)
g.setColor(Color.yellow);
g.fillOval(x, y, d, d);
g.setColor(c);
move();
touchWall();
touchEach();
}
public double getDirection() {
return direction;
}
public void setDirection(double direction) {
this.direction = direction;
}
//小球运动函数
public void move(){
if(direction>=0&&direction<=90){
y += Math.sin((direction)*Math.PI/180)*10;
x += Math.cos((d
相关文档:
Java学习从入门到精通
一、 JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
// AuthorityInterceptor.java 文件
package com.aptech.jb.epet.web.authority;
import javax.servlet.http.HttpServletRequest;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.struts.action.ActionMapping;
public class Authority ......
一: java.awt包
java.awt包中提供了GUI设计所使用的类和接口,可从中看到主要类之间的关系。
java.awt包提供了基本的java程序的GUI设计工具。主要包括下述三个概念:
组件--Component
容器--Container
布局管理器--LayoutManager
二: ......