hello i m writing a program in which i try to write on the frame by mousemotionlistener and mouselistener methods till now i did the writing part and i i want to start that writing by clicking the button Ok then another frame is open in which i can write anything all the code is done in java format.

public class Paint {
 JFrame f = new JFrame;
 JButton ok = new JButton("OK");

 public Paint() {
 bok.addActionListener(new ActionListener() {

 public void actionPerformed(ActionEvent ae) {
  new new2();

 public static void main(String[] args) {
    new Paint();

and by using mouseevents methods for the writing in new2 class
in above class call the new2 to open up new frame in which writing is to be done.

 import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    class new2 extends JPanel implements MouseListener,MouseMotionListener

        int xPressed,yPressed;
        int xReleased,yReleased;

        int xDragged,yDragged;
        JFrame f = new JFrame();

        public  new2()

            System.out.println("Frame ");

                f.setSize(500, 500);



 public void paint(Graphics g)


   public void mouseDragged(MouseEvent me) {
    Graphics g = getGraphics();
    g.drawLine(xPressed, yPressed, me.getX(), me.getY());
    xDragged = me.getX();
    yDragged = me.getY();

public void mouseMoved(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) { }

   public void mousePressed(MouseEvent me) {
    xPressed = me.getX();
     yPressed = me.getY();
public void mouseReleased(MouseEvent me) {}


Recommended Answers

All 34 Replies

so ... What exactly is your question?

LInes 46,47
The only place you can draw to the Graphics is inside the paint method (or paintComponent, which would be better). Swing is double-buffered and anything you draw in other methods is most likely never going to de used.

you haven't needed to do this for about 10 years. f.add(ok); is enough.
Similarly, you use an anonymous inner class for your action listener. In modern Javas a lambda is much simpler to code and to read, and less prone to syntax errors.
Are you sure you are using an up-to-date source of info, such as Oracle's own tutorials?

Anyway - what exactly was your question?

my question is where i m doing wrong to call the mouseevents(mousemotionlistener,mouselisener) in another window which is open after button click (OK).

I have not tried your code, but it looks OK. The way you add the mouse listeners in new2 looks perfectly normal.

Why do you think you are doing something wrong? Do you have error messgaes? Doesn't it work as you expected?

it doesn"t work as i expected....it did catch the mouseevents

means it doesn't draw anything on the other window.

OK! You didn't explain that.
the listeners have to be added to the window where you are clicking/dragging the mouse. What you ned to do is then to pass the info back to the first window.
Here's a good way to do this that can be applied in many other examples as well

create a public method in the first class, like

public void updateDrawing(parameters for pressed and draggged coordinates) {
   take a copy of the parameter values
   call repaint

in the first class override paint and draw the line according to the values

in the second class's mouse listeners, whenever the values change, call the first window's updateDrawing method. To do that you will need areference to the first window. The best way is to pass that as a parameter to the seocnd window's constructor.

could you write it more clearly...

WHich part did you not understand?
I'm not going to give you the code, but I will try to explain what code you need to write.

got the problem is just a silly mistake of mine......

as per you what the mean of making method in paint class and passing
parameters for pressed and draggged coordinates. And take a copy of the parameter values.

its something along the lines of

private int startX, startY, etc
public void updateDrawing(int newStartX, int newStartY ... etc) {
   startX = newStartX;

then you can call that method from the second class to update the first class's values and make it re-draw the line

i didn't get it.... you told me to to make a method in the firt class with parameters of dragged and pressed. ok but i didn't get the repaint()????
call this mwthod to second class??????

what is the use of repaint();

repaint() tells Swing that you want to update your window. Swing adds that request to it's queue of work and when it gets to the top of the queue it calls your paint method where you can re-draw the window's contents.

I'm worried that this conversation is going a bit random. In particular I'm still a bit puzzled why you want to drag the mouse in one wondow but draw in the other window. Is that right?
Can you please explain again exactly what you want to happen in each window.

actually their are two windows one which contain the button(ok) which is use to open up the new window and that window is used drwing purpose by the using mousemotion events. And the problem is that i can open up the new window by clicking on the button of the first window. But the when the new window open i couldn't draw anything on it.

Earlier you said "it did catch the mouseevents ... it doesn't draw anything on the other window" - which is the problem I was addressing. But now it seems you want to draw on the same window as the one where you catch the mouse events?

In that case it would help if you added the new2 object to a JFrame so it is visible on-screen.

ok thnx....done

one thing i also wanted from this that after drawing something on drawing window that is directly synchronized to other pc means when i draw something to that drawing window that drawing thing is copying to other pc.
I knew firstly i should make a socket between them and then? Please give any suggestion about that or any method which is useful for making that possible.

All the stuff I suggested before is about drawing on another window applies perfectly in that case.
First, get it working with two windows in the same program, using the method I suggested.
When that works you will see that just 4 numbers need to be passed from the "mouse" window to the "slave" window each time the mouse is dragged. You can then pass those numbers via a Socket connection between different programs or computers. There are lots of tutorials on how to use Sockets, but please get it working with two windows in the smae program first.

what do mean by four number???????

and after making a socket when i click ok on my pc the window is prpoed up to other pc???

The 4 numbers are your xPressed, yPressed, xReleased, yReleased. They are all you need to draw the same line(s) in another window.

You have a server program running or yur PC. Someone starts a client program an their machine. It connects to your server via a Socket. Then you can send info back & forth. See some tutorials, eg: https://docs.oracle.com/javase/tutorial/networking/sockets/

i want parallel working of both pcs means if i draw something om my pc then it also copy parallely to another pc.

Yes. You said that already. That's what I was answering.

it means only making socket on that button will synchronized the window on which is drawing something

Again, Yes. You said that already. That's what I was answering.

not working james i just made a socket program and call that class to that prgram in which writing work is done here is the code let me know where i m doing wrong here is the code of client.

import java.io.*;  
import java.net.*;  
public class Sockettest {  
public static void main(String[] args) {  
Socket s=new Socket("server ip",6666);  
DataOutputStream dout=new DataOutputStream(s.getOutputStream());  

new class2();
dout.writeUTF("Hello Server"); 

}catch(Exception e){System.out.println(e);}  

here class2 is open in the client side but it also use to open at server side whenever i start the socket but till now it only open it client side only... please let me where i m doing wrong..

That fragment of code opens a socket connection, sends some text, and disconnects. What did you expect?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.