JavaFX Timeline Animation Stutters after a couple hours - java

Preface:
So I have this, well its not really all that important what the application is, it's a tcp listener that chats it up all day with some hardware. But it's also got some display stuff going on. The display shows different custom nodes in different areas of the screen and at the bottom of the screen is a grid that displays some "stuff" but when there isn't any "stuff" it scrolls some text across the screen.
Question:
As I will post in my method that builds the timeline, there is a Text element that transitions from right to left across the screen. This is working as expected for the first hour or maybe two, but after a while it becomes progressively more jittery. The TCP listener is still snappy and the threads it spawns don't have any trouble going in and out of the controller and changing the UI but that scrolling text is jumping like 2 inches at a time across the 55" screen it is displaying on. I really need a way to get text to scroll smoothly like it does when I first start up the application for an indefinite amount of time.
private void buildChyron() throws IllegalArgumentException {
setMessages();
LowerGrid.getChildren().clear();
StringBuilder txtstr = new StringBuilder();
for (String s : messages) {
txtstr.append(s).append(" ");
}
txtstr = new StringBuilder(txtstr.toString().trim());
Text msg = new Text(txtstr.toString());
LowerGrid.add(msg, 0, 0);
msg.setTextOrigin(VPos.TOP);
msg.setFont(Font.font("Veranda", FontWeight.BOLD, 150));
double sceneWidth = MainGrid.getWidth();
double msgWidth = msg.getLayoutBounds().getWidth();
KeyValue initKeyValue = new KeyValue(msg.translateXProperty(), sceneWidth);
KeyFrame initFrame = new KeyFrame(Duration.ZERO, initKeyValue);
KeyValue endKeyValue = new KeyValue(msg.translateXProperty(), -1.0 * msgWidth);
KeyFrame endFrame = new KeyFrame(Duration.seconds((msgWidth + sceneWidth) / messageRate), endKeyValue);
if ((timeline != null) && (timeline.getStatus().equals(Timeline.Status.PAUSED))) {
timeline.playFromStart();
} else {
timeline = new Timeline(initFrame, endFrame);
timeline.setCycleCount(Timeline.INDEFINITE);
if (msgIndex == messages.size() - 1) msgIndex = 0;
else msgIndex++;
timeline.playFromStart();
}
}
Here is where I call the thing from, as you can see I've been trying to experiment with it thinking maybe it was a hardware resources issue, but I haven't been able to get it under control. I'm testing the thing now and it's in a super visible position in the facility I am at. Super embarassing
private void updateStack() {
int count = 0;
System.out.println(andonStack);
andonStack.sortStack();
LowerGrid.getChildren().clear();
if (pokeyStack.size() > 0) {
AndonPane pane = new AndonPane(pokeyStack.get(0).getPokayo());
LowerGrid.add(pane, 0, 0, 9, 1);
} else {
int i;
for (i = 0; i < andonStack.size(); i++) {
if (i >= 9) break;
panes[i] = new AndonPane(((AndonSignalStack.AndonSignal) andonStack.get(i)).getType(),
((AndonSignalStack.AndonSignal) andonStack.get(i)).getStation(), this);
count++;
}
for (; i < 9; i++) {
panes[i] = new Pane();
panes[i].setStyle("-fx-background-color: rgba(0,0,0, 0)");
panes[i].setPrefWidth(MainGrid.getWidth() * (9 - i) / 9);
}
for (i = 0; i < count; i++) {
LowerGrid.add(panes[i], i, 0);
}
if (i == 0) {
Platform.runLater(this::buildChyron);
}else{
if (timeline != null) {
System.gc();
timeline.stop();
timeline = null;
}
}
}
}
Scrolling Text Picture

the answer was the clear method in the pane that it's setting in. the animation needs to be stopped and deleted
don't use pane.clear(); when theres an animation use LowerGrid.getChildren().removeAll();

Related

For Loop causing a Window to pop up more times than expected

