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;
}

Recommended Answers

All 5 Replies

Introduce own boolean flag 'pressed' and use it to switch needed behavior

Don't paint when value of x & y variable is zero.

public void paintComponent(Graphics g){
  if(x>0 &&  y>0) {
     g.drawString("X: " + x + ",Y: " + y + "",x,y);
     g.drawString("Click #: " + counter ++ ,x , y + 16);
  }
}

As the previous poster said, use a boolean for first click, also increment your counter in mousePressed. If you increment it in paintComonent, counter will be incremented everytime the frame is repainted. For example when you resize your frame, your counter dramatically changes. This is the fixed code.

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() {
		firstClick = false;
		addMouseListener(new MouseHandler());
	}

	private boolean firstClick;

	private class MouseHandler extends MouseAdapter {
		public void mousePressed(MouseEvent event) {
			x = event.getX();
			y = event.getY();
			counter++;
			firstClick = true;
			repaint();
		}
	}

	public void paintComponent(Graphics g) {
		if (firstClick) {
			g.drawString("X: " + x + ",Y: " + y + "", x, y);
			g.drawString("Click #: " + counter, x, y + 16);
		}
	}

	int x;
	int y;
	private static int counter = 0;
}

I appreciate the explanation ...that was very helpful :)

Thank you for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.