title: Repaint() is not called from inside a running thread java
body: <p>I have a JFrame on to which i have a JPanel, and i am trying to do some free hand drawing on to it using paintComponent(). 
  Now, i change the xPOS and yPOS variable from inside the mouseDragged method, and then calls the repaint(), so it works fine</p>
  
  <pre><code>//**PROGRAM 1**-My DrawPanel Class
    public class DrawPanel extends JPanel implements Runnable, MouseMotionListener{
      int xpos = 0;
      int ypos = 0;
      String message;
      DrawPanel(){
          //constructor ini
          addMouseMotionListener(this);
          setBackground(Color.green);
          setSize(500, 400);
          setBounds(10, 10, 500, 400);
          }
  
      public void paintComponent(Graphics g) {
  
        g.setColor(Color.red);
        g.fillOval(xpos, ypos, 5, 5);
        }
  
      @Override
      public void mouseDragged(MouseEvent arg0) {
       xpos= arg0.getX();
           ypos= arg0.getY();
      //   s.SendData(xpos,ypos);
          repaint();
      }
  
      public void run() {
  
          try{
              while((message = reader.readLine())!=null) {
                  int in = message.indexOf("#");
                  xpos = Integer.parseInt(message.substring(0, in));
                  ypos = Integer.parseInt(message.substring(in+1));
                  System.out.print(xpos+" "+ypos);
  
                  repaint();
              }}catch(Exception e){e.printStackTrace();}
      }
  
  }
  </code></pre>
  
  <p>i have a thread, which accepts some new co-ordinates from some other java program thru socket, i am able to accept them , my </p>
  
  <pre><code>System.out.print(xpos+" "+ypos);
  </code></pre>
  
  <p>works fine, but the repaint() method just after that, doesn't seem to work, no error, nothing happens.</p>
  
  <p>FYI, the other java program i mentioned, has the same kind of structure, and i send the co-ordinates from this java program to that one,</p>
  
  <pre><code>//   s.SendData(xpos,ypos);
  </code></pre>
  
  <p>and that one is able to call the repaint() method from inside the thread.
  but just can;t seem to understand, why it is not happening in this one.
  Any help would be appreciated, thanks.</p>
  
  <p><strong>SSCCE</strong> PaintBrushServer</p>
  
  <pre><code> import java.awt.*;
   import java.awt.event.*;
  //import java.awt.image.BufferedImage;
  import java.io.*;
  import java.net.*;
  import javax.imageio.ImageIO;
  import javax.swing.*;
  public class PaintBrush {
  
  DrawPanel dr = new DrawPanel();
   myFrame mf;
   Graphics g1;
   boolean flag=false;
   server s;
   BufferedReader reader;
  PaintBrush(){
  myFrame mf = new myFrame();
  s = new server();
  }
  
  public static void main(String[] args) {
  
      new PaintBrush();
     }
  
  public class ButtonPanel extends JPanel{
  ButtonPanel(){
      add(new myButton());
  }
   }
  
  public class DrawPanel extends JPanel implements Runnable, MouseMotionListener{
  
  int xpos = 0;
   int ypos = 0;
  String message;
  
  DrawPanel(){
      //constructor ini
      addMouseMotionListener(this);
      setBackground(Color.green);
      setSize(500, 400);
      setBounds(10, 10, 500, 400);
      //s = new server();
  
  }
  
  public void paintComponent(Graphics g) {
  
    g.setColor(Color.red);
    g.fillOval(xpos, ypos, 5, 5);
  
  }
  
  @Override
  public void mouseDragged(MouseEvent arg0) {
       xpos= arg0.getX();
       ypos= arg0.getY();
       s.SendData(xpos,ypos);
      repaint();
  }
  
  
  @Override
  public void run() {
      try{
  
          while((message = reader.readLine())!=null) {
  
              int in = message.indexOf("#");
              xpos = Integer.parseInt(message.substring(0, in));
              ypos = Integer.parseInt(message.substring(in+1));
             // System.out.print(xpos+" "+ypos);
             repaint();
          }}catch(Exception e){e.printStackTrace();}
  }
  
  
  
  
  }
   public class myFrame extends JFrame{
   myFrame(){
       DrawPanel dr = new DrawPanel();
      //setBackground(Color.black); 
      setResizable(false);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setVisible(true);
      setSize(665,490);
      getContentPane().add(BorderLayout.CENTER,dr);
      getContentPane().add(BorderLayout.NORTH, new ButtonPanel());
   }
  }public class myButton extends JButton{
  
   myButton(){
       setText("PaintBrushServer");
      // addActionListener(new listen());
  
   }
  
   }
  
  public class server {
    ServerSocket ssock;
   Socket clientSocket;
   PrintWriter writer;
   DataOutputStream os = null;
   server(){
       //ini server
        try {
          ssock = new ServerSocket(42422);
          System.out.println("waiting");
          clientSocket = ssock.accept();
          System.out.println(clientSocket.getRemoteSocketAddress()+" connected\n");
          //writer = new PrintWriter(.getOutputStream());
          InputStreamReader streamReader = new InputStreamReader(clientSocket.getInputStream());
          reader = new BufferedReader(streamReader);
          os = new DataOutputStream(clientSocket.getOutputStream());
          new Thread(dr).start();
        } catch (IOException e) {
  
          e.printStackTrace();
      }
   }
   public void SendData(int x, int y){
      try{ 
          os.writeBytes(x+"#"+y+"\n");
      }catch(Exception e){e.printStackTrace();}
   }
  
  }
  }
  </code></pre>
  
  <p><strong>SSCCE-PaintBrushClient</strong></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseMotionListener;
  import java.io.*;
  import java.net.*;
  import javax.swing.*;
  public class Client {
  int xpos = 0;
  int ypos = 0;
  Boolean flag = false;
  ClientConnect cc ;
  DataOutputStream os = null;
  
  BufferedReader reader;
  Client(){
  //server connect
  ClientConnect cc = new ClientConnect();
  
  }
  public static void main(String[] args) {
  
  new Client();
  }
  
  public class ClientConnect {
  private Socket sock;
  String message;
  ClientConnect(){
     try {
      sock = new Socket("127.0.0.1", 42422);
      InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
      reader = new BufferedReader(streamReader);
      os = new DataOutputStream(sock.getOutputStream());
       //sr = new ServerReader();
       //sr.start();
      new myFrame();
  
  } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  }}
  
  public void SendData(int x, int y){
  try{ 
      os.writeBytes(x+"#"+y+"\n");
  }catch(Exception e){e.printStackTrace();}
  }
  public class DrawPanel extends JPanel implements Runnable, MouseMotionListener{
  
   String message;
  DrawPanel(){
      //constructor ini
      addMouseMotionListener(this);
      setBackground(Color.green);
      setSize(500, 400);
      setBounds(10, 10, 500, 400);
  }
  
  public void paintComponent(Graphics g) {
    g.setColor(Color.red);
    g.fillOval(xpos, ypos, 5, 5);
  
  }
  
  @Override
  public void run() {
  
      // TODO Auto-generated method stub
         try{
  
          while((message = reader.readLine())!=null) {
              int in = message.indexOf("#");
              xpos = Integer.parseInt(message.substring(0, in));
              ypos = Integer.parseInt(message.substring(in+1));
              //System.out.println(message);
              System.out.println(xpos+" "+ypos);
              repaint();
  
          }}catch(Exception e){e.printStackTrace();}
  }
  
  @Override
  public void mouseDragged(MouseEvent arg0) {
      // TODO Auto-generated method stub
       xpos= arg0.getX();
       ypos= arg0.getY();
       //flag = true;
       //System.out.println(xpos);
       //SendData(xpos,ypos);
      repaint();
  }
  
  public void mouseMoved(MouseEvent arg0) {
      }
  }
   public class myButton extends JButton{
   myButton(){
   setText("PaintBrushClient");
   //addActionListener(new listen());
   }
   }
  public class ButtonPanel extends JPanel{
  ButtonPanel(){
      add(new myButton());
  }
  }
  
  public class myFrame extends JFrame{
   myFrame(){
   DrawPanel dr = new DrawPanel();
   new Thread(dr).start();
  setResizable(false);
   setBackground(Color.black); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      setSize(665,490);
      getContentPane().add(BorderLayout.CENTER,dr);
      getContentPane().add(BorderLayout.NORTH, new ButtonPanel());
  
  
   }
  }
  }
  </code></pre>