Dragging a portion of a line to form an arc.

I want help in the drawing of an arc which I want to draw by dragging a portion of a line sement. I Drew the line with this Line2D.Float. I selected two nodes with mouse click. Now I want to drag the line segment, with mouse, bounded by these two nodes to an arc.

I would be grateful for help.

[307 byte] By [skmda] at [2007-11-26 13:38:53]
# 1
Do you mean you want to draw a parabola?How will this arc be defined by the 2 points and the mouse?
Rodney_McKaya at 2007-7-7 22:32:28 > top of Java-index,Security,Cryptography...
# 2
I think I know what you mean. But what is your specific problem. Is it animating the drawling or drawling itself?You are going to need to use Arc2D instead of Line2D.
zadoka at 2007-7-7 22:32:28 > top of Java-index,Security,Cryptography...
# 3

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

public class ArcLines extends JPanel {

Point2D.Double[] points = new Point2D.Double[0];

GeneralPath path = new GeneralPath();

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(Color.blue);

g2.draw(path);

g2.setPaint(Color.red);

for(int j = 0; j < points.length; j++)

g2.fill(new Ellipse2D.Double(points[j].x-2, points[j].y-2, 4, 4));

}

public void setPoint(Point2D.Double p, double x, double y) {

p.setLocation(x, y);

setPath();

repaint();

}

public void setNewPoint(Point p) {

addPoint(new Point2D.Double(p.x, p.y));

setPath();

repaint();

}

private void addPoint(Point2D.Double p) {

Point2D.Double[] temp = new Point2D.Double[points.length+1];

System.arraycopy(points, 0, temp, 0, points.length);

temp[points.length] = p;

points = temp;

}

private void setPath() {

path.reset();

path.moveTo((float)points[0].x, (float)points[0].y);

if(points.length == 2) {

path.lineTo((float)points[1].x, (float)points[1].y);

} else if(points.length == 3) {

Point2D.Double ctrl = getOnLineControlPoint();

float ctrlx = (float)ctrl.x;

float ctrly = (float)ctrl.y;

float x2 = (float)points[1].x;

float y2 = (float)points[1].y;

path.quadTo(ctrlx, ctrly, x2, y2);

}

}

private Point2D.Double getOnLineControlPoint() {

Point2D.Double p = new Point2D.Double();

p.x = 2 * points[2].x - (points[0].x + points[1].x)/2;

p.y = 2 * points[2].y - (points[0].y + points[1].y)/2;

return p;

}

public static void main(String[] args) {

ArcLines arcLines = new ArcLines();

PointMaster pm = new PointMaster(arcLines);

arcLines.addMouseListener(pm);

arcLines.addMouseMotionListener(pm);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(arcLines);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class PointMaster extends MouseInputAdapter {

ArcLines arcLines;

Point2D.Double selectedPoint;

Point2D.Double offset = new Point2D.Double();

boolean dragging = false;

double PROX_LOC = 3.0;

public PointMaster(ArcLines al) {

arcLines = al;

}

public void mousePressed(MouseEvent e) {

Point p = e.getPoint();

Point2D.Double[] points = arcLines.points;

// Set the end points.

if(points.length < 2) {

arcLines.setNewPoint(p);

return;

}

// With end points set, are we selecting an existing point?

for(int j = 0; j < points.length; j++) {

if(points[j].distance(p) <= PROX_LOC) {

selectedPoint = points[j];

offset.x = p.x - points[j].x;

offset.y = p.y - points[j].y;

dragging = true;

break;

}

}

// Or, are we adding the control point to the line?

if(!dragging && points.length < 3 && isOnPath(p))

arcLines.setNewPoint(p);

}

public void mouseReleased(MouseEvent e) {

dragging = false;

}

public void mouseDragged(MouseEvent e) {

if(dragging) {

double x = e.getX() - offset.x;

double y = e.getY() - offset.y;

arcLines.setPoint(selectedPoint, x, y);

}

}

private boolean isOnPath(Point p) {

if(arcLines.points.length == 2) {

double x1 = arcLines.points[0].x;

double y1 = arcLines.points[0].y;

double x2 = arcLines.points[1].x;

double y2 = arcLines.points[1].y;

if(Line2D.ptLineDist(x1, y1, x2, y2, p.x, p.y) <= PROX_LOC)

return true;

}

return false;

}

}

crwooda at 2007-7-7 22:32:28 > top of Java-index,Security,Cryptography...