I'm in front of a big issue and I find any explanation.
I'm currently playing a little with Java and it seems that my programs are very slow (like 5-10 FPS even if I specify my thread to run at 60 FPS), unless I move my mouse over the window, even if my window doesn't have the focus. I had the same issue with several examples and I give to you a very short one within my issue occurs.
package com.coukaratcha;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BouncingBall extends JFrame {
private int x;
private int y;
private int vx;
private int vy;
public BouncingBall(){
super("Bouncing Ball");
this.x = 300;
this.y = 200;
this.vx = 5;
this.vy = -2;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
final JPanel content = new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillOval(x-10, y-10, 10, 10);
}
};
content.setPreferredSize(new Dimension(600, 400));
content.setBackground(Color.BLACK);
this.setContentPane(content);
Thread thread = new Thread(new Runnable(){
public void run(){
while(true){
updatePhysics();
content.repaint();
try{
Thread.sleep(1000/60);
}
catch (InterruptedException e){
;
}
}
}
});
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
thread.start();
}
public void updatePhysics(){
if (x > 595 || x < 10){
vx*= -1;
}
if (y > 395 || y < 10){
vy*= -1;
}
x+=vx;
y+=vy;
}
public static void main(String[] args) {
BouncingBall ball = new BouncingBall();
}
}
I tried this code on some Windows and it works perfectly, but on my Fedora 20/21 or the Ubuntu of a friend, the slowing strikes again. It's compiled with Java 1.8.
Thank you in advance for your help.
[EDIT] After @jtahlborn invite me to use the so called EDT, without clearly explaining how to do that by the way, I've made some modifications in my code and here is the last version where exactly the same issue occurs. I must do something bad, in this case : someone would be nice enough to explain me how to properly do ? Otherwise, I don't have any idea how to avoid this problem. For new arrivals, I've changed the Thread part (compare the two codes), in the constructor.
My new code :
package com.coukaratcha;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BouncingBall extends JFrame {
private int x;
private int y;
private int vx;
private int vy;
public BouncingBall(){
super("Bouncing Ball");
this.x = 300;
this.y = 200;
this.vx = 5;
this.vy = -2;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
final JPanel content = new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillOval(x-10, y-10, 10, 10);
}
};
content.setPreferredSize(new Dimension(600, 400));
content.setBackground(Color.BLACK);
this.setContentPane(content);
Thread thread = new Thread(new Runnable(){
public void run(){
while(true){
updatePhysics();
EventQueue.invokeLater(new Runnable(){
public void run(){
content.repaint();
}
});
try{
Thread.sleep(1000/60);
}
catch (InterruptedException e){
;
}
}
}
});
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
thread.start();
}
public void updatePhysics(){
if (x > 595 || x < 10){
vx*= -1;
}
if (y > 395 || y < 10){
vy*= -1;
}
x+=vx;
y+=vy;
}
public static void main(String[] args) {
BouncingBall ball = new BouncingBall();
}
}