updating variable on ouseMoved

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 76
Reputation: Lensva has a little shameless behaviour in the past 
Solved Threads: 3
Lensva Lensva is offline Offline
Junior Poster in Training

updating variable on ouseMoved

 
0
  #1
Jun 5th, 2009
Class A has a MouseMotionListener which updates cursorLocation every time its called;
Class B extends A. the idea is to make B's lastSelected update once cursorLocation is updated and println the new coords. i thought paintComponent could be used for that but im sure theres something obvious im missing:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. class A extends JPanel{
  7. Point cursorLocation;
  8.  
  9. A() {
  10. setPreferredSize(new Dimension(200,200));
  11. addMouseMotionListener(new MouseMotionHandler());
  12. }
  13.  
  14. private class MouseMotionHandler extends MouseMotionAdapter {
  15.  
  16. public void mouseMoved(MouseEvent e) { cursorLocation = new Point(e.getX(), e.getY());}
  17.  
  18. }
  19. }
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class B extends A{
  5. Point lastSelected;
  6.  
  7. static public void main(String[] args) {
  8. JFrame frame = new JFrame();
  9. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. frame.setSize(600,400);
  11.  
  12.  
  13. B b = new B();
  14. frame.getContentPane().add(BorderLayout.CENTER,b);
  15. frame.setVisible(true);
  16. }
  17.  
  18. B() {
  19. super();
  20. }
  21. /*
  22. public void paintComponent(Graphics g) {
  23. if (lastSelected != cursorLocation) { lastSelected = new Point(cursorLocation); repaint(); }
  24. System.out.println(lastSelected);
  25. */
  26.  
  27.  
  28. }
  29. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC