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.
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();
}
}
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);
}
}
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 :\