Results 1 to 4 of 4
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default Displaying Mouse Coordinate problems

    I'm trying to display my mouse coordinates in a JPanel which is added to a JFrame, but whenever I run my program the JFrame only displays the two values that the program is set to first display.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class SmallBox extends JPanel implements ActionListener
    {
    
      public Timer timer;
      public int x = 50;
      public int y = 50;
    
      public SmallBox()
      {
        super();
        setSize(100,50);
        setFocusable(false);
        setBackground(Color.white);
        setDoubleBuffered(true);
        setVisible(true);
        timer = new Timer(10,this);
      }
    
      public void paint(Graphics g)
      {
        super.paint(g);
        g.setColor(Color.BLACK);
        g.clearRect(0,0,getWidth(),getHeight());
        Integer integer = new Integer(5);
        String string1 = integer.toString(x);
        String string2 = integer.toString(y);
        g.drawString(string1,10,10);
        g.drawString(string2,50,10);
      }
        
    
      public void actionPerformed(ActionEvent e)
      {
        Point point = MouseInfo.getPointerInfo().getLocation();
        x = point.x;
        y = point.y;
        repaint();
      }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class Box extends JFrame
    {
    
      public SmallBox small;
    
      public Box()
      {
        super();
        small = new SmallBox();
        add(small);
        setSize(200,150);
        setTitle("Tester");
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
      }
    }
    Java Code:
    public class MainBox
    {
      public static void main(String[] args)
      {
        Box box = new Box();
      }
    }
    I can't figure out what's wrong with it. Maybe it's because Integers and Strings are final, and they keep holding the beginning values, but that doesn't seem logical because the Strings and Integers (should?) be disposed of when the paint(Graphics g) method ends, and new ones created when the paint(Graphics g) method executes again. I don't know if it has to do with the JFrame, also.. I'm a bit lost for what's not happening to re-draw these coordinates :\

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For mouse coordinates, you need a MouseListener and MouseMotionListener (a single MouseAdapter can handle both), and need to add these to the JPanel. An ActionListener that's added to nothing will do, well, nothing.

  3. #3
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Ah, but in this case I didn't.

    I found the bug. I hadn't started the timer after constructing it (silly mistake XD)!

    Now that I've added timer.start(); after timer = new Timer(10,this); the window does indeed display the mouse's coordinates on the screen (not in the JPanel/JFrame, sorry if I didn't make that clear).

    Thanks for making me re-check some of my older code to solve my own problem :D

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. JList displaying problems
    By D.Calladine in forum New To Java
    Replies: 0
    Last Post: 12-03-2010, 12:27 AM
  2. Replies: 3
    Last Post: 02-07-2010, 09:34 PM
  3. Problems displaying and array of JPanels
    By dousedfirepants in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:47 AM
  4. Replies: 1
    Last Post: 08-25-2008, 09:48 PM
  5. Problems with JPanels and displaying
    By Mastergeek666 in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2008, 01:32 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •