Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:56:34+00:00 2026-06-01T22:56:34+00:00

I have a JFrame on to which i have a JPanel, and i am

  • 0

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

//**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();}
    }

}

i have a thread, which accepts some new co-ordinates from some other java program thru socket, i am able to accept them , my

System.out.print(xpos+" "+ypos);

works fine, but the repaint() method just after that, doesn’t seem to work, no error, nothing happens.

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,

//   s.SendData(xpos,ypos);

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.

SSCCE PaintBrushServer

 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();}
 }

}
}

SSCCE-PaintBrushClient

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());


 }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:56:35+00:00Added an answer on June 1, 2026 at 10:56 pm

    The Answer to above problem of repaint() method not called from inside the thread on PaintBrushServer, while it is being called on PaintBrushClient is….

    Just remove the DrawPanel dr = new DrawPanel(); from myFrame(JFrame) class, as i already did that in the beginning.

    Thanks all for their comments and suggestions, also @andrewThompson i now know how to provide a good SSCCE..lol

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame inside of which is a jpanel that im using as
I have a JFrame(frm) in which I've added a JPanel (pnl1)..which in turn has
I have a JFrame which has 3 JPanels in GridBagLayout .. Now, when I
I have a JFrame with an associated JPanel which fill the screen, both having
I have a JPanel which has a line, circle, etc. Now when I click
So I have a JFrame , in which it has a bunch of JPanel
I have a JFrame which contains just one JPanel. I have tried setting the
I have a JFrame which contains a JPanel . The frame can be resized
So I have a JFrame which I've added a custom JPanel component. The JPanel
I have a program in which a JPanel is added to a JFrame: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.