Skip to content

Commit

Permalink
adding LineInfoFrame: added controls to view dx, dy, x, y
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Nov 17, 2024
1 parent 47c79f7 commit 20308fa
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/studio/ui/chart/ChartConfigPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ public LegendIcon getLegendIcon(int index) {
}

public void addLine(Line line) {
String title = "Line " + (1+ listLines.getListSize());
line.setTitle(title);
lines.add(line);
listLines.add("Line " + (1+ listLines.getListSize()), line.getIcon());
listLines.add(title, line.getIcon());

new LineInfoFrame(line);
}

private void refresh() {
Expand Down
29 changes: 28 additions & 1 deletion src/studio/ui/chart/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Line extends AbstractAnnotation implements XYAnnotation {
private final ChartPanel chartPanel;
private Point2D.Double p0, p1;
private LegendIcon icon;
private String title = "";

private Point screenP0, screenP1;
private boolean selected = false;
Expand Down Expand Up @@ -106,6 +107,7 @@ public void moveTo(Point2D.Double p) {
p0 = new Point2D.Double(x0, y0);
p1 = p;
refresh();
fireAnnotationChanged();
}

public void dragTo(Point p) {
Expand All @@ -122,6 +124,31 @@ public void dragTo(Point p) {
p0 = chartPanel.toPlot(screenP0);
p1 = chartPanel.toPlot(screenP1);
refresh();
fireAnnotationChanged();
}

public double getDX(double dy) {
return dy * (p1.x - p0.x) / (p1.y - p0.y);
}

public double getDY(double dx) {
return dx * (p1.y - p0.y) / (p1.x - p0.x);
}

public double getX(double y) {
return intersectHorizontal(new Line2D.Double(p0, p1), y);
}

public double getY(double x) {
return intersectVertical(new Line2D.Double(p0, p1), x);
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public LegendIcon getIcon() {
Expand Down Expand Up @@ -168,6 +195,6 @@ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis dom
g2.setPaint(icon.getColor());
g2.setStroke(stroke);
g2.drawLine(screenP0.x, screenP0.y, screenP1.x, screenP1.y);

}

}
155 changes: 155 additions & 0 deletions src/studio/ui/chart/LineInfoFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package studio.ui.chart;

import studio.ui.GroupLayoutSimple;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.DoubleConsumer;

public class LineInfoFrame extends JFrame {

private final Line line;
private final JLabel lblDX = new JLabel("");
private final JLabel lblDY = new JLabel("");
private final JLabel lblX = new JLabel("");
private final JLabel lblY = new JLabel("");
private final JTextField txtTitle = new JTextField();
private final JTextField txtDX = new JTextField();
private final JTextField txtDY = new JTextField();
private final JTextField txtX = new JTextField();
private final JTextField txtY = new JTextField();

private double dx = 1;
private double dy = Double.NaN;
private double x = 0;
private double y = Double.NaN;
private boolean lockDX = true;
private boolean lockX = true;

public LineInfoFrame(Line line) {
this.line = line;
line.addChangeListener(e -> refresh());
initComponents();
}


private JComponent createPanelLabel(JLabel label, Runnable action) {
JPanel panel = new JPanel(new BorderLayout(0,0));
panel.add(label, BorderLayout.EAST);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
action.run();
refresh();
}
});
return panel;
}

private void performAction(JTextField textField, DoubleConsumer action) {
try {
double value = Double.parseDouble(textField.getText());
action.accept(value);
} catch (NumberFormatException e) {}
refresh();
}

private JTextField withAction(JTextField textField, DoubleConsumer action) {
textField.addActionListener(e -> performAction(textField, action));
textField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
performAction(textField, action);
}
});
return textField;
}

private void initComponents() {
setTitle(getTitle());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);

JLabel lblTitle = new JLabel("Title");



JPanel content = new JPanel();
GroupLayoutSimple layout = new GroupLayoutSimple(content);

layout.setStacks(
new GroupLayoutSimple.Stack()
.addLine(Box.createGlue(),lblTitle)
.addLine(createPanelLabel(lblDX, () -> lockDX=true ) )
.addLine(createPanelLabel(lblDY, () -> lockDX=false ))
.addLine(createPanelLabel(lblX, () -> lockX=true ))
.addLine(createPanelLabel(lblY, () -> lockX=false )),
new GroupLayoutSimple.Stack()
.addLine(txtTitle)
.addLine(withAction(txtDX, this::updateDX))
.addLine(withAction(txtDY, this::updateDY))
.addLine(withAction(txtX, this::updateX))
.addLine(withAction(txtY, this::updateY))
);

refresh();
setContentPane(content);
pack();
setSize(getPreferredSize());
setVisible(true);
}

@Override
public String getTitle() {
return line.getTitle() + " info";
}

private String fmtLabel(String text, boolean locked) {
if (locked) {
return "<html><u>" + text + "</u></html>";
} else {
return text;
}
}

private void updateDX(double newDX) {
dx = newDX;
}

private void updateDY(double newDY) {
dy = newDY;
}

private void updateX(double newX) {
x = newX;
}

private void updateY(double newY) {
y = newY;
}

private void refresh() {
if (lockDX) dy = line.getDY(dx);
else dx = line.getDX(dy);

if (lockX) y = line.getY(x);
else x = line.getDX(y);

txtDX.setText("" + dx);
txtDY.setText("" + dy);
txtX.setText("" + x);
txtY.setText("" + y);

txtTitle.setText(line.getTitle());

lblDX.setText(fmtLabel("Δx", lockDX));
lblDY.setText(fmtLabel("Δy", !lockDX));
lblX.setText(fmtLabel("x", lockX));
lblY.setText(fmtLabel("y", !lockX));

}

}

0 comments on commit 20308fa

Please sign in to comment.