本来是想实现泡泡屏保(javascript实现漂亮的气泡碰撞效果(Chrome浏览器下更佳) 下载-脚本之家)的,还未实现
20231221晚上整理了下,看起来简洁些,也加了点注释:
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
class Bubble
{
public int x;
public int y;
public int vx;
public int vy;
public int radius;
public Bubble(int x, int y, int vx, int vy, int radius) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = radius;
}
@Override
public String toString() {
return "Bubble{" + "x=" + x + ", y=" + y + ", vx=" + vx + ", vy=" + vy + ", radius=" + radius + '}';
}
}
public class MainFrame extends JFrame implements Runnable {
Graphics graphics;
private final int windowWidth; //画板的宽度
private final int windowHeight; //画板的高度
private final LinkedList<Bubble> bubbleLinkedList;
MainFrame()
{
windowWidth = 629;
windowHeight = 990;
setSize(windowWidth, windowHeight);
setVisible(true);
graphics = getContentPane().getGraphics();
bubbleLinkedList = new LinkedList<>();
//初始化小球
for (int i = 0; i < 3; i++) {
int x =(int) ((Math.random() - 0.5) * windowWidth);
int y = (int) ((Math.random() - 0.5) * windowHeight);
int vx = (int) ((Math.random() - 0.5) * 16);
int vy = (int) ((Math.random() - 0.5) * 16);
bubbleLinkedList.add(new Bubble(x, y, vx, vy, 40));
}
}
@Override
public void run() {
while (true)
{
graphics.clearRect(0, 0, windowWidth, windowHeight);
int color = 0;
for (Bubble b: bubbleLinkedList) {
graphics.setColor(new Color(color,0,0));
graphics.fillArc(b.x,b.y, b.radius, b.radius,0,360);
move(b);
color += 255 / bubbleLinkedList.size();
}
for (Bubble bubble0: bubbleLinkedList)
{
for (Bubble bubble1: bubbleLinkedList)
{
if (!bubble0.equals(bubble1)) {
BulletHit(bubble0, bubble1);
}
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void move(Bubble bubble) //球移动,并且判断是否超出屏幕的上下左右
{
bubble.vy += 2;
bubble.x += bubble.vx;
bubble.y += bubble.vy;
System.out.println(bubble);
if (bubble.y > windowHeight - 2 * bubble.radius)
{
bubble.y = windowHeight - 2 * bubble.radius;
bubble.vy *= -0.95;
}
if (bubble.y < bubble.radius )
{
bubble.y = bubble.radius;
bubble.vy *= -0.95;
}
if (bubble.x > windowWidth - bubble.radius) {
bubble.x = windowWidth - bubble.radius;
bubble.vx *= -1;
}
if( bubble.x < 0 )
{
bubble.x = 0;
bubble.vx *= -1;
}
}
public void BulletHit(Bubble bubble0, Bubble bubble1) //如果两个球撞击
{
int dx = bubble1.x - bubble0.x;
int dy = bubble1.y - bubble0.y;
int DistanceCenter = (int) Math.sqrt(dx * dx + dy * dy);
int DistanceRadius = (bubble0.radius + bubble1.radius) / 2;
if (DistanceCenter < DistanceRadius)
{
double angle = Math.atan2(dy, dx);
double ax = (bubble0.x - bubble1.x + Math.cos(angle) * DistanceRadius) * 0.9;
double ay = (bubble0.y - bubble1.y + Math.sin(angle) * DistanceRadius) * 0.9;
bubble0.vx -= ax;
bubble1.vx += ax;
bubble0.vy -= ay;
bubble1.vy += ay;
}
}
public static void main(String[] args) {
new Thread(new MainFrame()).start();
}
}
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 vx;
public int vy;
public int displayWidth;
public int displayHeight;
public int gravity;
public double bounce;
public Bubble(String image, int x, int y, int vx, int vy, int displayWidth, int displayHeight) {
this.image = Toolkit.getDefaultToolkit().getImage("src//" + image);
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.displayWidth = displayWidth;
this.displayHeight = displayHeight;
gravity = 1;
bounce = 0.9;
}
public void move()
{
}
@Override
public String toString() {
return "Bubble{" +
"x=" + x +
", y=" + y +
", vx=" + vx +
", vy=" + vy +
", displayWidth=" + displayWidth +
", displayHeight=" + displayHeight +
", gravity=" + gravity +
", bounce=" + bounce +
'}';
}
}
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 gravity= 2; // 重力
{
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++) {
int x = new Random().nextInt(windowWidth / 10);
int y = new Random().nextInt(windowHeight / 20);
int vx = (int) (Math.random() * 16 - 8);
int vy = (int) (Math.random() * 16 - 8);
bubbleLinkedList.add(new Bubble("background.jpg", x, y, vx, vy, 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();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void move(Bubble bubble)
{
//gravity += 2;
bubble.vy += gravity;
bubble.x += bubble.vx;
bubble.y += bubble.vy;
System.out.println(bubble.x + ": " + bubble.y + ": " + bubble.displayHeight + ": " + windowHeight);
if (bubble.y + bubble.displayHeight > windowHeight)
{
bubble.y = windowHeight - bubble.displayHeight;
bubble.vy *= -0.95;
}
if (bubble.x + bubble.displayWidth > windowWidth) {
bubble.x = windowWidth - bubble.displayWidth;
bubble.vx *= -1;
}
if( bubble.x <= bubble.displayWidth )
{
bubble.x = bubble.displayWidth;
bubble.vx *= -1;
}
}
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) * 0.9;
double ay = (ty - bubble1.y) * 0.9;
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();
}
}
显示效果:
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();
}
}