Skip to content

Commit

Permalink
do not accept the line if it is set by the same points
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Dec 18, 2024
1 parent 2532bf7 commit a8bf385
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/studio/ui/chart/ChartPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private void addLine(MouseEvent e) {
newLine = new Line(this, p);
plot.addAnnotation(newLine);
} else {
newLine.addPoint(p);
if (! newLine.addPoint(p) ) return;
chartChanged(new AnnotationChangeEvent(this, newLine));
notifyListeners(new NewLineEvent(this, newLine));
newLine = null;
Expand Down
14 changes: 8 additions & 6 deletions src/studio/ui/chart/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Line extends AbstractAnnotation implements XYAnnotation {

private Point screenP0, screenP1;
private boolean selected = false;
private boolean off = false;
private boolean off = true;
private boolean init = false;
private boolean visible = true;

Expand All @@ -36,11 +36,13 @@ public Line(ChartPanel chartPanel, Point2D.Double p0) {
plot.getRangeAxis().addChangeListener(e -> refresh());
}

public void addPoint(Point2D.Double p1) {
public boolean addPoint(Point2D.Double p1) {
if (init) throw new IllegalStateException("The line is already initialized");
this.p1 = p1;
init = true;
refresh();
init = ! off;
return init;
}

private void refresh() {
Expand Down Expand Up @@ -69,9 +71,9 @@ private void refresh() {
Point2D.Double p1 = points.get(1);
screenP0 = chartPanel.fromPlot(p0);
screenP1 = chartPanel.fromPlot(p1);
off = true;
} else {
off = false;
} else {
off = true;
selected = false;
}
}
Expand All @@ -89,7 +91,7 @@ private static boolean within(double d0, double d1, double d2) {
}

public double distanceSqr(int x, int y) {
if (!init || !off || !visible) return Double.POSITIVE_INFINITY;
if (!init || off || !visible) return Double.POSITIVE_INFINITY;

double s2 = x*(screenP0.y-screenP1.y) + screenP0.x*(screenP1.y-y) + screenP1.x*(y-screenP0.y);
double l2 = screenDist(screenP0, screenP1);
Expand Down Expand Up @@ -178,7 +180,7 @@ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis dom
return;
}

if (!off || !visible) return;
if (off || !visible) return;

BasicStroke stroke = icon.getStroke();
if (selected) {
Expand Down

0 comments on commit a8bf385

Please sign in to comment.