- 恢复同代 GC Avalonia 主界面(主页/下载/设置/关于)替换 Flutter - 模型窗口统一 Avalonia;HTML 课件用 WebView.Avalonia 内嵌渲染 - 新增 CoursewareServer(回环 HTTP + Range)与 CoursewareWindow - 彻底移除 Flutter/Dart 前端及构建脚本相关逻辑 - 新增 ./build.sh run 开发运行命令 - 项目源码整理到 C#/ 子目录,新增 Java/ 实现 - 工作日志随项目迁移
641 lines
29 KiB
Java
641 lines
29 KiB
Java
package com.gc.ui;
|
||
|
||
import com.gc.core.I18n;
|
||
import com.gc.core.ModelManifest;
|
||
import com.gc.model.scale.ScaleData;
|
||
import com.gc.model.scale.ScaleMath;
|
||
|
||
import javax.swing.*;
|
||
import javax.swing.border.EmptyBorder;
|
||
import javax.swing.event.ChangeListener;
|
||
import java.awt.*;
|
||
import java.awt.geom.RoundRectangle2D;
|
||
import java.util.Locale;
|
||
import java.util.Random;
|
||
|
||
/** 比例尺计算器:三栏输入 / 可视化标尺 / 三种写法与判读 + 实地尺与练习。 */
|
||
public final class ScalePanel extends JPanel {
|
||
private final ScaleMath.State s = new ScaleMath.State();
|
||
private final ScaleData data;
|
||
private final ModelManifest.Package pkg;
|
||
private boolean updating;
|
||
private final Random rnd = new Random();
|
||
|
||
private final UI.TextField mapField = new UI.TextField();
|
||
private final UI.TextField realField = new UI.TextField();
|
||
private final UI.TextField nField = new UI.TextField();
|
||
private final UI.Select unitBox = new UI.Select(new String[]{"unit.km", "unit.m", "unit.cm"}).display(I18n::t);
|
||
private final UI.Slider mapSlider = new UI.Slider(1, 200, 30, true);
|
||
private final JLabel mapResult = Theme.boldLabel("", 13, Theme.INK);
|
||
private final JLabel realResult = Theme.boldLabel("", 13, Theme.INK);
|
||
private final JLabel nResult = Theme.boldLabel("", 13, Theme.INK);
|
||
private final UI.Card mapCard = new UI.Card();
|
||
private final UI.Card realCard = new UI.Card();
|
||
private final UI.Card scaleCard = new UI.Card();
|
||
private final UI.RoundButton btnReal = UI.button(I18n.t("mode.real"));
|
||
private final UI.RoundButton btnMap = UI.button(I18n.t("mode.map"));
|
||
private final UI.RoundButton btnScale = UI.button(I18n.t("mode.scale"));
|
||
private final JLabel digital = Theme.boldLabel("", 17, Theme.INK);
|
||
private final JLabel verbal = Theme.label("", 12.5f, Theme.INK);
|
||
private final JLabel segmentLabel = Theme.label("", 12.5f, Theme.INK);
|
||
private final JLabel gradeChip = new JLabel();
|
||
private final JLabel gradeNote = Theme.label("", 11.5f, Theme.DIM);
|
||
private final JLabel gradeRule = Theme.label(I18n.t("grade.rule"), 10.5f, Theme.FAINT);
|
||
private final JTextArea stepsArea = new JTextArea();
|
||
private final JPanel stepsWrap = new JPanel(new BorderLayout());
|
||
private final UI.Select landmarkBox = new UI.Select();
|
||
private final JLabel landmarkResult = Theme.boldLabel("", 20, Theme.ACCENT);
|
||
private final JLabel quizPrompt = Theme.label("", 12.5f, Theme.INK);
|
||
private final UI.TextField quizInput = new UI.TextField();
|
||
private final JLabel quizFeedback = Theme.boldLabel("", 12, Theme.SUCCESS);
|
||
private final JLabel quizUnit = Theme.label("", 10.5f, Theme.FAINT);
|
||
private final JLabel quizStreak = Theme.label("", 10.5f, Theme.FAINT);
|
||
private final JPanel quizSteps = new JPanel();
|
||
private final ScaleRuler ruler = new ScaleRuler();
|
||
private ScaleMath.Quiz quiz;
|
||
private int streak;
|
||
|
||
public ScalePanel(ModelManifest.Package pkg) {
|
||
this.data = ScaleData.load(pkg.dataDir());
|
||
this.pkg = pkg;
|
||
setLayout(new BorderLayout());
|
||
setBackground(new Color(0xF4F6FB));
|
||
add(header(), BorderLayout.NORTH);
|
||
JPanel center = new JPanel(new GridBagLayout());
|
||
center.setOpaque(false);
|
||
center.setBorder(new EmptyBorder(12, 16, 8, 16));
|
||
GridBagConstraints gc = new GridBagConstraints();
|
||
gc.fill = GridBagConstraints.BOTH; gc.weighty = 1;
|
||
gc.gridx = 0; gc.gridy = 0; gc.weightx = 0; gc.insets = new Insets(0, 0, 0, 8);
|
||
center.add(left(), gc);
|
||
gc.gridx = 1; gc.weightx = 1; gc.insets = new Insets(0, 0, 0, 8);
|
||
center.add(middle(), gc);
|
||
gc.gridx = 2; gc.weightx = 0; gc.insets = new Insets(0, 0, 0, 0);
|
||
center.add(right(), gc);
|
||
add(center, BorderLayout.CENTER);
|
||
add(bottom(), BorderLayout.SOUTH);
|
||
wire();
|
||
setMode(ScaleMath.SolveMode.REAL);
|
||
newQuiz();
|
||
refresh();
|
||
}
|
||
|
||
private JComponent header() {
|
||
JPanel h = new JPanel(new BorderLayout());
|
||
h.setBackground(Color.WHITE);
|
||
h.setBorder(BorderFactory.createCompoundBorder(
|
||
BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(0xE3E8F3)),
|
||
new EmptyBorder(14, 20, 14, 20)));
|
||
JPanel l = new JPanel();
|
||
l.setOpaque(false);
|
||
l.setLayout(new UI.VStackLayout());
|
||
l.add(Theme.boldLabel(pkg.manifest.localizedName(), 22, new Color(0x111A2E)));
|
||
l.add(Theme.label(pkg.manifest.localizedDescription(), 12, new Color(0x5B6A86)));
|
||
h.add(l, BorderLayout.WEST);
|
||
JPanel r = new JPanel();
|
||
r.setOpaque(false);
|
||
r.setLayout(new UI.VStackLayout());
|
||
JLabel hint = Theme.label(I18n.t("mode.hint"), 10.5f, new Color(0x8A97B1));
|
||
hint.setAlignmentX(Component.RIGHT_ALIGNMENT);
|
||
r.add(hint);
|
||
JPanel modes = new JPanel(new FlowLayout(FlowLayout.RIGHT, 8, 6));
|
||
modes.setOpaque(false);
|
||
modes.add(btnReal); modes.add(btnMap); modes.add(btnScale);
|
||
r.add(modes);
|
||
h.add(r, BorderLayout.EAST);
|
||
return h;
|
||
}
|
||
|
||
private JComponent left() {
|
||
UI.Card card = new UI.Card(new BorderLayout());
|
||
card.setPreferredSize(new Dimension(315, 0));
|
||
JPanel p = new JPanel();
|
||
p.setOpaque(false);
|
||
p.setLayout(new UI.VStackLayout());
|
||
p.setBorder(new EmptyBorder(14, 14, 14, 14));
|
||
JPanel head = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 0));
|
||
head.setOpaque(false);
|
||
JLabel bar = new JLabel();
|
||
bar.setPreferredSize(new Dimension(4, 16));
|
||
bar.setOpaque(true); bar.setBackground(Theme.ACCENT);
|
||
head.add(bar);
|
||
head.add(Theme.boldLabel(I18n.t("input.title"), 13.5f, Theme.INK));
|
||
p.add(head);
|
||
p.add(Box.createVerticalStrut(10));
|
||
|
||
inputCard(mapCard, p, I18n.t("input.map"), mapResult, mapField, null, mapSlider);
|
||
p.add(Box.createVerticalStrut(8));
|
||
inputReal(p);
|
||
p.add(Box.createVerticalStrut(8));
|
||
inputScale(p);
|
||
p.add(Box.createVerticalStrut(10));
|
||
p.add(Theme.label(I18n.t("input.presets"), 11.5f, Theme.FAINT));
|
||
JPanel presets = new JPanel(new GridLayout(0, 2, 6, 6));
|
||
presets.setOpaque(false);
|
||
for (ScaleData.Preset preset : data.presets) {
|
||
UI.RoundButton b = UI.button(I18n.t(preset.nameKey()));
|
||
b.setFont(Theme.font(10.5f));
|
||
b.setBorder(new EmptyBorder(5, 7, 5, 7));
|
||
b.addActionListener(e -> { s.n = preset.n(); ScaleMath.solve(s); refresh(); });
|
||
presets.add(b);
|
||
}
|
||
p.add(presets);
|
||
p.add(Box.createVerticalStrut(10));
|
||
JSeparator sep = new JSeparator(); sep.setForeground(new Color(0xE3E8F3));
|
||
p.add(sep);
|
||
p.add(Box.createVerticalStrut(8));
|
||
UI.RoundButton reset = UI.button(I18n.t("input.reset"));
|
||
reset.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
reset.addActionListener(e -> {
|
||
s.mapCm = 3; s.realCm = 300000; s.n = 100000; setMode(ScaleMath.SolveMode.REAL); ScaleMath.solve(s); refresh();
|
||
});
|
||
p.add(reset);
|
||
p.add(Box.createVerticalStrut(4));
|
||
p.add(Theme.label(I18n.t("input.dragHint"), 10.5f, Theme.FAINT));
|
||
card.add(UI.scroll(p), BorderLayout.CENTER);
|
||
return card;
|
||
}
|
||
|
||
private void inputCard(UI.Card card, JPanel parent, String label, JLabel result,
|
||
JTextField field, UI.Select unit, UI.Slider slider) {
|
||
card.setLayout(new UI.VStackLayout());
|
||
card.setBorder(new EmptyBorder(12, 12, 12, 12));
|
||
card.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
card.setMaximumSize(new Dimension(Integer.MAX_VALUE, slider == null ? 100 : 150));
|
||
JPanel top = new JPanel(new BorderLayout());
|
||
top.setOpaque(false);
|
||
top.add(Theme.label(label, 11.5f, Theme.DIM), BorderLayout.WEST);
|
||
top.add(result, BorderLayout.EAST);
|
||
card.add(top);
|
||
card.add(Box.createVerticalStrut(6));
|
||
JPanel row = new JPanel(new BorderLayout(6, 0));
|
||
row.setOpaque(false);
|
||
Theme.styleField(field);
|
||
field.setPreferredSize(new Dimension(120, 34));
|
||
row.add(field, BorderLayout.CENTER);
|
||
if (unit != null) {
|
||
unit.setPreferredSize(new Dimension(92, 34));
|
||
row.add(unit, BorderLayout.EAST);
|
||
}
|
||
card.add(row);
|
||
if (slider != null) {
|
||
slider.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
slider.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
|
||
card.add(Box.createVerticalStrut(4));
|
||
card.add(slider);
|
||
}
|
||
parent.add(card);
|
||
}
|
||
|
||
private void inputReal(JPanel p) {
|
||
realCard.setLayout(new UI.VStackLayout());
|
||
realCard.setBorder(new EmptyBorder(12, 12, 12, 12));
|
||
realCard.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
realCard.setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));
|
||
JPanel top = new JPanel(new BorderLayout());
|
||
top.setOpaque(false);
|
||
top.add(Theme.label(I18n.t("input.real"), 11.5f, Theme.DIM), BorderLayout.WEST);
|
||
top.add(realResult, BorderLayout.EAST);
|
||
realCard.add(top);
|
||
realCard.add(Box.createVerticalStrut(6));
|
||
JPanel row = new JPanel(new BorderLayout(6, 0));
|
||
row.setOpaque(false);
|
||
Theme.styleField(realField);
|
||
realField.setPreferredSize(new Dimension(120, 34));
|
||
row.add(realField, BorderLayout.CENTER);
|
||
unitBox.setPreferredSize(new Dimension(92, 34));
|
||
row.add(unitBox, BorderLayout.EAST);
|
||
realCard.add(row);
|
||
p.add(realCard);
|
||
}
|
||
|
||
private void inputScale(JPanel p) {
|
||
scaleCard.setLayout(new UI.VStackLayout());
|
||
scaleCard.setBorder(new EmptyBorder(12, 12, 12, 12));
|
||
scaleCard.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
scaleCard.setMaximumSize(new Dimension(Integer.MAX_VALUE, 100));
|
||
JPanel top = new JPanel(new BorderLayout());
|
||
top.setOpaque(false);
|
||
top.add(Theme.label(I18n.t("input.scale"), 11.5f, Theme.DIM), BorderLayout.WEST);
|
||
top.add(nResult, BorderLayout.EAST);
|
||
scaleCard.add(top);
|
||
scaleCard.add(Box.createVerticalStrut(6));
|
||
JPanel row = new JPanel(new BorderLayout(6, 0));
|
||
row.setOpaque(false);
|
||
JLabel prefix = Theme.boldLabel("1 :", 13, Theme.DIM);
|
||
prefix.setBorder(new EmptyBorder(0, 8, 0, 4));
|
||
row.add(prefix, BorderLayout.WEST);
|
||
Theme.styleField(nField);
|
||
nField.setPreferredSize(new Dimension(120, 34));
|
||
row.add(nField, BorderLayout.CENTER);
|
||
scaleCard.add(row);
|
||
p.add(scaleCard);
|
||
}
|
||
|
||
private JComponent middle() {
|
||
UI.Card card = new UI.Card(new BorderLayout());
|
||
JPanel p = new JPanel(new BorderLayout());
|
||
p.setOpaque(false);
|
||
p.setBorder(new EmptyBorder(16, 16, 12, 16));
|
||
JPanel head = new JPanel(new BorderLayout());
|
||
head.setOpaque(false);
|
||
head.add(Theme.boldLabel(I18n.t("ruler.title"), 14, Theme.INK), BorderLayout.WEST);
|
||
head.add(Theme.label(I18n.t("ruler.sameWidth"), 10.5f, Theme.FAINT), BorderLayout.EAST);
|
||
p.add(head, BorderLayout.NORTH);
|
||
p.add(ruler, BorderLayout.CENTER);
|
||
JLabel hint = Theme.label(I18n.t("ruler.hint"), 10.5f, Theme.FAINT);
|
||
hint.setBorder(new EmptyBorder(6, 2, 0, 2));
|
||
p.add(hint, BorderLayout.SOUTH);
|
||
card.add(p, BorderLayout.CENTER);
|
||
return card;
|
||
}
|
||
|
||
private JComponent right() {
|
||
UI.Card card = new UI.Card(new BorderLayout());
|
||
card.setPreferredSize(new Dimension(340, 0));
|
||
JPanel p = new JPanel();
|
||
p.setOpaque(false);
|
||
p.setLayout(new UI.VStackLayout());
|
||
p.setBorder(new EmptyBorder(14, 14, 14, 14));
|
||
|
||
p.add(Theme.boldLabel(I18n.t("forms.title"), 13.5f, Theme.INK));
|
||
p.add(Box.createVerticalStrut(8));
|
||
p.add(formRow(I18n.t("forms.digital"), digital));
|
||
p.add(Box.createVerticalStrut(6));
|
||
p.add(formRow(I18n.t("forms.verbal"), verbal));
|
||
p.add(Box.createVerticalStrut(6));
|
||
p.add(formRow(I18n.t("forms.segment"), segmentLabel));
|
||
p.add(Box.createVerticalStrut(12));
|
||
|
||
UI.Card grade = new UI.Card();
|
||
grade.setLayout(new UI.VStackLayout());
|
||
grade.setBorder(new EmptyBorder(12, 12, 12, 12));
|
||
grade.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
grade.setMaximumSize(new Dimension(Integer.MAX_VALUE, 120));
|
||
JPanel gt = new JPanel(new BorderLayout(8, 0));
|
||
gt.setOpaque(false);
|
||
gt.add(Theme.label(I18n.t("grade.title"), 11.5f, Theme.DIM), BorderLayout.WEST);
|
||
gradeChip.setFont(Theme.bold(11));
|
||
gradeChip.setOpaque(true);
|
||
gradeChip.setBorder(new EmptyBorder(3, 8, 3, 8));
|
||
gt.add(gradeChip, BorderLayout.EAST);
|
||
grade.add(gt);
|
||
grade.add(Box.createVerticalStrut(6));
|
||
grade.add(gradeNote);
|
||
grade.add(Box.createVerticalStrut(4));
|
||
grade.add(gradeRule);
|
||
p.add(grade);
|
||
p.add(Box.createVerticalStrut(12));
|
||
|
||
JPanel stepsHead = new JPanel(new BorderLayout());
|
||
stepsHead.setOpaque(false);
|
||
stepsHead.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
|
||
stepsHead.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
stepsHead.add(Theme.boldLabel(I18n.t("steps.title"), 13.5f, Theme.INK), BorderLayout.WEST);
|
||
UI.RoundButton toggle = UI.button(I18n.t("steps.show"));
|
||
toggle.ghost();
|
||
toggle.setFont(Theme.font(10.5f));
|
||
toggle.addActionListener(e -> {
|
||
boolean show = !stepsWrap.isVisible();
|
||
stepsWrap.setVisible(show);
|
||
toggle.setText(show ? I18n.t("steps.hide") : I18n.t("steps.show"));
|
||
p.revalidate(); p.repaint();
|
||
});
|
||
stepsHead.add(toggle, BorderLayout.EAST);
|
||
p.add(stepsHead);
|
||
p.add(Box.createVerticalStrut(6));
|
||
stepsArea.setEditable(false);
|
||
stepsArea.setLineWrap(true);
|
||
stepsArea.setWrapStyleWord(true);
|
||
stepsArea.setFont(Theme.font(11));
|
||
stepsArea.setForeground(Theme.DIM);
|
||
stepsArea.setBackground(new Color(0xF7F9FE));
|
||
stepsArea.setBorder(new EmptyBorder(8, 8, 8, 8));
|
||
stepsWrap.setOpaque(false);
|
||
stepsWrap.add(stepsArea, BorderLayout.CENTER);
|
||
stepsWrap.setVisible(false);
|
||
stepsWrap.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
stepsWrap.setMaximumSize(new Dimension(Integer.MAX_VALUE, 170));
|
||
p.add(stepsWrap);
|
||
p.add(Box.createVerticalStrut(10));
|
||
|
||
p.add(Theme.boldLabel(I18n.t("pit.title"), 13.5f, Theme.INK));
|
||
p.add(Box.createVerticalStrut(5));
|
||
for (String k : new String[]{"pit.1", "pit.2", "pit.3"})
|
||
p.add(Theme.label(I18n.t(k), 10.5f, Theme.DIM));
|
||
card.add(UI.scroll(p), BorderLayout.CENTER);
|
||
return card;
|
||
}
|
||
|
||
private JComponent formRow(String label, JLabel value) {
|
||
JPanel p = new JPanel(new BorderLayout(10, 0));
|
||
p.setOpaque(false);
|
||
p.setMaximumSize(new Dimension(Integer.MAX_VALUE, 34));
|
||
JLabel l = Theme.label(label, 11.5f, Theme.FAINT);
|
||
l.setPreferredSize(new Dimension(52, 20));
|
||
p.add(l, BorderLayout.WEST);
|
||
p.add(value, BorderLayout.CENTER);
|
||
return p;
|
||
}
|
||
|
||
private JComponent bottom() {
|
||
JPanel wrap = new JPanel(new GridLayout(1, 2, 16, 0));
|
||
wrap.setOpaque(false);
|
||
wrap.setBorder(new EmptyBorder(4, 16, 14, 16));
|
||
wrap.setPreferredSize(new Dimension(0, 190));
|
||
|
||
UI.Card field = new UI.Card();
|
||
field.setLayout(new UI.VStackLayout());
|
||
field.setBorder(new EmptyBorder(12, 14, 12, 14));
|
||
field.add(Theme.boldLabel(I18n.t("field.title"), 13, Theme.INK));
|
||
field.add(Box.createVerticalStrut(4));
|
||
field.add(Theme.label(I18n.t("field.hint"), 10.5f, Theme.FAINT));
|
||
field.add(Box.createVerticalStrut(8));
|
||
for (ScaleData.Landmark lm : data.landmarks) landmarkBox.addItem(I18n.t(lm.nameKey()));
|
||
landmarkBox.setMaximumSize(new Dimension(Integer.MAX_VALUE, 34));
|
||
landmarkBox.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
field.add(landmarkBox);
|
||
field.add(Box.createVerticalStrut(10));
|
||
landmarkResult.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
field.add(landmarkResult);
|
||
|
||
UI.Card quizCard = new UI.Card();
|
||
quizCard.setLayout(new UI.VStackLayout());
|
||
quizCard.setBorder(new EmptyBorder(12, 14, 12, 14));
|
||
JPanel qh = new JPanel(new BorderLayout());
|
||
qh.setOpaque(false);
|
||
qh.add(Theme.boldLabel(I18n.t("quiz.title"), 13, Theme.INK), BorderLayout.WEST);
|
||
qh.add(quizStreak, BorderLayout.EAST);
|
||
quizCard.add(qh);
|
||
quizCard.add(Box.createVerticalStrut(5));
|
||
quizPrompt.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
quizCard.add(quizPrompt);
|
||
quizCard.add(Box.createVerticalStrut(6));
|
||
JPanel ans = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 0));
|
||
ans.setOpaque(false);
|
||
Theme.styleField(quizInput);
|
||
quizInput.setPreferredSize(new Dimension(100, 32));
|
||
ans.add(quizInput);
|
||
UI.RoundButton check = UI.primary(I18n.t("quiz.check"));
|
||
check.setBorder(new EmptyBorder(6, 12, 6, 12));
|
||
check.addActionListener(e -> checkQuiz());
|
||
UI.RoundButton next = UI.button(I18n.t("quiz.next"));
|
||
next.setBorder(new EmptyBorder(6, 10, 6, 10));
|
||
next.addActionListener(e -> newQuiz());
|
||
ans.add(check);
|
||
ans.add(next);
|
||
ans.add(quizUnit);
|
||
quizCard.add(ans);
|
||
quizCard.add(Box.createVerticalStrut(5));
|
||
quizFeedback.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||
quizCard.add(quizFeedback);
|
||
quizSteps.setOpaque(false);
|
||
quizSteps.setLayout(new UI.VStackLayout());
|
||
quizCard.add(quizSteps);
|
||
|
||
wrap.add(field);
|
||
wrap.add(quizCard);
|
||
return wrap;
|
||
}
|
||
|
||
private void wire() {
|
||
ChangeListener slider = e -> {
|
||
if (updating) return;
|
||
s.mapCm = mapSlider.getValue() / 10.0;
|
||
ScaleMath.solve(s);
|
||
refresh();
|
||
};
|
||
mapSlider.addChangeListener(slider);
|
||
mapField.addActionListener(e -> {
|
||
try { s.mapCm = Double.parseDouble(mapField.getText().trim()); ScaleMath.solve(s); refresh(); }
|
||
catch (Exception ex) { mapField.setText(ScaleMath.num(s.mapCm)); }
|
||
});
|
||
realField.addActionListener(e -> {
|
||
Double cm = ScaleMath.tryParseReal(realField.getText(), String.valueOf(unitBox.getSelectedItem()));
|
||
if (cm != null) { s.realCm = cm; ScaleMath.solve(s); refresh(); }
|
||
else refresh();
|
||
});
|
||
unitBox.addActionListener(e -> refresh());
|
||
nField.addActionListener(e -> {
|
||
try { s.n = Double.parseDouble(nField.getText().trim().replace(",", "")); ScaleMath.solve(s); refresh(); }
|
||
catch (Exception ex) { nField.setText(ScaleMath.formatN(s.n)); }
|
||
});
|
||
btnReal.addActionListener(e -> setMode(ScaleMath.SolveMode.REAL));
|
||
btnMap.addActionListener(e -> setMode(ScaleMath.SolveMode.MAP));
|
||
btnScale.addActionListener(e -> setMode(ScaleMath.SolveMode.SCALE));
|
||
landmarkBox.addActionListener(e -> refresh());
|
||
}
|
||
|
||
private void setMode(ScaleMath.SolveMode m) {
|
||
s.mode = m;
|
||
ScaleMath.solve(s);
|
||
refresh();
|
||
}
|
||
|
||
private void styleMode(UI.RoundButton b, boolean active) {
|
||
b.primary = active;
|
||
b.ghost = !active;
|
||
b.repaint();
|
||
}
|
||
|
||
private void refresh() {
|
||
updating = true;
|
||
styleMode(btnReal, s.mode == ScaleMath.SolveMode.REAL);
|
||
styleMode(btnMap, s.mode == ScaleMath.SolveMode.MAP);
|
||
styleMode(btnScale, s.mode == ScaleMath.SolveMode.SCALE);
|
||
mapField.setText(ScaleMath.num(s.mapCm));
|
||
String unit = String.valueOf(unitBox.getSelectedItem());
|
||
realField.setText(ScaleMath.num(ScaleMath.toUnit(s.realCm, unit)));
|
||
nField.setText(ScaleMath.formatN(s.n));
|
||
mapSlider.setValue((int) Math.round(Math.max(0.1, Math.min(20, s.mapCm)) * 10));
|
||
mapResult.setText(ScaleMath.num(s.mapCm) + " cm");
|
||
realResult.setText(ScaleMath.formatRealCm(s.realCm).replace("unit.", ""));
|
||
nResult.setText("N " + ScaleMath.formatN(s.n));
|
||
highlight(mapCard, s.mode == ScaleMath.SolveMode.REAL);
|
||
highlight(realCard, s.mode == ScaleMath.SolveMode.MAP);
|
||
highlight(scaleCard, s.mode == ScaleMath.SolveMode.SCALE);
|
||
|
||
digital.setText(ScaleMath.digital(s.n));
|
||
verbal.setText(I18n.f("forms.verbalTpl", ScaleMath.verbalDistance(s.n).replace("unit.", "")));
|
||
ScaleMath.Segment seg = ScaleMath.pickSegment(s.n, 60, 120);
|
||
segmentLabel.setText(I18n.f("forms.segmentDesc", ScaleMath.segmentLabel(seg.metersPerDiv()), seg.divCount()));
|
||
ScaleMath.Grade g = ScaleMath.grade(s.n);
|
||
String gk = ScaleMath.gradeKey(s.n);
|
||
gradeChip.setText(I18n.t(gk));
|
||
gradeChip.setForeground(g == ScaleMath.Grade.LARGE ? Theme.SUCCESS
|
||
: g == ScaleMath.Grade.MEDIUM ? Theme.WARN : Theme.DANGER);
|
||
gradeChip.setBackground(g == ScaleMath.Grade.LARGE ? new Color(0xE8F7F0)
|
||
: g == ScaleMath.Grade.MEDIUM ? new Color(0xFFF6E6) : new Color(0xFDECEC));
|
||
gradeNote.setText("<html><body style='width:200px'>" + I18n.t(gk + "Note") + "</body></html>");
|
||
gradeNote.setForeground(Theme.DIM);
|
||
stepsArea.setText(stepsText());
|
||
ruler.set(s);
|
||
refreshLandmark();
|
||
updating = false;
|
||
}
|
||
|
||
private void highlight(UI.Card c, boolean active) {
|
||
c.setBorder(BorderFactory.createCompoundBorder(
|
||
BorderFactory.createLineBorder(active ? Theme.ACCENT : Theme.LINE, 1, true),
|
||
new EmptyBorder(11, 11, 11, 11)));
|
||
c.alt = active;
|
||
c.repaint();
|
||
}
|
||
|
||
private String stepsText() {
|
||
StringBuilder sb = new StringBuilder();
|
||
sb.append(I18n.t("steps.1.unify")).append('\n');
|
||
String formula = switch (s.mode) {
|
||
case REAL -> I18n.t("steps.2.formulaReal");
|
||
case MAP -> I18n.t("steps.2.formulaMap");
|
||
case SCALE -> I18n.t("steps.2.formulaScale");
|
||
};
|
||
sb.append(formula).append('\n');
|
||
String subst = switch (s.mode) {
|
||
case REAL -> ScaleMath.num(s.mapCm) + " cm × " + ScaleMath.formatN(s.n);
|
||
case MAP -> ScaleMath.formatRealCm(s.realCm).replace("unit.", "") + " ÷ " + ScaleMath.formatN(s.n);
|
||
case SCALE -> ScaleMath.formatRealCm(s.realCm).replace("unit.", "") + " ÷ " + ScaleMath.num(s.mapCm) + " cm";
|
||
};
|
||
sb.append(I18n.f("steps.3.substitute", subst)).append('\n');
|
||
String res = switch (s.mode) {
|
||
case REAL -> ScaleMath.formatRealCm(s.realCm).replace("unit.", "");
|
||
case MAP -> ScaleMath.num(s.mapCm) + " cm";
|
||
case SCALE -> "1 : " + ScaleMath.formatN(s.n);
|
||
};
|
||
sb.append(I18n.f("steps.4.result", res)).append('\n');
|
||
sb.append(I18n.t("steps.5.convert"));
|
||
return sb.toString();
|
||
}
|
||
|
||
private void refreshLandmark() {
|
||
if (landmarkBox.getSelectedIndex() < 0 || data.landmarks.isEmpty()) return;
|
||
ScaleData.Landmark lm = data.landmarks.get(landmarkBox.getSelectedIndex());
|
||
double mapCm = ScaleMath.realToMapCm(lm.meters() * 100, s.n);
|
||
String text;
|
||
if (mapCm > 100) text = I18n.t("field.tooLong");
|
||
else if (mapCm < 0.001) text = I18n.t("field.tooShort");
|
||
else text = I18n.f("field.mapLength", ScaleMath.num(mapCm) + " cm");
|
||
landmarkResult.setText(text);
|
||
}
|
||
|
||
private void newQuiz() {
|
||
quiz = ScaleMath.generateQuiz(rnd);
|
||
quizInput.setText("");
|
||
quizFeedback.setText("");
|
||
quizSteps.removeAll();
|
||
quizUnit.setText(I18n.f("quiz.unit", I18n.t(quiz.answerUnitKey)));
|
||
quizPrompText();
|
||
quizStreak.setText(I18n.f("quiz.streak", streak));
|
||
revalidate(); repaint();
|
||
}
|
||
|
||
private void quizPrompText() {
|
||
quizPrompt.setText("<html><body style='width:420px'>" + I18n.f(quiz.promptKey, quiz.promptArgs) + "</body></html>");
|
||
}
|
||
|
||
private void checkQuiz() {
|
||
try {
|
||
double v = Double.parseDouble(quizInput.getText().trim().replace(",", ""));
|
||
if (ScaleMath.checkAnswer(quiz, v)) {
|
||
streak++;
|
||
quizFeedback.setForeground(Theme.SUCCESS);
|
||
quizFeedback.setText(I18n.t("quiz.correct"));
|
||
quizSteps.removeAll();
|
||
} else {
|
||
streak = 0;
|
||
quizFeedback.setForeground(Theme.DANGER);
|
||
quizFeedback.setText(I18n.t("quiz.wrong"));
|
||
quizSteps.removeAll();
|
||
for (String k : quiz.stepKeys) {
|
||
JLabel l = Theme.label(I18n.t(k), 10.5f, Theme.DIM);
|
||
quizSteps.add(l);
|
||
}
|
||
}
|
||
quizStreak.setText(I18n.f("quiz.streak", streak));
|
||
revalidate(); repaint();
|
||
} catch (Exception e) {
|
||
quizFeedback.setForeground(Theme.DANGER);
|
||
quizFeedback.setText(I18n.t("quiz.wrong"));
|
||
}
|
||
}
|
||
|
||
/** 中栏可视化标尺:图上 / 实地双条、线段式、比例尺光谱。 */
|
||
private static final class ScaleRuler extends JComponent {
|
||
private ScaleMath.State st = new ScaleMath.State();
|
||
|
||
void set(ScaleMath.State s) { st = s.copy(); repaint(); }
|
||
|
||
@Override protected void paintComponent(Graphics g) {
|
||
Graphics2D g2 = (Graphics2D) g.create();
|
||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||
int w = getWidth(), h = getHeight();
|
||
g2.setColor(new Color(0xF7F9FE));
|
||
g2.fillRoundRect(6, 4, w - 12, h - 8, 12, 12);
|
||
int x = 26, y = 34;
|
||
int maxW = w - 52;
|
||
int blueW = (int) Math.max(30, Math.min(maxW, st.mapCm * 70));
|
||
|
||
g2.setFont(Theme.font(11));
|
||
g2.setColor(new Color(0x5B6A86));
|
||
g2.drawString(I18n.t("ruler.mapBar"), x, y - 12);
|
||
g2.setColor(new Color(0x2F6BFF));
|
||
g2.fillRoundRect(x, y, blueW, 16, 7, 7);
|
||
g2.setColor(new Color(0x1F4FD0));
|
||
g2.setFont(Theme.bold(10.5f));
|
||
g2.drawString(ScaleMath.num(st.mapCm) + " cm", x + blueW + 8, y + 13);
|
||
|
||
int y2 = y + 52;
|
||
g2.setFont(Theme.font(11));
|
||
g2.setColor(new Color(0x5B6A86));
|
||
g2.drawString(I18n.t("ruler.realBar"), x, y2 - 12);
|
||
int greenW = blueW;
|
||
g2.setColor(new Color(0x1F9D6B));
|
||
g2.fillRoundRect(x, y2, greenW, 16, 7, 7);
|
||
g2.setColor(new Color(0x137A51));
|
||
g2.setFont(Theme.bold(10.5f));
|
||
g2.drawString(ScaleMath.formatRealCm(st.realCm).replace("unit.", ""), x + greenW + 8, y2 + 13);
|
||
|
||
int y3 = y2 + 54;
|
||
g2.setFont(Theme.bold(11.5f));
|
||
g2.setColor(new Color(0x111A2E));
|
||
g2.drawString(I18n.t("ruler.segment"), x, y3);
|
||
g2.setFont(Theme.font(10));
|
||
g2.setColor(new Color(0x8A97B1));
|
||
g2.drawString(I18n.t("ruler.segmentHint"), x + 110, y3);
|
||
ScaleMath.Segment seg = ScaleMath.pickSegment(st.n, 1, 90);
|
||
int sy = y3 + 14;
|
||
int segW = (int) Math.max(14, Math.min(60, seg.divPx()));
|
||
int count = Math.min(seg.divCount(), Math.max(2, maxW / segW));
|
||
for (int i = 0; i < count; i++) {
|
||
g2.setColor(i % 2 == 0 ? new Color(0x111A2E) : Color.WHITE);
|
||
g2.fillRect(x + i * segW, sy, segW, 12);
|
||
g2.setColor(new Color(0x8A97B1));
|
||
g2.drawRect(x + i * segW, sy, segW, 12);
|
||
}
|
||
g2.setColor(new Color(0x5B6A86));
|
||
g2.setFont(Theme.font(9.5f));
|
||
g2.drawString("0", x - 2, sy + 26);
|
||
g2.drawString(ScaleMath.segmentLabel(seg.metersPerDiv() * count), x + count * segW - 18, sy + 26);
|
||
g2.drawString(ScaleMath.segmentLabel(seg.metersPerDiv()), x + segW - 14, sy + 26);
|
||
|
||
int y4 = sy + 50;
|
||
g2.setFont(Theme.bold(11.5f));
|
||
g2.setColor(new Color(0x111A2E));
|
||
g2.drawString(I18n.t("ruler.spectrum"), x, y4);
|
||
int spY = y4 + 14, spH = 12;
|
||
GradientPaint gp = new GradientPaint(x, 0, new Color(0x9CCBFF), x + maxW, 0, new Color(0xFF9D9D));
|
||
g2.setPaint(gp);
|
||
g2.fillRoundRect(x, spY, maxW, spH, 6, 6);
|
||
int px = x + (int) (ScaleMath.spectrumPosition(st.n) * maxW);
|
||
g2.setColor(new Color(0x111A2E));
|
||
g2.fillPolygon(new int[]{px - 6, px + 6, px}, new int[]{spY - 7, spY - 7, spY}, 3);
|
||
g2.setFont(Theme.font(9));
|
||
g2.setColor(new Color(0x5B6A86));
|
||
g2.drawString(I18n.t("ruler.scopeUp"), x, spY + spH + 14);
|
||
String right = I18n.t("ruler.detailDown");
|
||
g2.drawString(right, x + maxW - g2.getFontMetrics().stringWidth(right), spY + spH + 14);
|
||
g2.dispose();
|
||
}
|
||
}
|
||
}
|