Hey there,
I am trying to build an application that listens to my mouse. once I click on my panel, it reads the x, y coordinates of the point I clicked on. Second time I click the panel, it reads the x, y coordinates of this other point and draws a line from the first point to the second.

Here is my code:

import java.awt.*;
import java.applet.Applet;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


public class HelloFromVenus extends Applet implements MouseListener
{
	LineSegment ls = new LineSegment();
	boolean firstclick = false;
	int i = 0;
	public void paint(Graphics g)
	{
	
		g.setColor(Color.BLUE);
		g.fillRect(0, 0, 400, 400);
		ls.draw(g);
		
		addMouseListener(this);
	}
	
	
	public void mouseClicked(MouseEvent ev)
	{
		Point p = ev.getPoint();
	
		if(!firstclick)
		{
			ls.start_x = (int)p.getX();
			ls.start_y = (int)p.getY();
			firstclick = true;
			System.out.println("first click detected");
		}
		else
		{
			ls.end_x = (int)p.getX();
			ls.end_y = (int)p.getY();
			firstclick = false;
			System.out.println("second click detected");
			repaint();
		}
	}
	
	public void mousePressed(MouseEvent ev)
	{

	}
	
	public void mouseReleased(MouseEvent ev)
	{
		
	}
	
	public void mouseEntered(MouseEvent ev)
	{
	
	}
	
	public void mouseExited(MouseEvent ev)
	{
		
	}

	
}

//--------------
//LineSegment class is here
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class LineSegment implements Shape
{
	int start_x = 0;
	int start_y = 0;
	
	int end_x = 0;
	int end_y = 0;
	public void area()
	{
		
	}
	
	public void draw(Graphics g)
	{
		g.setColor(Color.BLACK);
		g.drawLine(start_x, start_y, end_x, end_y);
	}
	
	
}
//---------------
//shape interface is here
import javax.swing.*;
import java.awt.*;

public interface Shape
{
	public void area();
	
	public void draw(Graphics g);
}

Now, the problem is the following:

first click:
point read (first click detected)
second click:
point read (second click detected)
line drawn

third click:
point read (first click detected)
point read (second click detected)
line drawn (a point as first and second click are the same)

fourth click:
point read (first click detected)
point read (second click detected)
line drawn (a point as first and second click are the same)
point read (first click detected)
point read (second click detected)

Basically, except for the first couple of clicks, I can't get a line to show for any two clicks.

Anyone knows what is going on?

thanks.

Try debugging your code by having it print out all of the variables values involved.
The print outs will show you what the code is doing.

I put System.outs inside my if-else block...

System.out.println("first click detected");//inside my if, inside mouseClicked

System.out.println("second click detected");//inside my else, inside mouseClicked

Surprisingly, my third click acts as a triple click, my fourth click as a five-tuple (if I said it correctly)... and so on increasing. I do not know what pattern the increase seems to follow.

My third click should be a click alone, not more than one... I can't see what in my code makes it behave as a multiple click.

I put an int i variable, and removed everything else, it behaved as expected.
inside my if, I'd output i and increase it by one.
inside my else, I'd output i and set it to zero.

everything worked fine.
I suspect repaint is somehow messing my code, but I do not know how

UPDATE: Now that I checked, it seems that after the second click, the third counts as two clicks, the fourth as three.. the x-th as x - 1.

Edited 4 Years Ago by bibiki: n/a

Can you post the print outs that show what you mean? I think that the event object has a time value in it that will show when the event occurred.

Well, this is my code for mousClicked method:

Point p = ev.getPoint();
 
		if(!firstclick)
		{
			ls.start_x = (int)p.getX();
			ls.start_y = (int)p.getY();
			firstclick = true;
			System.out.println("first click detected");
		}
		else
		{
			ls.end_x = (int)p.getX();
			ls.end_y = (int)p.getY();
			firstclick = false;
			System.out.println("second click detected");
			repaint();
		}

I expect that this prints either "first click detected" or "second click detected" every time I click on my applet.

