?
Solved

Show x, y on click...

Posted on 2011-10-07
3
Medium Priority
?
310 Views
Last Modified: 2012-05-12
Test class below one mouse drag displays the coordinates of the mouse. I want to change it to;

1. not to show any (x, y) upon lunching it.
2. On click, to show (x, y) at that location and remain there.
3. Upon clicking on a different location step 2 above repeats.

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

public class Test extends JFrame{

    static boolean mouseClicked;
    static String str;
    
    public Test (){
        MovableMessagePanel p=new MovableMessagePanel("Test ");
        setLayout(new BorderLayout());
        add(p);
    }
   
    public static void main(String[]a){
        Exercise16_8 frame=new Exercise16_8();
        frame.setTitle("Test");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,250);
        frame.setVisible(true);
    }
    static class MovableMessagePanel extends JPanel{
        private String message="Welcom";
        private int x=20;
        private int y=20;
        
        public MovableMessagePanel(String s){
            message=s;
            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    x=e.getX();
                    y=e.getY();  
                    repaint();
                    str="("+e.getX()+", "+e.getY()+")";
                }
            });
        }
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(str,x,y);
        }
    }
}

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Amitkumar P earned 2000 total points
ID: 36929263
try the following updated code :

public class Test extends JFrame{

    static boolean mouseClicked;
    static String str;
    
    public Test (){
        MovableMessagePanel p=new MovableMessagePanel("Test ");
        setLayout(new BorderLayout());
        add(p);
    }
   
    public static void main(String[]a){
        Exercise16_8 frame=new Exercise16_8();
        frame.setTitle("Test");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,250);
        frame.setVisible(true);
    }
    static class MovableMessagePanel extends JPanel{
        private String message="Welcom";
        private int x=20;
        private int y=20;
        
        public MovableMessagePanel(String s){
            message=s;
            addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    x=e.getX();
                    y=e.getY();  
                    repaint();
                    str="("+e.getX()+", "+e.getY()+")";
                }
            });
			
			
            addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    x=e.getX();
                    y=e.getY();  
                    repaint();
                    str="("+e.getX()+", "+e.getY()+")";
                }
            });

			
        }
		
		
		
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawString(str,x,y);
        }
    }
}

Open in new window



I've added following fork in the constructor for MovableMessagePanel.

addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent e){
            x=e.getX();
            y=e.getY();  
            repaint();
            str="("+e.getX()+", "+e.getY()+")";
      }
});


0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 36929622
Thank you
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36929689
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 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