x, y at mouse click point...

In the following code:

On mouse press: (x, y) appears.
On mouse press: (x, y) disappears.

Problem: The location of the (x, y) needs to be at the mouse-click point. Right now, it just shows it at (50, 50)

Question: How can I change this so that x,y would follow the mouse click point?

Thank you
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Exercise16_8_Part_2 extends JFrame implements MouseListener {
           MyMousePanel mmp;
       
    public Exercise16_8_Part_2(){

        mmp = new MyMousePanel();
        mmp.addMouseListener(this);
        Container c = this.getContentPane();
        c.add(mmp);
        this.setSize(400,250);
        this.setTitle("Exercise16_8 Part 2 (Click to see x,y)");
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }



  public void mouseClicked(MouseEvent e){}

      public void mousePressed(MouseEvent e){

          mmp.setString("("+e.getX() + ", "  +  e.getY()+")");
          //mmp.setString(me.getPoint().getX() + "  "  +  me.getPoint().getY());
          repaint();
      }

      public void mouseReleased(MouseEvent e){
              mmp.setString("");
              repaint();


      }
      public void mouseEntered(MouseEvent e){}
      public void mouseExited(MouseEvent e){}

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

class MyMousePanel extends JPanel {
    String s = "";

  public void setString(String s){
      this.s = s;
  }
    public void paint(Graphics g){
        
        String str=s;
        str=str.replace("(", "");
        str=str.replace(")", "");
        str=str.replace(",", "");
        String[] xy=str.split(" ");
        int x=50;//Integer.parseInt(xy[0]);
        int y=50;//Integer.parseInt(xy[1]);
        g.drawString(s, x,y );

    }
}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
Code from this morning when it prints at the point:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MousePressedUp extends JFrame implements MouseListener {
           MyMousePanel mmp;
    public MousePressedUp(){

         mmp = new MyMousePanel();
          mmp.addMouseListener(this);
        Container c = this.getContentPane();
        c.add(mmp);
        this.setSize(300,300);
        this.setVisible(true);

    }



  public void mouseClicked(MouseEvent me){}

      public void mousePressed(MouseEvent me){

                  mmp.setString(me.getPoint().getX() + "  "  +  me.getPoint().getY());
                mmp.setX((int)me.getPoint().getX());
                  mmp.setY((int)me.getPoint().getY());
           repaint();
      }

      public void mouseReleased(MouseEvent me){
                mmp.setString("");
              repaint();


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

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

class MyMousePanel extends JPanel {
    String s = "";
    int x = 50;
    int y = 50;


   public void setX(int x){
       this.x = x;
   }
    public void setY(int y){
       this.y = y;
   }


  public void setString(String s){
      this.s = s;
  }
    public void paint(Graphics g){

        g.drawString(s, x,y );

    }

    

}

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I was thinking of writing get and set for x, y... Now, I now better.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
 
for_yanCommented:
You are always welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.