java web 弹幕技术实现

先放入效果图

实现环节

1-前端实现将把信息发送到后台和接收后台发送过来的信息并且将其发射弹幕到浏览器上

2-通过websocket与每个服务端连接,当接收到来自任何一个前端发来的信息,就将该信息群发至所有的客户端(也就是浏览器)



客户端(浏览器)的代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="Keywords" content="danmu">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>弹幕网站</title>
<style type="text/css">
body {
background: url(images/01.jpg); no-repeat top center;
font-size: 12px;
font-family: "微软雅黑";
}


* {
margin: 0;
padding: 0;
}
/* screen start*/
.screen {
width: 300px;
height: 100px;
background: #669900;
}


.dm {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
display: none;
}


.dm .d_screen .d_del {
width: 38px;
height: 38px;
background: #600;
display: block;
text-align: center;
line-height: 38px;
text-decoration: none;
font-size: 20px;
color: #fff;
border-radius: 19px;
border: 1px solid #fff;
z-index: 2;
position: absolute;
right: 20px;
top: 20px;
outline: none;
}


.dm .d_screen .d_del:hover {
background: #F00;
}


.dm .d_screen .d_mask {
width: 100%;
height: 100%;
background: #000;
position: absolute;
top: 0;
left: 0;
opacity: 0.6;
filter: alpha(opacity = 60);
z-index: 1;
}


.dm .d_screen .d_show {
position: relative;
z-index: 2;
}


.dm .d_screen .d_show div {<

  • 10
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是使用 Java 实现弹幕的基本步骤: 1. 定义弹幕类(Danmu),包含弹幕的文本内容、颜色、字体大小、位置、速度等属性。 2. 创建弹幕池(DanmuPool),用于存储弹幕对象。 3. 从弹幕池中获取弹幕对象,并设置弹幕的属性。 4. 将弹幕对象添加到弹幕面板(DanmuPanel)中。 5. 启动弹幕线程(DanmuThread),不断地从弹幕池中获取弹幕对象,将其移动并绘制到弹幕面板上。 以下是一个简单的 Java 弹幕代码示例: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class Danmu extends JPanel { private static final long serialVersionUID = 1L; private String text; private Color color; private Font font; private int x; private int y; private int speed; public Danmu(String text, int x, int y) { this.text = text; this.color = new Color(new Random().nextInt(256), new Random().nextInt(256), new Random().nextInt(256)); this.font = new Font("微软雅黑", Font.BOLD, 16); this.x = x; this.y = y; this.speed = 5; } public void move() { x -= speed; } public void draw(Graphics g) { g.setColor(color); g.setFont(font); g.drawString(text, x, y); } public boolean isOutOfPanel() { return x < -text.length() * font.getSize(); } public static class DanmuPool { private List<Danmu> danmus; public DanmuPool() { danmus = new ArrayList<>(); } public void addDanmu(Danmu danmu) { danmus.add(danmu); } public Danmu getDanmu() { for (Danmu danmu : danmus) { if (danmu.isOutOfPanel()) { return danmu; } } Danmu danmu = new Danmu("", 0, 0); danmus.add(danmu); return danmu; } public List<Danmu> getDanmus() { return danmus; } } public static class DanmuThread extends Thread { private DanmuPool pool; private DanmuPanel panel; public DanmuThread(DanmuPool pool, DanmuPanel panel) { this.pool = pool; this.panel = panel; } public void run() { while (true) { Danmu danmu = pool.getDanmu(); danmu.move(); panel.repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static class DanmuPanel extends JPanel { private static final long serialVersionUID = 1L; private DanmuPool pool; public DanmuPanel(DanmuPool pool) { this.pool = pool; } public void paint(Graphics g) { super.paint(g); for (Danmu danmu : pool.getDanmus()) { danmu.draw(g); } } } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DanmuPool pool = new DanmuPool(); DanmuPanel panel = new DanmuPanel(pool); frame.add(panel); frame.setVisible(true); DanmuThread thread = new DanmuThread(pool, panel); thread.start(); } } ``` 在上面的示例中,我们定义了 Danmu 类,用于表示弹幕对象。Danmu 类包含了弹幕的文本内容、颜色、字体大小、位置、速度等属性,以及移动和绘制弹幕的方法。同时,我们还定义了 DanmuPool 类,用于存储弹幕对象。DanmuThread 类是弹幕线程,不断地从弹幕池中获取弹幕对象,将其移动并绘制到弹幕面板上。DanmuPanel 类是弹幕面板,用于显示弹幕。 在 main 方法中,我们创建了一个 JFrame 窗口,并将一个 DanmuPanel 对象添加到窗口中。然后创建了一个 DanmuThread 线程,启动弹幕线程。这样就可以实现一个简单的 Java 弹幕效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值