Mike Eghtebas
asked on
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
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 );
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
You are always welcome.
ASKER