Get Static Map -> Img speichern, öffnen, anzeigen, get von mapquest

This commit is contained in:
Georg Reisinger 2021-04-15 22:30:26 +02:00
parent 34aa390f2c
commit 39c4244668
11 changed files with 168 additions and 27 deletions

View File

@ -19,4 +19,10 @@ path = ./reports/
[hilfe]
gitrepo = https://git.dergeorg.at/dergeorg/tourplaner
doxygendoc = https://git.dergeorg.at/dergeorg/tourplaner
javadoc = https://git.dergeorg.at/dergeorg/tourplaner
javadoc = https://git.dergeorg.at/dergeorg/tourplaner
[map]
key =
size = 1500,400
path = D:\\TourplanerImages\\
file_pre = file:///

View File

@ -0,0 +1,33 @@
package tourplaner.business;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
public class DirectionMap {
private double dauer, strecke;
private String start, end, tourname, file;
private Image map;
public DirectionMap (String start, String ende, String tourname) throws IOException {
this.map = getMap(start, ende);
this.start = start;
this.end = ende;
this.tourname = tourname;
this.file = ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "path") + this.tourname + ".jpg";
FileHelper.saveImage(this.map, "jpg", new File(file));
FileHelper.openDefault(file);
}
private Image getMap(String start, String ende) throws IOException {
return HttpHelper.httpGetStaticMap(start, ende);
}
}

View File

@ -0,0 +1,36 @@
package tourplaner.business;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class FileHelper {
public static void openDefault(String f){
// A reference to a text file
File file = new File(f);
try {
Desktop desktop = Desktop.getDesktop();
// Open a file using the default program for the file type. In the example
// we will launch a default registered program to open a text file. For
// example on Windows operating system this call might launch a notepad.exe
// to open the file.
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveImage(Image img, String type, File file) throws IOException {
ImageIO.write(ImgHelper.toBufferedImage(img), type, file);
}
public static BufferedImage getImage(File file) throws IOException {
return ImageIO.read(file);
}
}

View File

@ -0,0 +1,14 @@
package tourplaner.business;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
public class HttpHelper {
public static Image httpGetStaticMap(String start, String end) throws IOException {
URL url = new URL("https://www.mapquestapi.com/staticmap/v5/map?start="+start+"&end="+end+"&size="+ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "size")+"&key="+ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "key"));
return ImageIO.read(url);
}
}

View File

@ -0,0 +1,32 @@
package tourplaner.business;
import java.awt.*;
import java.awt.image.BufferedImage;
public class ImgHelper {
/**
* Converts a given Image into a BufferedImage
*
* @param img The Image to be converted
* @return The converted BufferedImage
*/
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
}

View File

