リビジョン ed537b0e
| src/com/torutk/tinymap/TinyMapView.fxml | ||
|---|---|---|
|
<?import javafx.scene.control.Label?>
|
||
|
<?import javafx.scene.layout.AnchorPane?>
|
||
|
|
||
|
<AnchorPane id="AnchorPane" fx:id="rootPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="com.torutk.tinymap.TinyMapViewController">
|
||
|
<AnchorPane id="AnchorPane" fx:id="rootPane" prefHeight="520.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.torutk.tinymap.TinyMapViewController">
|
||
|
<children>
|
||
|
<Button fx:id="button" layoutX="241.0" layoutY="161.0" onAction="#loadShapefile" text="読み込み" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="17.0" />
|
||
|
<Label fx:id="scaleLabel" layoutX="241.0" layoutY="14.0" minHeight="16" minWidth="69" text="1:10000000" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="14.0" />
|
||
| src/com/torutk/tinymap/TinyMapViewController.java | ||
|---|---|---|
|
* @author Toru Takahashi
|
||
|
*/
|
||
|
public class TinyMapViewController implements Initializable {
|
||
|
|
||
|
private static final Affine IDENTITY_TRANSFORM = new Affine(); // 恒等変換(表示消去で使用)
|
||
|
private static final double METER_PER_INCH = 0.0254; // インチからメートルへの換算値
|
||
|
private static final double SCALE_RATE = 1.4; // 1段階の拡大縮小比
|
||
|
|
||
|
|
||
|
@FXML
|
||
|
private Label scaleLabel; // 縮尺表示用ラベル
|
||
|
@FXML
|
||
| ... | ... | |
|
private Canvas mapCanvas; // 地図描画領域
|
||
|
@FXML
|
||
|
private Pane rootPane;
|
||
|
|
||
|
|
||
|
private Affine mapTransform = new Affine(); // 地図の拡大縮小スクロールの座標変換
|
||
|
private DoubleProperty scaleProperty = new SimpleDoubleProperty(1); // 地図の拡大率
|
||
|
private double dotPitchInMeter; // 実行環境でのドットピッチを保持
|
||
|
|
||
|
|
||
|
private Point2D dragStartPoint; // 平行移動の開始点
|
||
|
private Point2D mapTranslateAtDragStart; // 平行移動開始時点の地図の座標変換を保持
|
||
|
private Point2D mapTranslate = new Point2D(0, 0); // 平行移動量
|
||
|
|
||
|
|
||
|
private TinyMapModel mapModel;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* ファイル選択ダイアログを表示し、ユーザーが指定したシェープファイルから地図データを読み込む。
|
||
|
*
|
||
|
* @param event
|
||
|
*
|
||
|
* @param event
|
||
|
*/
|
||
|
@FXML
|
||
|
private void loadShapefile(ActionEvent event) {
|
||
| ... | ... | |
|
chooser.setInitialDirectory(Paths.get(System.getProperty("user.dir")).toFile());
|
||
|
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Shapefile", "*.shp"));
|
||
|
File selected = chooser.showOpenDialog(mapCanvas.getScene().getWindow());
|
||
|
if (selected == null) {
|
||
|
return;
|
||
|
}
|
||
|
mapModel = new TinyMapModel(selected);
|
||
|
try {
|
||
|
mapModel.loadLines();
|
||
| ... | ... | |
|
gc.setTransform(IDENTITY_TRANSFORM);
|
||
|
gc.setFill(Color.MIDNIGHTBLUE);
|
||
|
gc.fillRect(0, 0, mapCanvas.getWidth(), mapCanvas.getHeight());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 地図の描画
|
||
|
*/
|
||
|
private void drawMapCanvas() {
|
||
|
clearMapCanvas();
|
||
|
if (mapModel == null) return;
|
||
|
if (mapModel == null) {
|
||
|
return;
|
||
|
}
|
||
|
GraphicsContext gc = mapCanvas.getGraphicsContext2D();
|
||
|
gc.setTransform(mapTransform);
|
||
|
gc.setStroke(Color.LIGHTGREEN);
|
||
| ... | ... | |
|
gc.strokePolyline(polyline.getXArray(), polyline.getYArray(), polyline.size());
|
||
|
});
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void initialize(URL url, ResourceBundle rb) {
|
||
|
// 実行環境でのドットピッチを計算
|
||
| ... | ... | |
|
mapCanvas.setOnScroll(event -> {
|
||
|
scaleProperty.set(
|
||
|
event.getDeltaY() >= 0 ? scaleProperty.get() * SCALE_RATE
|
||
|
: scaleProperty.get() / SCALE_RATE
|
||
|
: scaleProperty.get() / SCALE_RATE
|
||
|
);
|
||
|
mapTransform.setToTransform(
|
||
|
scaleProperty.get(), 0, mapTranslate.getX(),
|
||
| ... | ... | |
|
);
|
||
|
drawMapCanvas();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 地図縮尺(例: 1 / 10,000)をAffineのscaleに変換する.
|
||
|
*
|
||
| ... | ... | |
|
double mapToScale(double reduce) {
|
||
|
return reduce * dotPitchInMeter;
|
||
|
}
|
||
|
|
||
|
|
||
|
private void showError(String message, Throwable ex) {
|
||
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||
|
alert.setTitle("Tiny Map Viewer Message");
|
||
|
alert.setContentText(String.format("%s%n%s", message, ex.getLocalizedMessage()));
|
||
|
String exMessage = (ex.getCause() != null) ? ex.getCause().getLocalizedMessage()
|
||
|
: ex.getLocalizedMessage();
|
||
|
alert.setContentText(String.format("%s%n%s", message, exMessage));
|
||
|
alert.showAndWait();
|
||
|
}
|
||
|
}
|
||
シェープファイル読み込みエラー時の通知メッセージ改善、画面サイズを大きくした