One click should not print both, let alone print both more than one time each. However, what I get is:
the first time I click, it prints out "first click detected". the second time I click, I get "second click detected". So far so good. Now on, everything fails. Right now is my third click on the applet, and what I get is:
"first click detected"
"second click detected"
Now on my fourth click on applet I get:
"first click detected"
"second click detected"
"first click detected"
On my fifth click on applet I get:
"first click detected"
"second click detected"
"first click detected"
"second click detected"
On my n-th click, I get (n - 1) lines printed out
.
.
"first click detected"
"second click detected"
.
.
"first click detected"

Your prints don't show enough useful values.
You need to print: ev, p, start_x, end_x, start_y, end_y

well, if one click prints out both "first click detected" and "second click detected", the point is the same in both instances... what to me is one click seems to be more than one click to my machine.

anyways, I put inside my system outs x and y coordinates and they are the same for all print outs that one click of mine yields.

Here is the output of five clicks on my applet:

----jGRASP exec: appletviewer jgrasphta.htm

first click detected 52 69
second click detected 247 129
first click detected 248 129
second click detected 74 204
first click detected 74 204
second click detected 166 202
first click detected 166 202
second click detected 166 202
first click detected 156 185
second click detected 156 185
first click detected 156 185
second click detected 156 185
first click detected 206 212
second click detected 206 212
first click detected 206 212
second click detected 206 212
first click detected 206 212

----jGRASP: operation complete.

To give you more info on this, see this:

----jGRASP exec: appletviewer jgrasphta.htm

first click detected 52 69 - this is when my first click ends
second click detected 247 129 - this is when my second click ends
first click detected 248 129 - this is when my third click ends
second click detected 74 204
first click detected 74 204 - this is when my fourth click ends
second click detected 166 202
first click detected 166 202
second click detected 166 202 - this is when my fifth click ends
first click detected 156 185
second click detected 156 185
first click detected 156 185
second click detected 156 185 - this is when my sixth click ends
first click detected 206 212
second click detected 206 212
first click detected 206 212
second click detected 206 212
first click detected 206 212 - this is when my seventh click ends

----jGRASP: operation complete.

It seems that code is getting more than one event per click.
Change the end of your print outs to show what is happening by printing out the time the event is received.
...detected at "+ System.currentTimeMillis());

How many times do call the addMouseListener() method?

Edited 4 Years Ago by NormR1: n/a

Comments
I would have started the app from scratch...

time indicates the same as what reading points showed.

here is the output. i removed x, y coordinates, and only let time show with the lines "first/second click detected"

first click detected 1331761419281
second click detected 1331761420703
first click detected 1331761425109
second click detected 1331761425109
first click detected 1331761427234
second click detected 1331761427234
first click detected 1331761427234
second click detected 1331761429937
first click detected 1331761429937
second click detected 1331761429937
first click detected 1331761429937
second click detected 1331761436984
first click detected 1331761436984
second click detected 1331761436984
first click detected 1331761436984
second click detected 1331761436984
first click detected 1331761443187
second click detected 1331761443187
first click detected 1331761443187
second click detected 1331761443187
first click detected 1331761443187
second click detected 1331761443187
first click detected 1331761444984
second click detected 1331761444984
first click detected 1331761444984
second click detected 1331761444984
first click detected 1331761445000
second click detected 1331761445000
first click detected 1331761445000
second click detected 1331761450234
first click detected 1331761450234
second click detected 1331761450234
first click detected 1331761450234
second click detected 1331761450234
first click detected 1331761450234
second click detected 1331761450234
first click detected 1331761450234
second click detected 1331761454562
first click detected 1331761454562
second click detected 1331761454562
first click detected 1331761454578
second click detected 1331761454578
first click detected 1331761454578
second click detected 1331761454578
first click detected 1331761454578
second click detected 1331761454578

I tried it the other way... click and drag for drawing a line from the point I clicked on to the point I dragged to. This worked fine. But now this way got me hooked... I want to know more what is going on.

you are very likely right. I call addMouseListener everytime I repaint. I have it called inside my paint method. let me change that.

great. that worked. thank you a lot NormR1.

I swear I did not put that addMouseListener inside paint myself :)

thank you again.

I don't know who else would have done it.
Do you have a little brother?
Keep him away from your computer.

This question has already been answered. Start a new discussion instead.