Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Why isn't the MouseListener showing the X and Y cords like I want it to?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Why isn't the MouseListener showing the X and Y cords like I want it to?

    It should work. I have a MouseListener and I told it to make a new window that will show, in brackets which it's not, the point where the mouse is on the first window. Well, I have a hard time getting both windows to show at same time as clicking on one minimizes the other unless I shrink them both.

    I edited stuff out to make the code shorter and only contain what appears to be directly related to the problem.

    However, don't pitch a fit and groan "It won't compile!" if I mistakenly deleted something important or missed something and deleted only half of it. I tried.

    Anyway, I was trying to get the location of the mouse on one screen to show up on the other and to repaint the JLabel, but it always says 0.0 for both no matter where the mouse is.

    Also, why do my windows keep opening up minimized when the program starts?

    I was hoping to track the Mouse Location so that I can figure out where to draw stuff later and not have to play guessing games for hours in order to find the best location to draw something to make it just right.



       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       public class PaintDemo1 extends JFrame
       {
          static final int HOUR = 0;
          static final int TWELVE = 12;
          static final int TWENTY_FOUR = 24;
          static final int THIRTY_SIX = 36;
          static final int FORTY_EIGHT = 48;
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          private int xLoc;
          private int xLoc2, xLoc3, xLoc4;
          private int yLoc,yLoc2,yLoc3,yLoc4;
          private int timesCalledCounter;
          private String time;
          private int hour, minute, second;
          private Timer clockTimer;
         // private ClockListener cl;
         //  private int a;
          private double xLocation;
          private double yLocation;
        //  private AnalogClockListener acli;
     
     
     
          private class AnalogClockListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
     
     
             }
     
     
     
     
          }
     
          public class ShowMouseLocation extends JFrame implements ActionListener
          {
     
     
             private JLabel positions;
             public ShowMouseLocation()
             {
     
     
                positions = new JLabel();
                add(positions);
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
                repaint();
     
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                Double int2 = (Double) xLocation;
                String str2 = int2.toString();
                Double int3 = (Double) yLocation;
     
                String str3 = int3.toString();
                String str = "<BR>" + str2+ "</BR>" + "<BR>"+ str3 +"</BR>";
                positions.setText(str);
     
     
             }
          }
     
     
          public PaintDemo1()
          {
     
     
             timesCalledCounter = 0;
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(0, animator);
             timer.setDelay(1000);
             timer.start();
     
     
     
     
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
     
             setContentPane(jsp);
     
     
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemo1 pd1 = new PaintDemo1();
             pd1.setVisible(true);
     
          }
     
          public class FountainAnimator extends JPanel implements ActionListener, MouseListener 
          {
             private ShowMouseLocation sml;
             private Timer aTimer;
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
                sml = new ShowMouseLocation();
                sml.setVisible(true);
     
                aTimer = new Timer(0,sml);
                aTimer.setDelay(100);
     
             }
     
             public void mouseClicked(MouseEvent e)
             {
     
     
             }
     
             public void mouseExited(MouseEvent e){}
             public void mouseEntered(MouseEvent e){}
             public void mouseReleased(MouseEvent e){}
             public void mousePressed(MouseEvent e){
                xLocation = e.getXOnScreen();
                yLocation = e.getYOnScreen();
                System.out.println(xLocation);
             	sml.repaint();
             }
     
     
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
     
     
             }
     
     
     
          }
     
     
     
     
     
     
       }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Why are all of these component's on the editor weird?

    Also, the smiles are limited and there is a message popping up in yellow every once in while while I type that says "Auto-Saved" that I don't ever recall having seen before in the past.


    ^^
    Also, why is it storing my last text typed despite a page refreshed?

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    You can get the mouse position using this code - http://www.javaprogrammingforums.com...-position.html

    Quote Originally Posted by javapenguin
    Why are all of these component's on the editor weird?

    Also, the smiles are limited and there is a message popping up in yellow every once in while while I type that says "Auto-Saved" that I don't ever recall having seen before in the past.
    I upgraded the forum software last night. The new editor is part of this upgrade.
    There was a slight problem originally but this has been fixed.

    As far as im aware, the "Auto-Saved" feature is for just incase the browser crashes or there is a problem when you are about to post. It saves your message as a draft.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. The Following User Says Thank You to JavaPF For This Useful Post:

    javapenguin (June 13th, 2011)

  5. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Quote Originally Posted by javapenguin View Post
    It should work.

    I edited stuff out to make the code shorter and only contain what appears to be directly related to the problem.

    However, don't pitch a fit and groan "It won't compile!" if I mistakenly deleted something important or missed something and deleted only half of it. I tried.
    You clearly didn't even try to compile it... and sure enough, it doesn't compile. How do you know it demonstrates the problem if you haven't even compiled it?

    You have to understand that it's a waste of time complaining that it doesn't work, then posting code that won't compile. Even if I wanted to waste my time fixing it, I'm going to end up debugging code that isn't the code you're having a problem with.

    If you really want help with this problem, post compilable code that demonstrates the problem.

  6. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Having said the above, I don't actually see anywhere that you add a MouseListener to a component, so I wouldn't expect any mouse action to be recognized. Of course, that may be part of the code you deleted...

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I had a larger program that DID compile. However, after being told again and again and again and again to make a SSCEE I decided to remove all the parts that weren't relevant to the problem.

    I tried to get all of the stuff out but I can't compile the smaller program if I use the same title.

    It's possible I might have lost a bracket or something.

    In any case, JavaPF gave me a page to look at and I'm going to.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I tried the code JavaPF suggested but it's not working.

    In fact, it's not displaying anything at all now.

    What I'm trying to do is use a Timer on the FountainAnimator class to keep repainting the JLabel in the ShowMouseLocation class to the position of the mouse inside the FountainAnimator class.

    This was because I'm trying to make an analog clock and it'd help to figure out the positions instead of having to spend a long time guessing.

    To ensure that the code will compile, I've given the entire program that I've had.

    However, I'm convinced that the problem is within either the ShowMouseLocation class and/or is some glitch with the Timer in the FountainAnimator class or something else like calling the wrong repaint() somewhere inside the ShowMouseLocation class or something.

       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       import java.awt.MouseInfo;
       public class PaintDemo1 extends JFrame implements ActionListener
       {
          static final int HOUR = 0;
          static final int TWELVE = 12;
          static final int TWENTY_FOUR = 24;
          static final int THIRTY_SIX = 36;
          static final int FORTY_EIGHT = 48;
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          private int xLoc;
          private int xLoc2, xLoc3, xLoc4;
          private int yLoc,yLoc2,yLoc3,yLoc4;
          private int timesCalledCounter;
          private String time;
          private int hour, minute, second;
          private Timer clockTimer;
          private ClockListener cl;
         //  private int a;
          private double xLocation;
          private double yLocation;
          private AnalogClockListener acli;
     
     
     
          private class AnalogClockListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
     
     
             }
     
     
     
     
          }
     
          public class ShowMouseLocation extends JFrame implements ActionListener
          {
     
     
             private JLabel positions;
             private String str;
             public ShowMouseLocation()
             {
     
     
                positions = new JLabel();
                add(positions);
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
     
                xLocation = MouseInfo.getPointerInfo().getLocation().x;
                yLocation = MouseInfo.getPointerInfo().getLocation().y;
     
     
     
                str = "<br>"+xLocation + "</br>" + "<br>" + yLocation + "</br>";
                System.out.println(str);
     
                repaint();
     
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                positions.setText(str);
     
     
     
             }
          }
     
          private class ClockListener implements ActionListener
          {
     
     
     
             public void actionPerformed(ActionEvent e)
             {
                Calendar cal = Calendar.getInstance();
     
                int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
     
                minute = cal.get(Calendar.MINUTE);
                second = cal.get(Calendar.SECOND);
     
                if (hourOfDay < 12)
                { // beginning of if
                   if (minute < 10)
                   { // beginning of if
     
                      if (second < 10)
                      { // beginning of if
                         hour = hourOfDay;
                         time = hour + ":0" + minute + ":0" + second + " A.M.";
     
     
                      } // end if
                      else
                      { // begin else
                         hour = hourOfDay;
                         time = hour + ":0" + minute + ":" + second + " A.M.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second < 10)
                      { // begin if 
                         hour = hourOfDay;
                         time = hour + ":" + minute + ":0" + second + " A.M.";
                      } // end if
     
                      else
                      { // begin else
                         hour = hourOfDay;
                         time = hour + ":" + minute + ":" + second + " A.M.";
                      } // end else
     
                   } // end else
     
                } // end if
     
                else if (hourOfDay == 12)
                {  // begin else if 
                   if (minute < 10)
                   { // beginning of if
     
                      if (second < 10)
                      { // beginning of if
                         hour = hourOfDay;
                         time = hour + ":0" + minute + ":0" + second + " P.M.";
     
     
                      } // end if
                      else
                      { // begin else
                         hour = hourOfDay;
                         time = hour + ":0" + minute + ":" + second + " P.M.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second < 10)
                      { // begin if 
                         hour = hourOfDay;
                         time = hour + ":" + minute + ":0" + second + " P.M.";
                      } // end if
     
                      else
                      { // begin else
                         hour = hourOfDay;
                         time = hour + ":" + minute + ":" + second + " P.M.";
                      } // end else
     
                   } // end else
     
     
                } // end else if
     
                else
                { // begin else
                   if (minute < 10)
                   { // beginning of if
     
                      if (second < 10)
                      { // beginning of if
                         hour = hourOfDay - 12;
                         time = hour + ":0" + minute + ":0" + second + " P.M.";
     
     
                      } // end if
                      else
                      { // begin else
                         hour = hourOfDay - 12;
                         time = hour + ":0" + minute + ":" + second + " P.M.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second < 10)
                      { // begin if 
                         hour = hourOfDay - 12;
                         time = hour + ":" + minute + ":0" + second + " P.M.";
                      } // end if
     
                      else
                      { // begin else
                         hour = hourOfDay - 12;
                         time = hour + ":" + minute + ":" + second + " P.M.";
                      } // end else
     
                   } // end else
                } // end else
     
                animator.repaint();
             } // end method
     
          } // end class
          public PaintDemo1()
          {
     
     
             timesCalledCounter = 0;
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
             timer = new Timer(0, animator);
             timer.setDelay(1000);
             timer.start();
     
             cl = new ClockListener();
             clockTimer = new Timer(0,cl);
     
             clockTimer.setDelay(1000);
             clockTimer.start();
     
     
     
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
     
             setContentPane(jsp);
     
     
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemo1 pd1 = new PaintDemo1();
             pd1.setVisible(true);
     
          }
     
     
     
          public class FountainAnimator extends JPanel implements ActionListener, MouseListener 
          {
             private ShowMouseLocation sml;
             private Timer aTimer;
     
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
                sml = new ShowMouseLocation();
                sml.setVisible(true);
     
                aTimer = new Timer(0,sml);
                aTimer.setDelay(100);
                System.out.println(xLocation);
                System.out.println(yLocation);
     
     
     
             }
     
             public void mouseClicked(MouseEvent e)
             {
     
     
             }
     
             public void mouseExited(MouseEvent e){}
             public void mouseEntered(MouseEvent e){}
             public void mouseReleased(MouseEvent e){}
             public void mousePressed(MouseEvent e){
                xLocation = e.getXOnScreen();
                yLocation = e.getYOnScreen();
                System.out.println(xLocation);
                sml.repaint();
             }
     
             private int getTimeInterval()
             {
                if (minute == HOUR)
                   return HOUR;
     
     
                else if (minute == TWELVE)
                   return TWELVE;
     
                else if (minute == TWENTY_FOUR)
                   return TWENTY_FOUR;
     
                else if (minute == THIRTY_SIX)
                   return THIRTY_SIX;
     
                else 
                   return FORTY_EIGHT;
     
             }
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
     
                drawSidewalk(g);
                g.setColor(new Color(88,88,88));
                Polygon poly = new Polygon();
     
                poly.addPoint(200, 200);
                poly.addPoint(400, 200);
                poly.addPoint(400, 300);
                poly.addPoint(100, 300);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
     
                g.drawPolygon(poly);
     
                Polygon poly2 = new Polygon();
                poly2.addPoint(200, 200);
                poly2.addPoint(250, 100);
                poly2.addPoint(450, 110);
                poly2.addPoint(400, 200);
                g.setColor(Color.BLUE);
                g.fillPolygon(poly2);
     
                g.setColor(Color.BLACK);
                g.drawLine(200, 200, 250, 100);
                g.drawLine(250, 100, 450, 110);
                g.drawLine(450, 110, 400, 200);
     
     
                Polygon poly3 = new Polygon();
     
                poly3.addPoint(100, 300);
                poly3.addPoint(100, 200);
                poly3.addPoint(250, 100);
                poly3.addPoint(200, 200);
     
                g.setColor(new Color (88, 88,88));
                g.fillPolygon(poly3);
     
                g.setColor(Color.BLACK);
                g.drawLine(100, 300, 100, 200);
                g.drawLine(100, 200, 250, 100);
     
                Polygon poly4 = new Polygon();
                g.setColor(new Color(88,88,88));
     
                poly4.addPoint(450, 110);
                poly4.addPoint(510, 270);
                poly4.addPoint(400,300);
                poly4.addPoint(400, 200);
                g.fillPolygon(poly4);
                g.setColor(Color.BLACK);
                g.drawPolygon(poly4);
     
     
     
     
                drawFountain(g);
     
     
                animateFountain(g);
     
                drawLake(g);
     
                drawDigitalClock(g);
     
                drawRegularClock(g);
     
             }
     
             public void drawParkShelter(Graphics g)
             {
     
             }
     
     
             public void drawFountain(Graphics g)
             {
                g.setColor(new Color(95,95,95));
                g.drawOval(300, 120, 65, 65);
                g.setColor(Color.BLACK);
                g.drawOval(320, 140, 25, 25);
                g.setColor(new Color(95,95,95));
                g.fillOval(320, 140, 25, 25);
     
                g.setColor(new Color(50,50,50));
                Polygon poly = new Polygon();
                poly.addPoint(325, 153);
                poly.addPoint(325, 125);
                poly.addPoint(330, 115);
                poly.addPoint(340, 125);
                poly.addPoint(340, 153);
                g.fillPolygon(poly);
     
                g.setColor(Color.BLACK);
                g.drawPolygon(poly);
     
     
             }
     
             public void drawSidewalk(Graphics g)
             {
     
                g.setColor(new Color(183,183,183));
                g.fill3DRect(100,50,420,300,true);
                g.setColor(Color.BLACK);
                g.draw3DRect(100,50,420,300, true);
     
     
     
     
             }
     
     
             public void drawRegularClock(Graphics g)
             {
                g.setColor(Color.BLACK);
                g.drawOval(3000,3000, 150,150);
                g.fillOval(2990,2990,170,170);
                g.setColor(Color.WHITE);
                g.fillOval(3000,3000,150,150);
                g.setColor(Color.BLACK);
                g.setFont(new Font("Arial Bold", Font.BOLD, 17));
                g.drawString("12", 3065, 3015);
                g.setColor(Color.DARK_GRAY);
                g.drawLine(3072, 3020, 3072, 3005);
                g.setColor(Color.BLACK);
                g.drawString("6", 3071, 3150);
                g.setColor(Color.DARK_GRAY);
                g.drawLine(3073,3135, 3073, 3150);
     
     
     
     
     
             }
             public void drawDigitalClock(Graphics g)
             {
     
     
                Polygon poly = new Polygon();
     
                poly.addPoint(1000,2485);
                poly.addPoint(982, 2525);
                poly.addPoint(1189,2525);
                poly.addPoint(1172, 2485);
     
                g.setColor(Color.BLACK);
                g.fillPolygon(poly);
                g.setColor(Color.LIGHT_GRAY);
                g.drawPolygon(poly);
                g.setColor(Color.YELLOW);
                g.setFont(new Font("Arial Bold Italic", Font.BOLD + Font.ITALIC, 27));
                g.drawString(time, 1003,2510);
     
     
             }
     
     
     
     
     
             public void animateFountain(Graphics g)
             {
     
     
     
     
                g.setColor(new Color(0,159,206));
                g.fillOval(xLoc,yLoc, 10, 10);
                g.fillOval(xLoc2,yLoc2,10,10);
                g.fillOval(xLoc3,yLoc3, 10,10);
                g.fillOval(xLoc4, yLoc4, 10,10);
                g.setColor(Color.BLACK);
                g.drawOval(xLoc, yLoc, 10,10);
                g.drawOval(xLoc2, yLoc2, 10,10);
                g.drawOval(xLoc3, yLoc3, 10,10);
                g.drawOval(xLoc4, yLoc4, 10,10);
     
     
     
     
             // repaint();
             }
     
     
             public void drawLake(Graphics g)
             {
                g.setColor(new Color(9,34, 136));
                g.fillOval(500,500, 300, 200);
                g.setColor(Color.BLACK);
                g.drawOval(500,500,300,200);
     
     
     
     
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
     
     
     
     
                if (timesCalledCounter ==0)
                {
     
                   xLoc =  325;
                   yLoc = 125;
     
     
     
     
                   timesCalledCounter++;
                }
     
                else if (timesCalledCounter ==1)
                {
     
     
                   xLoc = 350;
                   yLoc = 138;
                   timesCalledCounter++;
                }
     
                else if (timesCalledCounter ==2)
                {
     
     
                   xLoc =373;
                   yLoc =  150;
     
     
     
     
                   timesCalledCounter++;
                }
     
                else
                {
     
     
                   xLoc = 385;
                   yLoc =  170;
                   timesCalledCounter = 0;
                }
     
     
     
                animator.repaint();
     
     
     
             }
     
          }
     
          public void actionPerformed(ActionEvent e)
          {
            // animator.repaint();
          	// Do I even need this listener?
          }
     
     
     
     
       }

    The problem wouldn't be caused by my setting autoScrolls to true for the FountainAnimator object, would it?

    I don't know what autoscrolls are but what I had been looking for when I wrote that part was to get the thing to resize withiout adding any new components to it, but I figured out that revalidate() was what I was looking for for that problem.

    Is this possibly a contentPane issue for ShowMouseLocation as I didn't ever call setContentPane() ?

    The ClockListener and stuff like that are only there because in the earlier, smaller code, I must have either forgotten a bracket and/or not erased a variable that refereed to one of those removed classes.

    Also, the MouseListeners in FountainAnimator no longer are needed. They're not the problem, I think, either.

    Also, the methods like drawLake() and such are not important to the problem being discussed. They are only there to ensure that the code will compile.

    The most likely spots that the problem is in are:

     public class ShowMouseLocation extends JFrame implements ActionListener
          {
     
     
             private JLabel positions;
             private String str;
             public ShowMouseLocation()
             {
     
     
                positions = new JLabel();
                add(positions);
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
     
                xLocation = MouseInfo.getPointerInfo().getLocation().x;
                yLocation = MouseInfo.getPointerInfo().getLocation().y;
     
     
     
                str = "<br>"+xLocation + "</br>" + "<br>" + yLocation + "</br>";
                System.out.println(str);
     
                repaint();
     
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                positions.setText(str);
     
     
     
             }
          }

    and

     public class FountainAnimator extends JPanel implements ActionListener, MouseListener 
          {
             private ShowMouseLocation sml;
             private Timer aTimer;
     
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
                sml = new ShowMouseLocation();
                sml.setVisible(true);
     
                aTimer = new Timer(0,sml);
                aTimer.setDelay(100);
                System.out.println(xLocation);
                System.out.println(yLocation);
     
     
     
             }

    Hmmmmm...also thought of something.

    It appears that most of my other Timers are inside the PaintDemo1 constructor and the Timer I'm using for the MouseLocation thingy is not. Could that be the problem?

    Also, I don't think I'm setting it to track the mouse position of the FountainAnimator thing like I wanted the code to but don't know how to do that exactly.
    Last edited by javapenguin; June 13th, 2011 at 01:36 PM.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    it's not working.
    Please explain.
    Try debugging the code by adding printlns to it to show the execution flow.

    You need to understand your program's logic and the problem you are having so that you can write a small executable program that we can compile and test with.

    Can you work on one problem at a time?

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I printed out the values already and they indicate that the mouse is always at 0.0, 0.0.

    Here, I've made the program shorter to get at the problem quicker:
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       import java.awt.MouseInfo;
       public class PaintDemoX extends JFrame
       {
     
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          private int xLoc;
          private int xLoc2, xLoc3, xLoc4;
          private int yLoc,yLoc2,yLoc3,yLoc4;
          private int timesCalledCounter;
          private String time;
          private int hour, minute, second;
       //   private Timer clockTimer;
         // private ClockListener cl;
         //  private int a;
          private double xLocation;
          private double yLocation;
       //   private AnalogClockListener acli;
     
     
     
     
     
          public class ShowMouseLocation extends JFrame implements ActionListener
          {
     
     
             private JLabel positions;
             private String str;
             public ShowMouseLocation()
             {
     
     
                positions = new JLabel();
                add(positions);
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
     
                xLocation = MouseInfo.getPointerInfo().getLocation().x;  
             /* I had hoped that these two methods calls for xLocation and yLocation would be getting the value of the mouse x and y
             locations in the FountainAnimator object animator, or any FountainAnimator object perhaps for that matter(at least animator at the very least).
             Anyway, I had hoped that it would show this location in the other JFrame.  However, the JFrame has a hard time staying open at the same time as my main JFrame.  If I minimize them both to really small size, then I can show both at same time, otherwise clicking on one of the JFrames makes the other automatically minimize.  But that's not the real issue here, though it certainly is annoying.  Actually, what I had hoped for was to originally show
             the mouse pointer in a JPanel thing that would be placed below the FountainAnimator object and then combine thse two into a single panel that shows both or something.  Kinda like how in JGrasp and other compilers, it shows the position where the key is ready to type, the vertical bar thingy that shows where you're about to type, anyway, it shows its position below in a smaller JPanel or whatever.  Anyway, I tried that and it wouldn't work quite well as one of the panels appeared too small or  something.  As it is, I am trying it this way unless you could show me how to use the layouts to attach a small JPanel thing that only has the JLabel on it to the bottom of the FountainAnimator object or something like that.  */
                yLocation = MouseInfo.getPointerInfo().getLocation().y;
     
     
     
                str = "<br>"+xLocation + "</br>" + "<br>" + yLocation + "</br>";
                System.out.println(str);
     
                repaint(); // which repaint() am I calling: the PaintDemo1 or the ShowMouseLocation paint method?
     
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                positions.setText(str);
                // why is str blank?
     
     
     
             }
          }
     
     
          public PaintDemoX()
          {
     
     
             timesCalledCounter = 0;
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
     
            // timer = new Timer(0, animator);
           //  timer.setDelay(1000);
           //  timer.start();
     
          //   cl = new ClockListener();
            // clockTimer = new Timer(0,cl);
     
            // clockTimer.setDelay(1000);
            // clockTimer.start();
     
     
     
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
     
             setContentPane(jsp);
     
     
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemoX pd1 = new PaintDemoX();
             pd1.setVisible(true);
     
          }
     
     
     
          public class FountainAnimator extends JPanel 
          {
             private ShowMouseLocation sml;
             private Timer aTimer;
     
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
                sml = new ShowMouseLocation();
                sml.setVisible(true);
     
                aTimer = new Timer(0,sml);
                aTimer.setDelay(100);
                System.out.println(xLocation);
                System.out.println(yLocation);
     
     
     
             }
     
     
     
     
     
     
          }
     
     
     
     
     
     
       }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I've told you about this before and you seem to have ignored it.
    When I execute your program all I get is a titlebar, no window???
    Please fix it and post working code next time.
    Attached Images Attached Images

  12. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    It is working.

    It just starts out minimized.

    I don't know what's causing that either.

    Can't fix that.

    Try enlarging it.

    Perhaps it's because I'm using JGrasp to compile this or something like that.

    I can't figure out how to start it out maximized.
    Last edited by javapenguin; June 13th, 2011 at 03:05 PM.

  13. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    @javapenguin - I still don't see where you're adding a MouseListener to a component. Please explain how you think the MouseListener action methods are going to be called - or is the MouseListener no longer an issue anymore, you seem to have removed it from your latest post.

    I'm no longer sure what you're asking for help with...

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I can't believe that you have over 1000 posts and ignore something as simple as starting the app window with a size.

    Plus you don't exit the JVM when you close the window so it leaves orphan java programs running.

    Please fix these two problems before posting any more code.

  15. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    I don't need a MouseListener apparently.

    MouseInfo class should work instead. However, Norm can't seem to get it to maximize the window.

    I don't know what's causing that either.

    I can maximize mine, though mine always starts out minimized.

    All I wanted was to track the Mouse Position inside the FountainAnimator class and get it to show that position on the JFrame in the ShowMouseLocation class.

    I thought that I needed a MouseListener earlier but now have found that I don't. However, the new code that should be working isn't working.

    The MouseInfo code isn't working that is.

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Good luck on your project. Until I see the two changes I mentioned, I'm done here.

  17. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Ok, I fixed the window problem(I think) and now it will exit on close.
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       import java.awt.MouseInfo;
       public class PaintDemoX extends JFrame
       {
     
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          private int xLoc;
          private int xLoc2, xLoc3, xLoc4;
          private int yLoc,yLoc2,yLoc3,yLoc4;
          private int timesCalledCounter;
          private String time;
          private int hour, minute, second;
       //   private Timer clockTimer;
         // private ClockListener cl;
         //  private int a;
          private double xLocation;
          private double yLocation;
       //   private AnalogClockListener acli;
     
     
     
     
     
          public class ShowMouseLocation extends JFrame implements ActionListener
          {
     
     
             private JLabel positions;
             private String str;
             public ShowMouseLocation()
             {
     
                Dimension dim2 = new Dimension(50,50);
                setMinimumSize(dim2);
                positions = new JLabel();
                add(positions);
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
     
                xLocation = MouseInfo.getPointerInfo().getLocation().x;  
             /* I had hoped that these two methods calls for xLocation and yLocation would be getting the value of the mouse x and y
             locations in the FountainAnimator object animator, or any FountainAnimator object perhaps for that matter(at least animator at the very least).
             Anyway, I had hoped that it would show this location in the other JFrame.  However, the JFrame has a hard time staying open at the same time as my main JFrame.  If I minimize them both to really small size, then I can show both at same time, otherwise clicking on one of the JFrames makes the other automatically minimize.  But that's not the real issue here, though it certainly is annoying.  Actually, what I had hoped for was to originally show
             the mouse pointer in a JPanel thing that would be placed below the FountainAnimator object and then combine thse two into a single panel that shows both or something.  Kinda like how in JGrasp and other compilers, it shows the position where the key is ready to type, the vertical bar thingy that shows where you're about to type, anyway, it shows its position below in a smaller JPanel or whatever.  Anyway, I tried that and it wouldn't work quite well as one of the panels appeared too small or  something.  As it is, I am trying it this way unless you could show me how to use the layouts to attach a small JPanel thing that only has the JLabel on it to the bottom of the FountainAnimator object or something like that.  */
                yLocation = MouseInfo.getPointerInfo().getLocation().y;
     
     
     
                str = "<br>"+xLocation + "</br>" + "<br>" + yLocation + "</br>";
                System.out.println(str);
     
                repaint(); // which repaint() am I calling: the PaintDemo1 or the ShowMouseLocation paint method?
     
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                positions.setText(str);
                // why is str blank?
     
     
     
             }
          }
     
     
          public PaintDemoX()
          {
     
     
             timesCalledCounter = 0;
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
             animator.setOpaque(true);
             Dimension dim = new Dimension(200,200);
             animator.setMinimumSize(dim);
             this.setMinimumSize(dim);
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
     
             setContentPane(jsp);
     
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemoX pd1 = new PaintDemoX();
             pd1.setVisible(true);
     
          }
     
     
     
          public class FountainAnimator extends JPanel 
          {
             private ShowMouseLocation sml;
             private Timer aTimer;
     
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
                sml = new ShowMouseLocation();
                sml.setVisible(true);
     
                aTimer = new Timer(0,sml);
                aTimer.setDelay(100);
                System.out.println(xLocation);
                System.out.println(yLocation);
     
     
     
             }
     
     
     
     
     
          }
     
     
     
     
     
     
       }

    I was able to set a minimum size.

    If there's like a setMaximized(true) option, I can't figure out what it is.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    What will cause the actionPerformed method to be called? Is that code working properly?

  19. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    sml = new ShowMouseLocation();
    sml.setVisible(true);

    aTimer = new Timer(0,sml);
    aTimer.setDelay(100);


    Should be working correctly, but it probably isn't.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Should be, but it probably isn't.
    Why not read the API doc or the tutorial to see how to use a Timer?

  21. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Wait a minute.....



    I never started the Timer, did I?

  22. #21
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Yep, that kinda would explain a lot.

    However, why isn't my JLabel breaking into lines apart with the <br> code?

    Now the Mouse Locator is working!

    But why is the <br> still literally there instead of line breaking?

  23. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Ok, but how do I get it to show the position relative to the Mouse location on FountainAnimator?

    So far it's just showing the location relative to the screen.
    Last edited by javapenguin; June 13th, 2011 at 04:00 PM.

  24. #23
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    Ok, the Component method of

    getMousePostion().get(X) is out too apparently. It's just causing a Null Pointer Exception.

    Should I try using a MouseEvent object or will that not work either?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

    When you get errors, please copy and paste the full text of the error message here. Also post the code that generated the error.

  26. #25
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why isn't the MouseListener showing the X and Y cords like I want it to?

       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import java.util.*;
       import java.io.*;
       import javax.swing.Timer;
       import java.awt.MouseInfo;
       public class PaintDemoX extends JFrame
       {
     
     
     
          private FountainAnimator animator;
          private Timer timer;
          private JScrollPane jsp;
          private int xLoc;
          private int xLoc2, xLoc3, xLoc4;
          private int yLoc,yLoc2,yLoc3,yLoc4;
          private int timesCalledCounter;
          private String time;
          private int hour, minute, second;
       //   private Timer clockTimer;
         // private ClockListener cl;
         //  private int a;
          private double xLocation;
          private double yLocation;
       //   private AnalogClockListener acli;
     
     
     
     
     
          public class ShowMouseLocation extends JFrame implements ActionListener
          {
     
     
             private JLabel positions;
             private String str;
             private String str2;
             public ShowMouseLocation()
             {
     
                Dimension dim2 = new Dimension(50,50);
                setMinimumSize(dim2);
                positions = new JLabel();
                add(positions);
     
             }
     
     
             public void actionPerformed(ActionEvent e)
             {
     
     
             /* I had hoped that these two methods calls for xLocation and yLocation would be getting the value of the mouse x and y
             locations in the FountainAnimator object animator, or any FountainAnimator object perhaps for that matter(at least animator at the very least).
             Anyway, I had hoped that it would show this location in the other JFrame.  However, the JFrame has a hard time staying open at the same time as my main JFrame.  If I minimize them both to really small size, then I can show both at same time, otherwise clicking on one of the JFrames makes the other automatically minimize.  But that's not the real issue here, though it certainly is annoying.  Actually, what I had hoped for was to originally show
             the mouse pointer in a JPanel thing that would be placed below the FountainAnimator object and then combine thse two into a single panel that shows both or something.  Kinda like how in JGrasp and other compilers, it shows the position where the key is ready to type, the vertical bar thingy that shows where you're about to type, anyway, it shows its position below in a smaller JPanel or whatever.  Anyway, I tried that and it wouldn't work quite well as one of the panels appeared too small or  something.  As it is, I am trying it this way unless you could show me how to use the layouts to attach a small JPanel thing that only has the JLabel on it to the bottom of the FountainAnimator object or something like that.  */
     
     
     
                MouseEvent me = new MouseEvent(animator, MouseEvent.MOUSE_CLICKED, 0, MouseEvent.BUTTON1, 0,0, 1, false);
     
                xLocation = animator.getMousePosition().getX();
                yLocation = animator.getMousePosition().getY();
     
                Double xL = xLocation;
                Double yL = yLocation;
                str = xL.toString();
                str2 = yL.toString();
     
                System.out.println(str);
     
                repaint(); // which repaint() am I calling: the PaintDemo1 or the ShowMouseLocation paint method?
     
             }
     
             public void paint(Graphics g)
             {
                super.paint(g);
                positions.setText("<html><br>"+str+ "</br> <br>" + str2 + "</br></html>");
                // why is str blank?
     
     
     
             }
          }
     
     
          public PaintDemoX()
          {
     
     
             timesCalledCounter = 0;
             setVisible(true);
             animator = new FountainAnimator();
             animator.setVisible(true);
             animator.setOpaque(true);
             Dimension dim = new Dimension(200,200);
             animator.setMinimumSize(dim);
             this.setMinimumSize(dim);
     
     
             jsp = new JScrollPane(animator, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS  , JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             Dimension d = new Dimension(9000,9000);
             animator.setPreferredSize(d);
             animator.revalidate();
             animator.setSize(d);
     
             animator.setAutoscrolls(true);
     
     
             setContentPane(jsp);
     
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
     
          public static void main(String[] args)
          {
             PaintDemoX pd1 = new PaintDemoX();
             pd1.setVisible(true);
     
          }
     
     
     
          public class FountainAnimator extends JPanel 
          {
             private ShowMouseLocation sml;
             private Timer aTimer;
     
     
             public FountainAnimator()
             {
                super(new BorderLayout());
                setBackground(Color.GREEN);
                sml = new ShowMouseLocation();
                sml.setVisible(true);
     
                aTimer = new Timer(0,sml);
                aTimer.setDelay(100);
                aTimer.start();
                System.out.println(xLocation);
                System.out.println(yLocation);
     
     
     
             }
     
     
     
     
     
          }
     
     
     
     
     
     
       }

    animator.getMousePosition().getX() is returning null.

    I'm trying to get the position of the mouse relative to the pixel position of the mouse inside the animator object.

    The FountainAnimator object.

    BTW, making a MouseEvent object and trying that route always yielded the Point (0,0).
    Last edited by javapenguin; June 13th, 2011 at 04:53 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. 3Dmouse mouselistener
    By zimzille in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 03:12 AM
  2. Help with mouselistener and mousemotionlistener pls..
    By rentaw02 in forum Member Introductions
    Replies: 1
    Last Post: March 18th, 2011, 05:45 PM
  3. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  4. [SOLVED] implements MouseListener doesn't work why
    By voltaire in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2010, 04:30 PM
  5. MouseListener & MouseMotionListener
    By JavaLearner in forum AWT / Java Swing
    Replies: 9
    Last Post: March 26th, 2010, 07:18 AM