一、首先建立包ball,然后创建模板类BallModel
BallModel类代码如下 :
package ball;
import java.awt.Color;
import java.awt.Graphics;
public class BallModel {
int x,y,d,speed; //定义小球的x,y坐标,小球直径和速度
Color c;
//构造方法
public BallModel() {
x=(int)(Math.random()*1920); //随机生成x坐标
y=(int)(Math.random()*1080); //随机生成y坐标
d=(int)(Math.random()*50)+10; //小球直径
speed=12-d/6; //球直径越小速度越快
c=new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)); //随机生成小球颜色
}
//画出小球
public void drawBall(Graphics g){
g.setColor(c);
g.fillOval(x, y, d, d);
}
}
二、新建BallPanel类,创建对象和移动小球
BallPanel类代码如下 :
package ball;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class BallPanel extends JPanel implements MouseMotionListener{
/**
*
*/
private static final long serialVersionUID = 5088665161758943179L;
int[] dir=new int[200]; //小球路径数组
BallModel[] ba=new BallModel[200]; //小球对象数组
public BallPanel (){
for (int i = 0; i < ba.length; i++) {
ba[i]=new BallModel(); //创建小球对象
dir[i]=(int)(Math.random()*4+1); //生成小球路径
}
}
//画出小球
@Override
public void paint(Graphics g) {
super.paint(g);
setOpaque(false);
for (int i = 0; i < ba.length; i++) {
ba[i].drawBall(g); //调用模板中画小球的方法
}
}
//小球的移动轨迹控制
public void moveBall(){
new Thread(){
public void run() { //重写run方法
super.run();
while(true){
for (int i=0;i<ba.length;i++){ //循环所有小球
//判断路径,增加值使小球前进
switch (dir[i]) {
case 1:{
ba[i].x+=ba[i].speed;
ba[i].y+=ba[i].speed;
break;
}case 2:{
ba[i].x-=ba[i].speed;
ba[i].y+=ba[i].speed;
break;
}case 3:{
ba[i].x-=ba[i].speed;
ba[i].y-=ba[i].speed;
break;
}case 4:{
ba[i].x+=ba[i].speed;
ba[i].y-=ba[i].speed;
break;
}default:
break;
}
//判断边界值,改变小球路径
if(ba[i].x>1920 - ba[i].d){
if(dir[i]==1){dir[i]=2;}
if(dir[i]==4){dir[i]=3;}
ba[i].c=new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
}
if(ba[i].x<0){
if(dir[i]==2){dir[i]=1;}
if(dir[i]==3){dir[i]=4;}
ba[i].c=new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
}
if(ba[i].y>1080 - ba[i].d){
if(dir[i]==1){dir[i]=4;}
if(dir[i]==2){dir[i]=3;}
ba[i].c=new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
}
if(ba[i].y<0){
if(dir[i]==4){dir[i]=1;}
if(dir[i]==3){dir[i]=2;}
ba[i].c=new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
}
}
repaint(); //重画
try {
Thread.sleep(20); //休眠20毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
@Override
public void mouseDragged(MouseEvent e) {
System.exit(0); //退出
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
三、新建BallFrame类,创建屏保窗口,添加组件
BallFrame类代码如下
package ball;
import javax.swing.JFrame;
import com.sun.awt.AWTUtilities;
public class BallFrame {
public static void main(String[] args) throws InterruptedException {
JFrame j=new JFrame();
BallPanel b=new BallPanel();
j.add(b);
j.setSize(1920, 1080);
j.setUndecorated(true); //去边框
j.setLocationRelativeTo(null); //居中
j.setDefaultCloseOperation(3); //默认关闭
AWTUtilities.setWindowOpaque(j, false); //设置透明
j.setVisible(true); //设为可见
b.moveBall(); //调用小球移动方法
j.addMouseMotionListener(b); //注册监听器
}
}
OK,一个简单的屏保程序就完成了。