S
SPG
Hi All,
I have a bit of a problem that you may be able to solve for me.
I need to draw a line on a canvas, using the awt.graphics class only.
Let me stress from the start I have to support MS JVM here..... Hence I
cannot use and of the 2d functionality (Suckola)
Anyhow, The drawing part is easy, and I have done this. But.. I need to
detect a mouse click on any part of the line itself, so I can allow the user
to move it.
The code posted works, but it only detects intersection inside the rectangle
that is startx, start.y, end.x, end.y
I need to actually refine this so I only get the intersection where the x,y
coords are physically on the line..
Some one out there knows the math on how to do this... Please help!!!
Assume a mouseClick event will fire a call into the intersects(x,y) method..
Cheers,
Steve
import java.awt.*;
public class TrendLine {
private Point _start;
private Point _end;
public TrendLine() {
_start = new Point(0,0);
_end = new Point(0,0);
}
public void setStart(Point _start) {
this._start = _start;
}
public Point getStart() {
return _start;
}
public void setEnd(Point _end) {
this._end = _end;
}
public Point getEnd() {
return _end;
}
public boolean intersects(int x, int y){
if( (x >= _start.x && x <= _end.x)
|| (x <= _start.x && x >= _end.x)){
if( ( y >= _start.y && y <= _end.y)
|| (y <= _start.y && y >= _end.y)){
return true;
}
}
return false;
}
public void draw(Graphics g){
//Draw Line
g.drawLine(getStart().x, getStart().y, getEnd().x, getEnd().y);
g.setColor(c);
}
}
I have a bit of a problem that you may be able to solve for me.
I need to draw a line on a canvas, using the awt.graphics class only.
Let me stress from the start I have to support MS JVM here..... Hence I
cannot use and of the 2d functionality (Suckola)
Anyhow, The drawing part is easy, and I have done this. But.. I need to
detect a mouse click on any part of the line itself, so I can allow the user
to move it.
The code posted works, but it only detects intersection inside the rectangle
that is startx, start.y, end.x, end.y
I need to actually refine this so I only get the intersection where the x,y
coords are physically on the line..
Some one out there knows the math on how to do this... Please help!!!
Assume a mouseClick event will fire a call into the intersects(x,y) method..
Cheers,
Steve
import java.awt.*;
public class TrendLine {
private Point _start;
private Point _end;
public TrendLine() {
_start = new Point(0,0);
_end = new Point(0,0);
}
public void setStart(Point _start) {
this._start = _start;
}
public Point getStart() {
return _start;
}
public void setEnd(Point _end) {
this._end = _end;
}
public Point getEnd() {
return _end;
}
public boolean intersects(int x, int y){
if( (x >= _start.x && x <= _end.x)
|| (x <= _start.x && x >= _end.x)){
if( ( y >= _start.y && y <= _end.y)
|| (y <= _start.y && y >= _end.y)){
return true;
}
}
return false;
}
public void draw(Graphics g){
//Draw Line
g.drawLine(getStart().x, getStart().y, getEnd().x, getEnd().y);
g.setColor(c);
}
}