易截截图软件、单文件、免安装、纯绿色、仅160KB

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入门

 Java学习从入门到精通 
一、 JDK (Java Development Kit) 
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......

JAVA读XML:sax,dom,jdom,dom4j的比较以及选择(转)

JAVA读XML:sax,dom,jdom,dom4j的比较以及选择(转)
原文:www.hicourt.gov.cn/homepage/show9_content.asp
SAX:                                 ......

java中的基本数据类型长度

        char[] cha = operator.toCharArray();
        char ch = cha[0];
        double sum = 0;
        switch (ch) {
      &nb ......

JAVA面试题集

                                     JAVA面试题集
1.数据库,比如100用户同时来访,要采取什么技术解决;(JDBC)
答:可采用 ......

java集合小结

在使用Java的时候,我们都会遇到使用集合(Collection)的时候,但是Java API提供了多种集合的实现,我在使用和面试的时候频
频遇到这样的“抉择” 。 :)(主要还是面试的时候)
久而久之,也就有了一点点的心得体会,写出来以供大家讨论 。
总的说来,Java API中所用的集合类,都是实现了Collection接口,他 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号