From 39c4244668b42dda2c252bcdd9a967987b504be7 Mon Sep 17 00:00:00 2001 From: Georg-Notebook Date: Thu, 15 Apr 2021 22:30:26 +0200 Subject: [PATCH] =?UTF-8?q?Get=20Static=20Map=20->=20Img=20speichern,=20?= =?UTF-8?q?=C3=B6ffnen,=20anzeigen,=20get=20von=20mapquest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf.ini.sample | 8 ++++- src/tourplaner/business/DirectionMap.java | 33 +++++++++++++++++++ src/tourplaner/business/FileHelper.java | 36 +++++++++++++++++++++ src/tourplaner/business/HttpHelper.java | 14 ++++++++ src/tourplaner/business/ImgHelper.java | 32 ++++++++++++++++++ src/tourplaner/business/LogHelper.java | 8 +++++ src/tourplaner/business/Reporter.java | 20 +----------- src/tourplaner/business/TourPlaner.java | 18 +++++++++-- src/tourplaner/tourplaner.fxml | 10 +++--- src/tourplaner/ui/TourplanerController.java | 4 +++ src/tourplaner/viewmodels/ViewModel.java | 12 ++++++- 11 files changed, 168 insertions(+), 27 deletions(-) create mode 100644 src/tourplaner/business/DirectionMap.java create mode 100644 src/tourplaner/business/FileHelper.java create mode 100644 src/tourplaner/business/HttpHelper.java create mode 100644 src/tourplaner/business/ImgHelper.java diff --git a/conf.ini.sample b/conf.ini.sample index ed69d71..a6827b3 100644 --- a/conf.ini.sample +++ b/conf.ini.sample @@ -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 \ No newline at end of file +javadoc = https://git.dergeorg.at/dergeorg/tourplaner + +[map] +key = +size = 1500,400 +path = D:\\TourplanerImages\\ +file_pre = file:/// \ No newline at end of file diff --git a/src/tourplaner/business/DirectionMap.java b/src/tourplaner/business/DirectionMap.java new file mode 100644 index 0000000..d7cfa4c --- /dev/null +++ b/src/tourplaner/business/DirectionMap.java @@ -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); + } + + + + +} diff --git a/src/tourplaner/business/FileHelper.java b/src/tourplaner/business/FileHelper.java new file mode 100644 index 0000000..164a37a --- /dev/null +++ b/src/tourplaner/business/FileHelper.java @@ -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); + } +} diff --git a/src/tourplaner/business/HttpHelper.java b/src/tourplaner/business/HttpHelper.java new file mode 100644 index 0000000..d7617e3 --- /dev/null +++ b/src/tourplaner/business/HttpHelper.java @@ -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); + } +} diff --git a/src/tourplaner/business/ImgHelper.java b/src/tourplaner/business/ImgHelper.java new file mode 100644 index 0000000..1913d37 --- /dev/null +++ b/src/tourplaner/business/ImgHelper.java @@ -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; + } +} diff --git a/src/tourplaner/business/LogHelper.java b/src/tourplaner/business/LogHelper.java index e0d9ee3..f7e3f4a 100644 --- a/src/tourplaner/business/LogHelper.java +++ b/src/tourplaner/business/LogHelper.java @@ -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 diff --git a/src/tourplaner/business/Reporter.java b/src/tourplaner/business/Reporter.java index bf5728b..ea6ceab 100644 --- a/src/tourplaner/business/Reporter.java +++ b/src/tourplaner/business/Reporter.java @@ -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(); - } - } - } diff --git a/src/tourplaner/business/TourPlaner.java b/src/tourplaner/business/TourPlaner.java index d0cbb10..324e38a 100644 --- a/src/tourplaner/business/TourPlaner.java +++ b/src/tourplaner/business/TourPlaner.java @@ -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 getLogs(String tourname){ return new DbConnect().getLogs(tourname); diff --git a/src/tourplaner/tourplaner.fxml b/src/tourplaner/tourplaner.fxml index 48ba22c..fd55dc6 100644 --- a/src/tourplaner/tourplaner.fxml +++ b/src/tourplaner/tourplaner.fxml @@ -34,6 +34,7 @@ + @@ -112,10 +113,11 @@ - - - - + + + + + diff --git a/src/tourplaner/ui/TourplanerController.java b/src/tourplaner/ui/TourplanerController.java index 16ce388..b84b2e5 100644 --- a/src/tourplaner/ui/TourplanerController.java +++ b/src/tourplaner/ui/TourplanerController.java @@ -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 beschreibungTableView; public TableColumn startCol, zielCol, dauerCol, streckeCol, nameCol; public TextField titleTextView, sucheInput; + public ImageView mapImageView; //Log -> rechts unten public TableView logTableView; public TableColumn logDauerCol, logStreckeCol, logDatumCol, logAvgCol, logHightCol, logPauseCol, logGegangenCol, logBemerkungCol; @@ -129,6 +131,7 @@ public class TourplanerController implements Initializable { logGegangenCol.setCellValueFactory(new PropertyValueFactory("gegangen")); logBemerkungCol.setCellValueFactory(new PropertyValueFactory("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())); } /** diff --git a/src/tourplaner/viewmodels/ViewModel.java b/src/tourplaner/viewmodels/ViewModel.java index c7dc7a4..d835f78 100644 --- a/src/tourplaner/viewmodels/ViewModel.java +++ b/src/tourplaner/viewmodels/ViewModel.java @@ -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;