I've been working on this for some time now and I can't seem to figure out where I have gone wrong. I'm working on a VLCJ project and trying to use an overlay to display changes to the video, like volume for example, and only display it for 3 seconds from the last input. Everything seems to work great except for the fact that the repaint() doesn't change the text in the overlay at all while it is displaying.
Here is my code...
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.geom.Rectangle2D;
import uk.co.caprica.vlcj.player.embedded.OverlayApi;
class Overlay extends Window implements Runnable
{
private static final long serialVersionUID = 1L;
private String text;
private int x, y;
private Thread thread;
private boolean running;
private FontMetrics metric; // the metrics from the graphics
private int stringWidth; // the width of the string that is currently being displayed
private OverlayApi overlay;
private long startTimer;
public Overlay(Window owner)
{
super(owner);
setBackground(new Color(0, 0, 0, 0));
text = "";
x = (int) getOwner().getLocation().getX();
y = (int) getOwner().getLocation().getY();
running = false;
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2D = (Graphics2D)g;
g2D.setPaint(Color.black);
Font fnt = new Font("Serif", Font.PLAIN,32);
g2D.setFont(fnt);
metric = g2D.getFontMetrics(fnt); // get the font size so it can figure out how wide the text is
stringWidth = metric.stringWidth(text); // get the width of the current text field
// make a rectangle the size of the text and fill it in with black
g2D.fill(new Rectangle2D.Double(x, y, stringWidth, g2D.getFont().getSize() + 3));
g2D.setPaint(Color.white);
g2D.drawString(text, x, y + g2D.getFont().getSize());
}
@Override
public void run()
{
long lastTime = System.currentTimeMillis();
double amountOfTicks = 60.0;
double ns = 1000 / amountOfTicks;
double delta = 0;
while (running)
{
long now = System.currentTimeMillis();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1)
{
if (timesUp())
{
overlay.enable(false);
running = false;
System.out.println("STOPPING");
}
delta--;
}
if (running) render();
}
}
private void render()
{
this.repaint();
}
public synchronized void start()
{
startTimer = System.currentTimeMillis();
overlay.enable(true);
if (!running)
{
thread = new Thread(this);
thread.start();
running = true;
}
}
private boolean timesUp()
{
long et = System.currentTimeMillis();
long time = et - startTimer;
long sec = 0;
if (time > 1000)
{
sec = (time / 1000) % 60;
if (sec == 3) return true;
}
return false;
}
public void setText(String s)
{
this.text = s;
}
public String getText()
{ return this.text; }
public void setOverlay(OverlayApi overlayRef)
{ this.overlay = overlayRef; }
}
Any help I can get would be greatly appreciated as I'm stuck, thank you
--UPDATED--
So, I completely removed the Thread from the equation and still no dice. I also removed the use of AWT and tried a JLabel but even that isn't updating. Its all the same problem...