Problem with Ubuntu window focus setting when using Java

Asked

Viewed 178 times

4

I was making a program in which a circle is drawn on the screen and it is walking around the screen, using the classes JFrame to create the window, a thread to control the circle I insert on the screen.

The problem, however, is when I run my program, because when running on my Operating System (Ubuntu 14.04) the circle seems to keep crashing while it is redesigned by the screen, but when moving the mouse inside the program the circle is redesigned perfectly. When tested in another OS the program will work perfectly.

I would like to know if anyone has any idea what I could do, whether in relation to my OS or in relation to my application.

Here are the codes from my circle class that implements Runnable:

public class Circulo extends JComponent implements Runnable{

private Desenho desenhoCirculo = null;
private int velocidadeCirculo_x;
private int velocidadeCirculo_y;
private Color corCirculo;
private ColisaoCirculo colisao;

public Circulo(Desenho desenhoCirculo, int velocidadeCirculo_x, int velocidadeCirculo_y, Color corCirculo, ColisaoCirculo colisao) {
    this.desenhoCirculo = desenhoCirculo;
    this.velocidadeCirculo_x = velocidadeCirculo_x;
    this.velocidadeCirculo_y = velocidadeCirculo_y;
    this.corCirculo = corCirculo;
    this.colisao = colisao;
    new Thread(this).start();
}

public void mexerCirculo( int x, int y ){
    this.desenhoCirculo.setPos_x( this.desenhoCirculo.getPos_x() + x);
    this.desenhoCirculo.setPos_y( this.desenhoCirculo.getPos_y() + y);
}

public Desenho getDesenhoCirculo() {
    return desenhoCirculo;
}

public void setDesenhoCirculo(Desenho desenhoCirculo) {
    this.desenhoCirculo = desenhoCirculo;
}

public int getVelocidadeCirculo_x() {
    return velocidadeCirculo_x;
}

public void setVelocidadeCirculo_x(int velocidadeCirculo_x) {
    this.velocidadeCirculo_x = velocidadeCirculo_x;
}

public int getVelocidadeCirculo_y() {
    return velocidadeCirculo_y;
}

public void setVelocidadeCirculo_y(int velocidadeCirculo_y) {
    this.velocidadeCirculo_y = velocidadeCirculo_y;
}

public Color getCorCirculo() {
    return corCirculo;
}

public void setCorCirculo(Color corCirculo) {
    this.corCirculo = corCirculo;
}

public ColisaoCirculo getColisao() {
    return colisao;
}

public void setColisao(ColisaoCirculo colisao) {
    this.colisao = colisao;
}



@Override
public void paint( Graphics g ){
    g.setColor(corCirculo);
    g.fillOval(desenhoCirculo.getPos_x(), desenhoCirculo.getPos_y(), desenhoCirculo.getLargura(), desenhoCirculo.getAltura());
}

@Override
public void run() {
    while( true ){
        try {
            Thread.sleep(20);
            this.mexerCirculo( this.velocidadeCirculo_x, this.velocidadeCirculo_y );
            this.colisao.checarColisaoComLimitesTela( this );
            repaint();

        } catch (InterruptedException ex) {
            Logger.getLogger(Circulo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

}

  • 1

    The content your thread is too big? Maybe you should post it here.

  • 3

    Your problem seems to be really the time spent for painting. Try to make the painting in the method paintComponent instead of doing in the method paint (since you inherit from JComponent) - and don’t forget to invoke the original parent class method before painting your circle! This other answer may help with some additional explanations (especially about the double buffering): http://answall.com/questions/4703/como-usar-uma-thread-espec%C3%Adfica-de-uma-classe-com-m%C3%Baltiplas-threads/4922#4922

  • 2

    Ubuntu, like most Linux distribution, presents problems with the graphics card drivers. And it ends up with errors like this. You can look if there is the official driver of your video card for that version of Ubuntu.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.