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
相关文档:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"
import="java.awt.*,java.awt.image.*,javax.imageio.*"%><%
/*
使用方法:在需要显示验证码的html代码中使用<img p">
在需判断session的时候判断session.getAttribute("vcode")
*/try{
int codeLength=4;//验 ......
import java.text.SimpleDateFormat;
Date date=new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
java.sql.Time now=new java.sql.Time(System.CurrentTimeMillis());
输出短的系统时间 18:34:44 ......
在使用Java的时候,我们都会遇到使用集合(Collection)的时候,但是Java API提供了多种集合的实现,我在使用和面试的时候频
频遇到这样的“抉择” 。 :)(主要还是面试的时候)
久而久之,也就有了一点点的心得体会,写出来以供大家讨论 。
总的说来,Java API中所用的集合类,都是实现了Collection接口,他 ......
package test
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class ReadSelectedLine{
// 读取文件指定行。
static void readAppointedLineNumber(File sourceFile, int lineNumber)
throws IOException {
......
Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利。为了编写高效稳定可靠的多线程程序,线程部分的新增内容显得尤为重要。
有关Java5线程新特征的内容全部在java.util.concurrent下面,里面包含数目众多 ......