this is one of the code i am working on. I am trying to get the end points of the line when i click on the the line but it is giving the end points as the same point where the line is clicked
class DrawPanel extends Panel implements MouseListener, MouseMotionListener { public static final int LINES = 0; public static final int POINTS = 1; int mode = LINES; @SuppressWarnings("rawtypes") static Vector lines = new Vector(); @SuppressWarnings("rawtypes") static Vector colors = new Vector(); int x1,y1; int x2,y2; int mx; int my; int xn, yn; public boolean isLineSelected = false; public boolean mousePressed=false; public boolean mouseReleased=false; public boolean mouseDragged=false; public Point2D lineStart; public Point2D lineEnd; public Rectangle r; public Point2D pointSelected; public Line2D line; public boolean is;
// boolean isMouseDraggingBox = false;
public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void setDrawMode(int mode) {
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}
public void deleteLine() { x1=y1=x2=y2=0;
repaint();
}
@SuppressWarnings("unchecked")
public void mouseDragged(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x2 = e.getX();
y2 = e.getY();
// System.out.println(x2+"in dragging of LINES");
// System.out.println(y2+"in dragging of LINES");
// System.out.println(" dragging LINES over");
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x1 = e.getX();
y1 = e.getY();
// System.out.println(x1+"in dragging of LINES");
// System.out.println(y1+"in dragging of LINES");
// System.out.println(" dragging default over");
break;
}
repaint();
}
public void mouseMoved(MouseEvent e) {
}
@SuppressWarnings("unchecked")
public void mousePressed(MouseEvent e) {
e.consume();
System.out.println("Mouse Pressed");
mousePressed=true;
switch (mode) {
case LINES:
//System.out.println("Mouse Pressed LINES case "+isLineSelected);
x1 = e.getX();
y1 = e.getY();
x2 = -1;
// System.out.println(x1+"in pressed of LINES");
// System.out.println(y1+"in pressed of LINES");
// System.out.println(" Pressed LINES over");
break;
case POINTS:
default:
// System.out.println("Mouse Pressed default case "+isLineSelected);
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
x1 = e.getX();
y1 = e.getY();
repaint();
// System.out.println(x1+"in pressed of Default");
// System.out.println(y1+"in pressed of Default");
// System.out.println(" Pressed default over");
break;
}
}
@SuppressWarnings("unchecked")
public void mouseReleased(MouseEvent e) {
e.consume();
System.out.println("Mouse Released");
mouseReleased=true;
xn=e.getX();
yn=e.getY();
switch (mode) {
case LINES:
//System.out.println("Mouse Released LINES case"+isLineSelected);
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
//System.out.println(x1+"in released of LINES");
// System.out.println(y1+"in released of LINES");
// System.out.println(" Released LINES over");
//isMouseDraggingBox = false;
break;
case POINTS:
default:
break;
}
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
e.consume();
//Point2D lineStart;
//Point2D lineEnd;
//mouseReleased=false;
//mousePressed=false;
System.out.println("Mouse Clicked");
mx=e.getX();
my=e.getY();
pointSelected=new Point2D.Double(mx,my);
// System.out.println("Size of vector of lines is"+lines.size());
for(int i=0;i<lines.size();i++)
{
r=(Rectangle)lines.elementAt(i);
if(r.width!=-1)
{
x1=r.x;
y1=r.y;
x2=r.width;
y2=r.height;
lineStart = new Point2D.Double(x1, y1);
lineEnd = new Point2D.Double(x2, y2);
// pointSelected=new Point(mx,my);
//System.out.println("linestart: "+lineStart);
//System.out.println("slineend: "+lineEnd);
/* if(lineStart.equals(lineEnd))
{
i=0;
continue;
}*/
line = new Line2D.Double(lineStart, lineEnd);
if ( line.ptSegDist(mx, my)==0.0 || line.contains(pointSelected) ) {
isLineSelected=true;
break;
}
}
}
System.out.println("isLineselected: "+isLineSelected);
if(isLineSelected){
//deleteLine();
//System.out.println("Start point "+ x1+ ", "+y1);
//System.out.println("EndPoint "+x2+", "+y2);
}
}
//Point lineStart = new Point(x1, y1);
//Point lineEnd = new Point(x2, y2);
public void paint(Graphics g) {
int np = lines.size();
/* draw the current lines */
if(mousePressed){
g.drawString("("+x1+","+y1+")",x1,y1);
}
if(mouseReleased)
{
g.drawString("("+xn+","+yn+")",xn,yn);
}
g.setColor(getForeground());
for (int i=0; i < np; i++) {
Rectangle p = (Rectangle)lines.elementAt(i);
g.setColor((Color)colors.elementAt(i));
if (p.width != -1) {
g.drawLine(p.x, p.y, p.width, p.height);
} else {
g.drawLine(p.x, p.y, p.x, p.y);
}
}
if (mode == LINES) {
g.setColor(getForeground());
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}
what should i do to get correct endpoints....