From a8bf3853b43eb96e6f2c484d527652f04825c32c Mon Sep 17 00:00:00 2001 From: dzmipt Date: Wed, 18 Dec 2024 16:53:48 +0100 Subject: [PATCH] do not accept the line if it is set by the same points --- src/studio/ui/chart/ChartPanel.java | 2 +- src/studio/ui/chart/Line.java | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/studio/ui/chart/ChartPanel.java b/src/studio/ui/chart/ChartPanel.java index 96e3946..f03ce6e 100644 --- a/src/studio/ui/chart/ChartPanel.java +++ b/src/studio/ui/chart/ChartPanel.java @@ -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; diff --git a/src/studio/ui/chart/Line.java b/src/studio/ui/chart/Line.java index 529de0d..6c6af3a 100644 --- a/src/studio/ui/chart/Line.java +++ b/src/studio/ui/chart/Line.java @@ -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; @@ -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() { @@ -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; } } @@ -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); @@ -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) {