Hi guys,
The following program works the way I want it to but there's one small problem; when it loads, it doesn't give a blank frame. It want it to show the output after I click on the blank area. Help me please.
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TrackMouseMovementTest{
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MouseFrame extends JFrame
{
public MouseFrame(){
setTitle("Mouse Movement Tracker");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
MouseComponent component = new MouseComponent();
add(component);
}
public static final int DEFAULT_WIDTH = 500;
public static final int DEFAULT_HEIGHT = 500;
}
class MouseComponent extends JComponent{
public MouseComponent(){
addMouseListener(new MouseHandler());
}
private class MouseHandler extends MouseAdapter{
public void mousePressed(MouseEvent event){
x = event.getX();
y = event.getY();
repaint();
}
}
public void paintComponent(Graphics g){
g.drawString("X: " + x + ",Y: " + y + "",x,y);
g.drawString("Click #: " + counter ++ ,x , y + 16);
}
int x;
int y;
private static int counter=-1;
}