-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathOverlayTest.java
More file actions
187 lines (152 loc) · 6.13 KB
/
OverlayTest.java
File metadata and controls
187 lines (152 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* @author clayoverwind
* @version 2017/4/10
* @E-mail clayanddev@163.com
*/
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.x.LibXUtil;
import com.sun.awt.AWTUtilities;
import com.sun.jna.platform.WindowUtils;
/**
* A test player demonstrating how to achieve a transparent overlay and translucent painting.
* <p>
* Press SPACE to pause the video play-back.
* <p>
* Press F11 to toggle the overlay.
* <p>
* If the video looks darker with the overlay enabled, then most likely you are using a compositing
* window manager that is doing some fancy blending of the overlay window and the main application
* window. You have to turn off those window effects.
* <p>
* Note that it is not possible to use this approach if you also want to use Full-Screen Exclusive
* Mode. If you want to use an overlay and you need full- screen, then you have to emulate
* full-screen by changing your window bounds rather than using FSEM.
* <p>
* This approach <em>does</em> work in full-screen mode if you use your desktop window manager to
* put your application into full-screen rather than using the Java FSEM.
* <p>
* If you want to provide an overlay that dynamically updates, e.g. if you want some animation, then
* your overlay should sub-class <code>JWindow</code> rather than <code>Window</code> since you will
* get double-buffering and eliminate flickering. Since the overlay is transparent you must take
* care to erase the overlay background properly.
* <p>
* Specify a single MRL to play on the command-line.
*/
public class OverlayTest extends VlcjTest {
public static void main(final String[] args) throws Exception {
final String[] testArgs = new String[1];
testArgs[0] = "D:\\a.mkv";
if(testArgs.length != 1) {
System.out.println("Specify a single MRL");
System.exit(1);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new OverlayTest(testArgs[0]);
}
});
}
public OverlayTest(String mrl) {
Frame f = new Frame("Test Player");
f.setIconImage(new ImageIcon(getClass().getResource("/icons/vlcj-logo.png")).getImage());
f.setSize(800, 600);
f.setBackground(Color.black);
f.setLayout(new BorderLayout());
Canvas vs = new Canvas();
f.add(vs, BorderLayout.CENTER);
f.setVisible(true);
final MediaPlayerFactory factory = new MediaPlayerFactory();
final EmbeddedMediaPlayer mediaPlayer = factory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(factory.newVideoSurface(vs));
f.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_F11:
mediaPlayer.enableOverlay(!mediaPlayer.overlayEnabled());
break;
case KeyEvent.VK_SPACE:
mediaPlayer.pause();
break;
}
}
});
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
mediaPlayer.release();
factory.release();
System.exit(0);
}
});
mediaPlayer.setOverlay(new Overlay(f));
mediaPlayer.enableOverlay(true);
mediaPlayer.playMedia(mrl);
LibXUtil.setFullScreenWindow(f, true);
}
private class Overlay extends Window {
private static final long serialVersionUID = 1L;
public Overlay(Window owner) {
super(owner, WindowUtils.getAlphaCompatibleGraphicsConfiguration());
AWTUtilities.setWindowOpaque(this, false);
setLayout(null);
JButton b = new JButton("JButton");
b.setBounds(150, 150, 100, 24);
add(b);
TranslucentComponent c = new TranslucentComponent();
c.setBounds(150, 200, 300, 40);
add(c);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(180.0f, 280.0f, new Color(255, 255, 255, 255), 250.0f, 380.0f, new Color(255, 255, 0, 0));
g2.setPaint(gp);
for(int i = 0; i < 3; i ++ ) {
g2.drawOval(150, 280, 100, 100);
g2.fillOval(150, 280, 100, 100);
g2.translate(120, 20);
}
}
}
private class TranslucentComponent extends JComponent {
private static final long serialVersionUID = 1L;
public TranslucentComponent() {
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g2.setPaint(new Color(255, 128, 128, 64));
g2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setPaint(new Color(0, 0, 0, 128));
g2.setFont(new Font("Sansserif", Font.BOLD, 18));
g2.drawString("Translucent", 16, 26);
}
}
}