@ -33,6 +33,14 @@ public class LogHelper{
getLog(name).error(msg);
}
/**
* Log info in file und Console
* @param e Exception
*/
public static void error(Exception e){
getLog(e.getClass().getName()).error(e.getMessage());
}
/**
* Log info in file und Console
* @param msg Nachricht in dem Log

View File

@ -54,7 +54,7 @@ public class Reporter {
} catch (DocumentException | FileNotFoundException e) {
LogHelper.error(e.getMessage(), e.getClass().getName());
}
openDefault(file);
FileHelper.openDefault(file);
}
// iText allows to add metadata to the PDF which can be viewed in your Adobe
@ -216,22 +216,4 @@ public class Reporter {
paragraph.add(new Paragraph(" "));
}
}
private static void openDefault(String f){
// A reference to a text file
File file = new File(f);
try {
Desktop desktop = Desktop.getDesktop();
// Open a file using the default program for the file type. In the example
// we will launch a default registered program to open a text file. For
// example on Windows operating system this call might launch a notepad.exe
// to open the file.
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -5,6 +5,8 @@ import tourplaner.data.DbConnect;
import tourplaner.object.Log;
import tourplaner.object.Tour;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@ -42,16 +44,28 @@ public class TourPlaner{
public static boolean delTour(String tourname){
return new DbConnect().delTour(tourname);
}
/**
* Fügt eine Tour hinzu
* @param newTour Neue Tour
* @return false bei error
*/
public static boolean addTour(Tour newTour){
public static boolean addTour(Tour newTour) throws IOException {
DirectionMap directionMap = new DirectionMap(newTour.getStart(), newTour.getZiel(), newTour.getName());
return new DbConnect().addTour(newTour);
}
public static Image getImage(String tourname) throws IOException {
return FileHelper.getImage(new File(getImagePath(tourname)));
}
public static String getImagePath(String tourname){
return ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "path") + tourname + ".jpg";
}
public static void openImage(String tourname){
FileHelper.openDefault(getImagePath(tourname));
}
public static ArrayList<Log> getLogs(String tourname){
return new DbConnect().getLogs(tourname);

View File

@ -34,6 +34,7 @@
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
@ -112,10 +113,11 @@
<children>
<TabPane fx:id="viewTabPane" layoutX="1.0" layoutY="69.0" prefWidth="702.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="-67.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab fx:id="kartenTab" text="Karte">
<content>
<AnchorPane />
</content></Tab>
<Tab fx:id="kartenTab" text="Karte">
<AnchorPane>
<ImageView fx:id="mapImageView" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="80.0" AnchorPane.leftAnchor="13.0" AnchorPane.rightAnchor="825.0" AnchorPane.topAnchor="0.0" />
</AnchorPane>
</Tab>
<Tab fx:id="beschreibungTab" text="Beschreibung">
<content>
<AnchorPane>

View File

@ -8,6 +8,7 @@ import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import tourplaner.business.ConfigHelper;
import tourplaner.business.LogHelper;
@ -37,6 +38,7 @@ public class TourplanerController implements Initializable {
public TableView<Tour> beschreibungTableView;
public TableColumn<Tour, String> startCol, zielCol, dauerCol, streckeCol, nameCol;
public TextField titleTextView, sucheInput;
public ImageView mapImageView;
//Log -> rechts unten
public TableView<Log> logTableView;
public TableColumn<Log, String> logDauerCol, logStreckeCol, logDatumCol, logAvgCol, logHightCol, logPauseCol, logGegangenCol, logBemerkungCol;
@ -129,6 +131,7 @@ public class TourplanerController implements Initializable {
logGegangenCol.setCellValueFactory(new PropertyValueFactory<Log, String>("gegangen"));
logBemerkungCol.setCellValueFactory(new PropertyValueFactory<Log, String>("bemerkung"));
mapImageView.setImage(this.viewModel.getImage(this.viewModel.getSelectedTour().getName()));
}
/**
@ -147,6 +150,7 @@ public class TourplanerController implements Initializable {
@FXML
private void addTour(){
this.viewModel.addTour();
this.mapImageView.setImage(this.viewModel.getImage(this.viewModel.getSelectedTour().getName()));
}
/**

View File

@ -2,6 +2,7 @@ package tourplaner.viewmodels;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import tourplaner.business.TourPlaner;
import tourplaner.ui.AlertHelper;
import tourplaner.business.ConfigHelper;
@ -31,6 +32,10 @@ public class ViewModel {
public Image getImage(String tourname){
return new Image( ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "map", "file_pre") + TourPlaner.getImagePath(tourname));
}
/**
* Bearbeitet eine bereits bestehende Tour
* prüft ob eine tour ausgewählt ist
@ -155,7 +160,12 @@ public class ViewModel {
Tour newTour = new Tour(this.neueTourName, "1", "1", 0, this.neueTourStart, this.neueTourZiel);
tourData.add(newTour);
tourNamen.add(this.neueTourName);
TourPlaner.addTour(newTour);
this.selectedTour = newTour;
try {
TourPlaner.addTour(newTour);
} catch (IOException e) {
e.printStackTrace();
}
}
this.neueTourStart = null;
this.neueTourZiel = null;