Skip to content

Commit 74cdecf

Browse files
committed
第一次提交
0 parents  commit 74cdecf

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# DanMaKu-Engine
2+
## 说明
3+
```
4+
这是一个屏幕弹幕引擎,主要作用是在屏幕中发送弹幕.
5+
运用场景:比如说年会,活动... 需要在屏幕中发送弹幕的场景
6+
```
7+
8+
## 使用方法
9+
10+
[DanMaKu-Engine](https://github.com/actar676309180/DanMaKu-Engine/releases/)
11+
12+
1. 点击上方链接,下载jar文件
13+
2. 在你的项目中引入jar文件
14+
3.
15+
```
16+
DanMaKuEngine.open()
17+
val danMaKu = DanMaKu("This is a Test.", Font.font(22.0), Color.GREEN)
18+
DanMaKuEngine.launch(danMaKu)
19+
```
20+
21+
本项目基于JavaFX,需要jdk1.8及以上
22+
如果使用的是openjdk,则需要引入openjfx
23+
24+
```
25+
DanMaKuEngine.open()
26+
```
27+
用于打开一个窗口,此方法是阻塞的.耗时2-3秒.
28+
29+
```
30+
DanMaKu("This is a Test.", Font.font(22.0), Color.GREEN)
31+
```
32+
用于创建一个弹幕 DanMaKu("消息",字体,颜色)
33+
34+
```
35+
DanMaKuEngine.launch(danMaKu)
36+
```
37+
用于发送弹幕

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.actar676309180</groupId>
8+
<artifactId>DanMaKu-Engine</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
24+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.actar676309180.danmaku;
2+
3+
import com.github.actar676309180.danmaku.utils.ScreenUtil;
4+
import javafx.scene.layout.VBox;
5+
import javafx.scene.text.Text;
6+
7+
import java.util.Random;
8+
9+
10+
public class Bullet extends VBox {
11+
12+
private static Random random = new Random();
13+
14+
public Bullet(DanMaKu danMaKu){
15+
Text text = new Text(danMaKu.getMessage());
16+
text.setFont(danMaKu.getFont());
17+
text.setFill(danMaKu.getColor());
18+
this.getChildren().add(text);
19+
this.setStyle("-fx-background:transparent;");
20+
21+
double width = ScreenUtil.visualBoundWidth;
22+
double height = ScreenUtil.visualBoundHeight;
23+
24+
this.setTranslateX(width);
25+
26+
this.setTranslateY(random.nextInt((int)height));
27+
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.actar676309180.danmaku;
2+
3+
import javafx.scene.paint.Color;
4+
import javafx.scene.text.Font;
5+
6+
public class DanMaKu {
7+
private String message;
8+
private Font font;
9+
private Color color;
10+
11+
public DanMaKu(String message, Font font, Color color) {
12+
this.message = message;
13+
this.font = font;
14+
this.color = color;
15+
}
16+
17+
public String getMessage() {
18+
return message;
19+
}
20+
21+
public void setMessage(String message) {
22+
this.message = message;
23+
}
24+
25+
public Font getFont() {
26+
return font;
27+
}
28+
29+
public void setFont(Font font) {
30+
this.font = font;
31+
}
32+
33+
public Color getColor() {
34+
return color;
35+
}
36+
37+
public void setColor(Color color) {
38+
this.color = color;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "DanMaKu{" +
44+
"message='" + message + '\'' +
45+
", font=" + font +
46+
", color=" + color +
47+
'}';
48+
}
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.github.actar676309180.danmaku;
2+
3+
import javafx.application.Application;
4+
5+
class DanMaKuEngine {
6+
7+
private static boolean isOpen = false;
8+
9+
static DanMaKuView danMaKudanMaKuView = null;
10+
11+
/**
12+
* 打开弹幕引擎
13+
*/
14+
static synchronized void open() {
15+
if (!isOpen) {
16+
new Thread(() -> Application.launch(DanMaKuView.class)).start();
17+
try {
18+
while (danMaKudanMaKuView == null) Thread.sleep(10);
19+
} catch (InterruptedException e) {
20+
throw new RuntimeException("线程调度失败");
21+
}
22+
isOpen = true;
23+
} else {
24+
throw new RuntimeException("open 不能被调用两次");
25+
}
26+
}
27+
28+
/**
29+
* 发射弹幕
30+
*
31+
* @param danMaKu 弹幕
32+
*/
33+
public static void launch(DanMaKu danMaKu) {
34+
if (isOpen) {
35+
danMaKudanMaKuView.launch(danMaKu);
36+
} else {
37+
throw new RuntimeException("未初始化,无法发射弹幕");
38+
}
39+
}
40+
41+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.github.actar676309180.danmaku;
2+
3+
import com.github.actar676309180.danmaku.utils.ScreenUtil;
4+
import javafx.application.Application;
5+
import javafx.application.Platform;
6+
import javafx.scene.Scene;
7+
import javafx.scene.layout.StackPane;
8+
import javafx.stage.Stage;
9+
import javafx.stage.StageStyle;
10+
11+
import java.util.List;
12+
import java.util.concurrent.CopyOnWriteArrayList;
13+
import java.util.concurrent.locks.Lock;
14+
import java.util.concurrent.locks.ReentrantLock;
15+
16+
public class DanMaKuView extends Application implements Runnable {
17+
18+
private Lock lock = new ReentrantLock();
19+
20+
private StackPane root = new StackPane();
21+
private List<Bullet> readyList = new CopyOnWriteArrayList<>();
22+
private List<Bullet> loopList = new CopyOnWriteArrayList<>();
23+
private List<Bullet> destroyList = new CopyOnWriteArrayList<>();
24+
25+
@Override
26+
public void start(Stage primaryStage) {
27+
DanMaKuEngine.danMaKudanMaKuView = this;
28+
29+
double width = ScreenUtil.visualBoundWidth;
30+
double height = ScreenUtil.visualBoundHeight;
31+
32+
Scene scene = new Scene(root, width, height);
33+
34+
scene.setFill(null);
35+
36+
primaryStage.initStyle(StageStyle.TRANSPARENT);
37+
38+
primaryStage.setWidth(width);
39+
primaryStage.setHeight(height);
40+
primaryStage.setX(0);
41+
primaryStage.setY(0);
42+
43+
primaryStage.setTitle("Dan Ma Ku");
44+
primaryStage.setScene(scene);
45+
// 始终悬浮在顶层
46+
primaryStage.setAlwaysOnTop(true);
47+
primaryStage.show();
48+
new Thread(this).start();
49+
}
50+
51+
private void ready() {
52+
lock.lock();
53+
try {
54+
for (Bullet bullet : readyList) {
55+
loopList.add(bullet);
56+
Platform.runLater(() -> {
57+
root.getChildren().add(bullet);
58+
});
59+
}
60+
readyList.clear();
61+
} finally {
62+
lock.unlock();
63+
}
64+
}
65+
66+
private void loop() {
67+
for (Bullet bullet : loopList) {
68+
Platform.runLater(()->{
69+
bullet.setTranslateX(bullet.getTranslateX()-1);
70+
});
71+
if (bullet.getTranslateX() < 0){
72+
destroyList.add(bullet);
73+
}
74+
}
75+
}
76+
77+
private void destroy() {
78+
for (Bullet bullet : destroyList) {
79+
loopList.remove(bullet);
80+
Platform.runLater(()->{
81+
root.getChildren().remove(bullet);
82+
});
83+
}
84+
destroyList.clear();
85+
}
86+
87+
@Override
88+
public void run() {
89+
while (true) {
90+
try { Thread.sleep(10); } catch (InterruptedException e) {e.printStackTrace();}
91+
ready();
92+
loop();
93+
destroy();
94+
}
95+
}
96+
97+
/**
98+
* 发射弹幕
99+
*
100+
* @param danMaKu 弹幕
101+
*/
102+
public void launch(DanMaKu danMaKu) {
103+
lock.lock();
104+
try {
105+
Bullet bullet = new Bullet(danMaKu);
106+
readyList.add(bullet);
107+
} finally {
108+
lock.unlock();
109+
}
110+
}
111+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.actar676309180.danmaku.utils;
2+
3+
import javafx.geometry.Rectangle2D;
4+
import javafx.stage.Screen;
5+
6+
public class ScreenUtil {
7+
8+
public final static Double boundWidth;
9+
public final static Double boundHeight;
10+
public final static Double visualBoundWidth;
11+
public final static Double visualBoundHeight;
12+
13+
static {
14+
final Screen primary = Screen.getPrimary();
15+
final Rectangle2D bounds = primary.getBounds();
16+
boundWidth = bounds.getWidth();
17+
boundHeight = bounds.getHeight();
18+
final Rectangle2D visualBounds = primary.getVisualBounds();
19+
visualBoundWidth = visualBounds.getWidth();
20+
visualBoundHeight = visualBounds.getHeight();
21+
}
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.actar676309180.danmaku;
2+
3+
import javafx.scene.paint.Color;
4+
import javafx.scene.text.Font;
5+
6+
public class MainTest {
7+
public static void main(String[] args) throws InterruptedException {
8+
DanMaKuEngine.open();
9+
for (int i = 0; i < 10000; i++) {
10+
Thread.sleep(500);
11+
DanMaKuEngine.launch(new DanMaKu("test "+i,Font.font(40), Color.GREEN));
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)