?
Solved

x, y at mouse click point...

Posted on 2011-10-07
4
Medium Priority
?
260 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36934621
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36934715
I was thinking of writing get and set for x, y... Now, I now better.
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 36934716
Thank you
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36934719
You are always welcome.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question