Matt Martin
Matt Martin

Reputation: 855

Java- starting thread issue

Here's part of my simple code.This code contains moveable oval and static oval with coords newX=100,newY=100.I'm trying to achieve moving that movable oval automatically after hitting left mouse button.Move code lines are in new Thread thread.Thrad really starts with clicking mouse button, but nothing happens.Just after doing one move using arrow keys, oval starts moving.I tried calling repaint() method at different places but it doesn't seem to help.Any suggestions? Thank you!

public class Buffer extends JPanel implements Runnable,KeyListener,MouseListener{
public static int x;
public static int y;
public static int newX;
public static int newY;
public static Thread thread;
public static boolean check;
public static JFrame frame;
public static int pointX;
public static int pointY;
public static boolean repaint;


public void paintComponent(Graphics g){
    super.paintComponent(g);

    g.drawOval(x, y, 20, 20);

        newX=100;
        newY=100;
        g.fillOval(newX, newY, 20, 20);

        if(repaint)
            repaint();
}
public static void main(String args[]){
    Buffer z=new Buffer();
    z.setBackground(Color.white);

    frame=new JFrame();
    frame.setSize(500,500);
    frame.addKeyListener(z);
    frame.addMouseListener(z);
    frame.add(z);
    frame.setVisible(true);
    frame.requestFocusInWindow();

    thread=new Thread(){
        public void run(){
            try{
                for(int i=0;i<=5;i++){
                    x=x+i;
                    repaint=true;
                    thread.sleep(1000);

                }
            }catch(InterruptedException v){System.out.println(v);}
           }
        };

}
public void keyPressed(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        x=x-10;
        repaint();
    }
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
        x=x+10;
        repaint();
    }
    if(e.getKeyCode()==KeyEvent.VK_UP){
        y=y-10;
        repaint();
    }
    if(e.getKeyCode()==KeyEvent.VK_DOWN){
        y=y+10;
        repaint();
    }
}

public void mouseClicked(MouseEvent e) {
        thread.start();
    }
}

Upvotes: 0

Views: 113

Answers (2)

Narendra Pathai
Narendra Pathai

Reputation: 41945

I don't know if that is the possible issue here but I am going to give it a try:

Try using AtomicInteger for the x and y variables as the code is not thread-safe.

You are trying to access shared variable across multiple threads and doing increment operation which is not atomic and thus is not thread safe.

private static AtomicInteger x = new AtomicInteger(0); //take the initial value that you need

//where you are incrementing the x
x.incrementAndGet();


//where you want to read x
value = x.get();

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691735

You're modifying shared variables from a thread, and reading them from another, without any kind of synchronization. That's wrong. Every access to a shared variable must be done in a synchronized way, or thread-safe objects (like AtomicInteger for example) should be used.

Moreover, the thread modifies the x value in a loop, but never calls repaint(), so there's no reason for the panel to repaint itself.

Upvotes: 2

Related Questions