本来是想实现泡泡屏保(javascript实现漂亮的气泡碰撞效果(Chrome浏览器下更佳) 下载-脚本之家)的,还未实现?
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.Random;
class Bubble
{
public static Image image;
public int x;
public int y;
public int displayWidth;
public int displayHeight;
public int gravity;
public double bounce;
public Bubble(String image, int x, int y, int displayWidth, int displayHeight) {
this.image = Toolkit.getDefaultToolkit().getImage("src//" + image);
this.x = x;
this.y = y;
this.displayWidth = displayWidth;
this.displayHeight = displayHeight;
gravity = 1;
bounce = 0.9;
}
public void move()
{
}
@Override
public String toString() {
return "Bubble{" +
"x=" + x +
", y=" + y +
", displayWidth=" + displayWidth +
", displayHeight=" + displayHeight +
'}';
}
}
public class MainFrame extends JFrame implements Runnable {
Graphics graphics;
Image image;
int x;
int y;
private final int windowWidth;//画板的宽度
private final int windowHeight;//画板的高度
private LinkedList<Bubble> bubbleLinkedList;
private double ballsnum= 5; // 小球数目
private double spring= 0.8; // 弹力加速度
private double bounce= -0.95; // 反弹
private double gravity= 0.1; // 重力
{
setLayout(null);
windowWidth = 629;
windowHeight = 990;
setSize(windowWidth, windowHeight);
setLocationRelativeTo(null);
setVisible(true);
graphics = getContentPane().getGraphics();
}
MainFrame()
{
bubbleLinkedList = new LinkedList<>();
for (int i = 0; i < 1; i++) {
Integer x = new Random().nextInt(windowWidth / 10);
Integer y = new Random().nextInt(windowHeight / 20);
bubbleLinkedList.add(new Bubble("background.jpg", x, y, 20, 20));
}
}
@Override
public void run() {
while (true)
{
for (Bubble b: bubbleLinkedList) {
graphics.drawImage(b.image, b.x, b.y, b.displayWidth, b.displayHeight, this);
BulletHit();
b.move();
}
y+=10;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void move(Bubble bubble)
{
gravity += 2;
bubble.x += 2;
bubble.y += gravity;
System.out.println(bubble.x + ": " + bubble.y + ": " + bubble.displayHeight + ": " + windowHeight);
if (bubble.y + bubble.displayHeight > windowHeight)
{
bubble.y = windowHeight - bubble.displayHeight;
gravity *= -0.8;
}
}
public void BulletHit()
{
for (Bubble bubble0: bubbleLinkedList)
{
for (Bubble bubble1: bubbleLinkedList)
{
if (!bubble0.equals(bubble1))
{
double x = Math.abs(bubble0.x - bubble1.x);
double x0 = bubble0.x - bubble1.x;
double y = Math.abs(bubble0.y - bubble1.y);
double y0 = bubble0.y - bubble1.y;
double dist = Math.sqrt(x * x + y * y);
if (dist <= bubble0.displayWidth)
{
double angle = Math.atan2(y0, x0);
double tx = bubble0.x + Math.cos(angle) * dist;
double ty = bubble0.y + Math.sin(angle) * dist;
double ax = (tx - bubble1.x) * spring;
double ay = (ty - bubble1.y) * spring;
bubble0.x -= ax;
bubble0.y -= ay;
bubble1.x -= ax;
bubble1.y -= ay;
}
}
}
}
for (Bubble bubble0: bubbleLinkedList)
{
move(bubble0);
}
}
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
new Thread(mainFrame).start();
}
}