I'm attempting to build Minesweeper in Java (Using Eclipse, if that matters for any reason whatsoever) and while making it, I've encountered an issue I haven't had before.
The window that's supposed to show up whenever a mine is pressed is opened... A lot. The window is only meant to open once and prompt the user to either continue or give up.
Important to note this only occurs whenever a mine is pressed on the top row of buttons. Not entirely sure if this is important, but the amount of times a window pops-up is 11. There are also no error codes given.
The following chunk of code is for whenever a user clicks on a mine.
//These are here just for clarification
private int continues = 3;
private Listen l = new Listen();
private class Listen extends MouseAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (source == mines[i][j])
{
//Though probably not optimal, I created mines based around a number referred to as difficulty
if (minePlacement[i][j] <= difficulty)
{
Frame f = new Frame();
Label L = new Label("You have " + continues + " continues remaining.");
Button B = new Button ("Try Again?");
Label La = new Label("You're out of continues...");
Button But = new Button("Exit");
f.setLayout(new FlowLayout());
B.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
f.dispose();
}
});
if (continues <= 0)
{
f.add(La);
f.add(But);
/*
For clarification,
This actionListener is used
solely to do System.exit(0);
*/
But.addActionListener(li);
f.setSize(250,125);
}
else
{
f.add(L);
f.add(B);
f.setSize(250,100);
}
f.setLocationRelativeTo(null);
f.setAlwaysOnTop(true);
f.setVisible(true);
f.setTitle("You Hit A Mine!");
continues--;
}
This following chunk of code is used in order to build the board itself.
for (int i = 0; i < rows; i++)
{
int x = (int) ((Math.random() + .1) * 10);
minePlacement[i][0] = x;
mines[i][0] = new Button();
mines[i][0].addMouseListener(l);
mines[i][0].addActionListener(l);
add(mines[i][0]);
for (int j = 0; j < cols; j++)
{
int y = (int) ((Math.random() + .1) * 10);
minePlacement[i][j] = y;
mines[i][j] = new Button();
mines[0][j].addMouseListener(l);
mines[0][j].addActionListener(l);
mines[i][j].addMouseListener(l);
mines[i][j].addActionListener(l);
add(mines[i][j]);
}
I've tried looking for other posts which may answer my question, though as it stands, I have failed to find any other posts that have been coded in Java. I've mainly found them in C++ or C, to which I don't understand how the solution was brought about.
I'd like to know if there's any way to set a limit on the amount of windows on the screen, or if there's a way to prevent the window from popping up more times than expected.
Thanks in advance!

Javafx canvas content disappears

I'm trying to visualize the process of solving the travelling salesman problem using the 2-opt-change algorithm but sometimes it seems like the execution of the program gets stuck and the window freezes without any error.
Sometimes even the lines drawn on a canvas disappear.
Some help would be nice.
Here's the drawing code:
int numCities = getNumOfCities();
Order currentOrder = data.getCurrentOrder();
if (newValue) {
GraphicsContext gc = canvasCities.getGraphicsContext2D();
gc.clearRect(0, 0, canvasCities.getWidth(), canvasCities.getHeight());
gc.setStroke(new Color(0, 0, 0, 1));
gc.setLineWidth(1);
gc.setFill((Color.GRAY));
for (int i = 0; i < currentOrder.getSize(); i++) {
City current = data.getCities()[currentOrder.getValue(i)];
City next;
if ((i + 1) > numCities - 1) {
next = data.getCities()[currentOrder.getValue(0)];
} else {
next = data.getCities()[currentOrder.getValue(i + 1)];
}
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10);
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY());
}
if(!(data.getBestEver()==null)) {
Order best = data.getBestEver();
gc.setStroke(new Color(1, 0, 0, 1));
gc.setLineWidth(3);
gc.setFill((Color.GRAY));
for (int i = 0; i < best.getSize(); i++) {
City current = data.getCities()[best.getValue(i)];
City next;
if ((i + 1) > numCities - 1) {
next = data.getCities()[best.getValue(0)];
} else {
next = data.getCities()[best.getValue(i + 1)];
}
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10);
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY());
}
}
repaint.set(false); //boolean that indicated if something changed and a repaint is necessairy
}
but sometimes it seems like the execution of the program gets stuck
and the window freezes without any error.
JFX consumes lots of resources...especially with Canvas and Shapes (e.g. recursive Group, etc.) If it got stuck it could be the "stack" was on the brink of overflow. Try with -Xss[nnM] where nn is any number, M stands for Megabytes. Example -Xss16M : Stack with 16MB for JFX threads. If it still behaves "weirdly" then combine -Xss with -Xms (for the heap).
The Problem was the multithreading part. After converting the paint part into a runnable that was invoked using Platform.runLater() it works fine.
Thank's to all who spent time on thinking about my problem.

