11-25-2011, 04:43 AM
(This post was last modified: 11-25-2011, 04:53 AM by tr4nquility.)
It works ^^ Thanks Itaru 
It seems that all kinds of drawing activities must be done inside paintComponent, and the listeners are meant to provide data necessary for drawing... Am I right ?
Would you please explain what this piece of code do ? (I know that it is another way to write "if" statement, but what is the logic behind this ?)
*Edit : I've managed to find my mistake... Before, I only call repaint(). It should be panel.repaint().... And then we must set a flag so that the paintComponent will fill the rectangle just once. This way, when mouseMoved() calls panel.repaint() , only the line is drawn. The rectangle is not refilled again.

It seems that all kinds of drawing activities must be done inside paintComponent, and the listeners are meant to provide data necessary for drawing... Am I right ?
Would you please explain what this piece of code do ? (I know that it is another way to write "if" statement, but what is the logic behind this ?)
Code:
int x = (start.x < end.x) ? start.x : end.x;
int y = (start.y < end.y) ? start.y : end.y;
panel.repaint(x, y, Math.abs(start.x - end.x) + 1, Math.abs(start.y - end.y) + 1);
*Edit : I've managed to find my mistake... Before, I only call repaint(). It should be panel.repaint().... And then we must set a flag so that the paintComponent will fill the rectangle just once. This way, when mouseMoved() calls panel.repaint() , only the line is drawn. The rectangle is not refilled again.