Android Game Logic - Obsticles

I have been working on android apps for a long time but now I have decided to create a game aside with my pre-calculus final. I have completed the whole code and it works perfectly except one tiny issue. First of the game is based on flying pig(my classmate's face) avoiding top and bottom osticle. I developed an algorithm so that the obsticles are evenly spaced and based on random selection placed either as the top or bottom of the screen but never both at the same time!. The algorithm that needs improvement is in the 3rd code segment!
This is the screenshot of the problem: screenshot here
(My account is new so stackoverflow wont let me to share photos directly)
This is the class that calls updates for all dynamic objects (ship = pig, bacon = bonus item, BM = BarrierManager class's update() which updates the obsticles)
//this will update the resources
void Update(float dt) {
ship.update(dt);
//bumbing
if (!ship.death) {
background.update(dt);
**BM.update(dt);**
for (int i = 0; i % 5 == 0; i++) {
bacon.update(dt, BM.position);
}
}
ArrayList<Point> bacon_point = new ArrayList<Point>(bacon.getArray());
if (ship.bump(bacon_point.get(0), bacon_point.get(1), bacon_point.get(2), bacon_point.get(3))) {
bacon.setX(-200);
bacon.setY(-200);
Message msg = BM.game_panel.game.handler.obtainMessage();
msg.what = 0;
BM.game_panel.game.handler.sendMessage(msg);
}
for (int i = 0; i < BM.TopWalls.size(); i++) {
ArrayList<Point> temp = new ArrayList<Point>(BM.TopWalls.get(i).getArray());
//after we have accest the TopWalls arraylist we can call bump that check TopWalls object's points position with the pig's points
ArrayList<Point> temp2 = new ArrayList<Point>(BM.BottomWalls.get(i).getArray());
//after we have accest the TopWalls arraylist we can call bump that check BottomWalls object's points position with the pig's points
if ((ship.bump(temp.get(0), temp.get(1), temp.get(2), temp.get(3))) || (ship.bump(temp2.get(0), temp2.get(1), temp2.get(2), temp2.get(3))) || ship.death) {
ship.death = true;
game.onStop();
while(f==0) {
MediaPlayer mp = MediaPlayer.create(game, R.raw.explosion_fart);
mp.start();
f++;
}
f++;
Message msg = BM.game_panel.game.handler.obtainMessage();
msg.what = 1;
BM.game_panel.game.handler.sendMessage(msg);
i = BM.TopWalls.size()-1;
if(f == 8){
thread.setRunning(false);
}
}
}
}
In the BarrierManager I have created this update method which takes float dt = MainTheards general time for the game.
TopWalls is ArrayList this ArrayList is composed of top obsticles. Bottom walls is the same but BottomWalls.
//zreb decides if it should create TopWalls or BottomWalls object. This parameter is later transfered to the Barier.update method where I work with it
public void update(float dt){
for (int i=0;i<Num+1; i++) {
int zreb = new Random().nextInt(2);
//{if} to make the spacing bigger
if (i % 5 == 0){
**TopWalls.get(i).update(dt, true, zreb);
BottomWalls.get(i).update(dt, false, zreb);**
if(zreb == 0){
position.set(TopWalls.get(i).getX(), TopWalls.get(i).getY());
}
else{
position.set(BottomWalls.get(i).getX(),BottomWalls.get(i).getY());
}
}
}
}
Now this algoritm in the Barrier.class is where the magic went wrong. This update method takes the float dt mentioned earlier, a boolean variable for determining if the obsticle we work with at that instance is the Top or Bottom, and the zreb random int that determines if the top or bottom obsticle is going to be shown.
//problem! needs to be discussed
public void update(float dt, boolean b, int zreb) {
//checking if the barrier is still there
if (x<-bitmap.getWidth()){
//'b'is passed from the Barriermanager - 'update' method, determining if we have to use monkey-true or farmer-false
int raz = 200;
int vyska = BM.dpos;
//'zreb' helps me to randomly determine if monkey or ballard is going to appear
//here it determines if we are going to have Top or Bottom obsticle in the game
if(zreb == 1) {
vyska = BM.dpos - raz;
}
else {
vyska = BM.dpos + raz;
}
//koniec tohto trienedia
if (b)
{
//setting the y value for the top wall
y = vyska - BM.dl/2 - bitmap.getHeight()/2;
}
else{
//setting the y value for bottom wall
y = vyska + BM.dl/2 + bitmap.getHeight()/2;
}
//setting x-value
x = (int) (x +bitmap.getWidth()*(BM.TopWalls.size()-1));
}
x = (int) (x - BM.game_panel.ShipSpeed*dt);
}
Do you have any idea why this "one-or-the-other" condition is not working properly?
This would help me lot because this error made me deactivate the app from the store.

Simple side-scrolling JFrame game lag, other questions haven't helped?

At the moment I'm trying to make a simple videogame in Java, just for fun. But there seems to be lag, and I'm not sure why it's happening. I'll give the lowdown:
The way it draws is using JFrame, and the actual drawing happens in the ImagePanel class. In ImagePanel, this is how I draw. It includes some things about debugging to show FPS and a timer to show length of run, but I'm not sure if that's important. It goes through multiple ArrayLists to show all the objects on the JFrame.
//Painting
public void paintComponent(Graphics g)
{
//Paint the background with its upper left corner at the upper left corner of the panel
g.drawImage(background, 0, 0, null);
//Paint each image in the foreground where it should go
for(MovingImage img : backgrounds)
{
g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
}
for(MovingImage img : foreground)
{
g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
}
if(g instanceof Graphics2D)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.scale(2, 2);
g2.setColor(Color.WHITE);
String milSpace;
if(timer%100 < 10)
milSpace = "0";
else
milSpace = "";
String secSpace;
if(timer/100 < 10)
secSpace = "0";
else
secSpace = "";
g2.drawString(secSpace + timer/100 + ":" + milSpace + timer%100, 10, 20);
//Debug
if(debug)
{
long currentTime = System.currentTimeMillis();
if (currentTime > nextSecond)
{
nextSecond += 1000;
frameInLastSecond = framesInCurrentSecond;
framesInCurrentSecond = 0;
}
framesInCurrentSecond++;
//g2.drawString("LagMS:" + (-frameRate - 10) + " FPS:" + frameInLastSecond, 20, 40); <-includes "lag"
g2.drawString("FPS:" + frameInLastSecond, 20, 40);
}
}
}
//Replaces the list of foreground images with the one given, and repaints the panel
public void updateImages(ArrayList<MovingImage> newForeground, ArrayList<MovingImage> newBackgrounds)
{
foreground = newForeground;
backgrounds = newBackgrounds;
//time checking
long time = System.currentTimeMillis();
lastTime = time;
repaint(); //This repaints stuff... you don't need to know how it works
}
Inside the primary class I made that includes a tick system, which causes it to be painted in the first place.
public void tick()
{
long lastTime = System.currentTimeMillis();
int place = 0;
boolean go = true;
while(go)
{
long time = System.currentTimeMillis(); //The current time
if(time - 10 > lastTime) //Update every .01 seconds, or 1 TICK (if time - 10 > lastTime)
{
lastTime = time;
//Reset the last time
place++;
imagePanel.incTime();
for(MovingImage object : movingObjects)
{
if(object instanceof Building)
{
object.incrementPosition(); //Augment position by velocity
if(place%500 == 0)//If 5 seconds have passed...
{
((Building) object).speedUp();//make it go a little faster!
}
}
if(object instanceof Player)
{
if(jumpKeyOn)
((Player) object).jump();//Initiate jump class assuming key is pressed
object.incrementPosition();
((Player) object).constrainPlayerToObjects(movingObjects, yMax);
if(object.getY()>yMax + 1000)
{
go = false;
}
}
}
//Repaint all the things in their new positions, possibly faster
for(MovingImage bg : backgrounds)
{
bg.incrementPosition();
if(place%500 == 0)
bg.setVelocity(bg.getXvel() - 0.1, 0);
}
/*
* Acceleration
*/
//Removes buildings once left screen
int i = 0;
while(i < movingObjects.size())
{
if(movingObjects.get(i) instanceof Building)
{
if(movingObjects.get(i).getX() + movingObjects.get(i).getImage().getWidth(null) < 0)
movingObjects.remove(i);
else
i++;
}
else
i++;
}
imagePanel.updateImages(movingObjects, backgrounds);
}
}
gameOver();
}
It's an endless loop that essentially runs the program. I used multiple ArrayLists in order to put different layers down. What am I doing that's causing it to lag? I'm still fairly new, but I'll answer any questions about the code or provide more details. I couldn't find any other questions that helped.
EDIT: There are some odd things I should mention. Occasionally it runs at nearly the full FPS, but most of the time not. I also noticed that when I ran another java program at the same time, it ran at nearly full speed.
EDIT 2: Should I include the entire primary class code and ImagePanel?

Processing won't display game over screen

I have a processing game i am designing that is supposed to display a game over screen when the game is lost. Unfortuinately, the game over screen never comes up. Here is the function i have it narrowed down to:
if (lives < 1) {
background(0);
textSize(100);
text("You Lost",10,10);
delay(1000);
lives = 10;
x = (int)random(width);
y = (int)random(height / 2);
velocity = new PVector(1,random(-1.4,-0.6));
score = 0;
}
When the amount of lives goes to zero, it pauses for a second and then restarts the game.
I have tried everything i can think of, but still no luck.
So my best guess, having gotten to know the processing language 2 minutes ago is the following:
Because you set the background to black (0) the text can't be seen because it is black as well, maybe try changing the text's color to something else with the fill() method to see if that's causing the issue.
if (lives < 1) {
background(0);
textSize(100);
fill(255, 255, 255); // White text
text("You Lost",10,10);
delay(1000);
lives = 10;
...
Figured it out:
i added a piece of code at the start of my loop:
if (dflag) {
delay(2000);
dflag = false;
}
and then, i put the regular update code in an else statement after it check weather you die:
if (lives < 1) {
// for(int df = 0; df < 1000; df++) {
background(0);
textSize(100);
text("You Lost",100,100);
dflag = true;
//redraw();
lives = 10;
x = (int)random(width);
y = (int)random(height / 2);
velocity = new PVector(1,random(-1.4,-0.6));
score = 0;
} else {
textSize(13);
background(0);
stroke(255);
text(score,10,10);
String l = "";
for (int q = 0; q < lives; q++) { l += "%"; }
text(l,50,10);
strokeWeight(balld);
point(x,y);
strokeWeight(8);
line(paddlex,height - 30,paddlex + paddlew,height-30);
}

Categories