Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

10 changed files with 75 additions and 190 deletions

View File

@ -7,6 +7,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="javaFX15" level="project" />
<orderEntry type="library" name="lib1" level="project" />
</component>
</module>

View File

@ -2,19 +2,10 @@
Implementation of a simple Photo viewer with zoom-functions and a slide-show mode
University Project using Java and JavaFX
University Project using Java 15 and JavaFX 15
Note for using project in Intelij IDEA:
To use JavaFX 15 with Intelij IDEA it is required to add the following settings to the VM-Options (in configuration)
```
--module-path /PATH/TO/JAVAFX/LIB/DIR --add-modules javafx.controls,javafx.fxml
--module-path /PATH/TO/JAVAFX/BIN/DIR --add-modules javafx.controls,javafx.fxml
```
## Requirenments:
[Java 15](https://jdk.java.net/15/)
[JavaFX 15](https://gluonhq.com/products/javafx/)
## Tested on
Arch Linux x64 w/ openjfx-15, openjdk-15
Windows 10 x64 w/ openjfx-15, openjdk-15

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 47 KiB

View File

@ -10,12 +10,10 @@ import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
@ -25,8 +23,6 @@ import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
/**
@ -35,17 +31,12 @@ import java.util.List;
*
* @author Tim Lukas Förster
* @version 1.0
* repository: https://gitlab.com/n0x_io/PhotoViewer
*/
public class PVController extends Application {
public class Controller extends Application {
////////////////////////////
//------ Attributes ------//
////////////////////////////
// Constants //
private static final String ARROWPREV = "icons/left-arrow.png";
private static final String ARROWNEXT = "icons/right-arrow.png";
// CENTER //
private final ScrollPane currentViewSP = new ScrollPane();
private final ImageView centerImageView = new ImageView();
@ -55,18 +46,17 @@ public class PVController extends Application {
private final MenuBar menuBar = new MenuBar();
private final Menu fileMenu = new Menu("File");
private final Menu aboutMenu = new Menu("About");
private final MenuItem menuItemOpenFiles = new MenuItem("Open");
private final MenuItem menuItemClearViewer = new Menu("Close all pictures");
private final MenuItem menuItemStartSlideShow = new MenuItem("Start Slide Show");
private final MenuItem menuItemExitViewer = new Menu("Exit");
private final MenuItem menuItemShowInfo = new Menu("Information");
private final MenuItem openFiles = new MenuItem("Open");
private final MenuItem clearViewer = new Menu("Close all");
private final MenuItem startSlideShow = new MenuItem("Start Slide Show");
private final MenuItem exitViewer = new Menu("Exit");
private final MenuItem showInfo = new Menu("Information");
// BOTTOM //
private final VBox bottomPanel = new VBox();
//private final HBox selectionPane = new HBox();
private final ListView<ImageView> previewPane = new ListView<>();
private final VBox pictureSelector = new VBox();
private final HBox selectionPane = new HBox();
private final ScrollPane pictureSelector = new ScrollPane();
private final BorderPane bottomLowerPanel = new BorderPane();
private final HBox bottomLeft = new HBox();
@ -87,13 +77,11 @@ public class PVController extends Application {
private PictureHandler picHandler;
// Etc. //
private Thread slideShowThread;
private boolean bIsFullScreen = false;
private boolean bSlideShowActive = false;
private final DoubleProperty slideShowSpeed = new SimpleDoubleProperty(4);
private final DoubleProperty zoomProperty = new SimpleDoubleProperty(200);
// Key Combinations & Shortcuts //
private final KeyCombination keyCrtlQ = new KeyCodeCombination(KeyCode.Q, KeyCombination.CONTROL_ANY);
////////////////////////////////
//------ End Attributes ------//
@ -128,7 +116,7 @@ public class PVController extends Application {
// Create all Actions //
createSliderActions();
createMenuActions(primaryStage);
createClickActions(primaryStage);
createButtonActions(primaryStage);
// Keypress Actions //
mainScene.setOnKeyPressed(event -> {
@ -163,11 +151,12 @@ public class PVController extends Application {
primaryStage.show();
}
/**
* Task used to run the slideshow in separate Threat.
*/
@SuppressWarnings("rawtypes")
private final Task slideShowTask = new Task<Void>(){
Task slideShowTask = new Task<Void>(){
@Override
@SuppressWarnings("BusyWait")
protected Void call() throws Exception {
@ -190,26 +179,10 @@ public class PVController extends Application {
/**
* Event wrapper for clear function
* @return EventHandle executing the clearPreviewView function
* @return EventHandle executing the clear function
*/
private EventHandler<ActionEvent> clearPreviewViewEvent(){
return e -> clearViewer();
}
/**
* Event wrapper for showAboutDialog function
* @return EventHandle executing the showAboutDialog function
*/
private EventHandler<ActionEvent> showAboutDialogEvent(){
return e -> showAboutDialog();
}
/**
* Event wrapper for showAboutDialog function
* @return EventHandle executing the showAboutDialog function
*/
private EventHandler<ActionEvent> toggleSlidesHowEvent(){
return e -> toggleSlideShow();
return e -> clearPreviewView();
}
////////////////////////////////
@ -219,7 +192,6 @@ public class PVController extends Application {
* Helper method for handling sliders
*/
private void createSliderActions() {
//TODO: Zoom kinda wonky... should be redone.
// Zoom Action //
zoomProperty.addListener(observable -> {
centerImageView.setFitWidth(zoomProperty.get() * 4);
@ -245,22 +217,18 @@ public class PVController extends Application {
* @param stage the primary stage, used to display the file-open-dialog
*/
private void createMenuActions(Stage stage) {
menuItemOpenFiles.setOnAction(openFileDialogEvent(stage));
menuItemExitViewer.setOnAction(e -> Platform.exit());
menuItemClearViewer.setOnAction(clearPreviewViewEvent());
menuItemStartSlideShow.setOnAction(toggleSlidesHowEvent());
menuItemShowInfo.setOnAction(showAboutDialogEvent());
openFiles.setOnAction(openFileDialogEvent(stage));
exitViewer.setOnAction( e -> Platform.exit());
clearViewer.setOnAction(clearPreviewViewEvent());
}
/**
* Helper method for handling button interaction
* @param stage the primary stage, used to display the file-open-dialog and control fullscreen functions
*/
private void createClickActions(Stage stage) {
// Opens the File dialog
private void createButtonActions(Stage stage) {
openFilesButton.setOnAction(openFileDialogEvent(stage));
// Displays the previous Picture
prevPicBtn.setOnAction(e -> {
try{
centerImageView.setImage(picHandler.getPrevPicture().getImage());
@ -268,7 +236,6 @@ public class PVController extends Application {
catch (NoPicturesLoadedException npl){ showNoPicturesLoadedWarning();}
});
// Displays the next picture
nextPicBtn.setOnAction(e -> {
try{
centerImageView.setImage(picHandler.getNextPicture().getImage());
@ -276,52 +243,36 @@ public class PVController extends Application {
catch (NoPicturesLoadedException npl){ showNoPicturesLoadedWarning();}
});
// Sets the PictureViewer to fullscreen
fullScreenBtn.setOnAction(e -> {
stage.setFullScreen(!bIsFullScreen);
bIsFullScreen ^= true;
});
// Starts the slideshow on first click, stops it on the next one
slideShowBtn.setOnAction(toggleSlidesHowEvent());
// Clicking on any picture in the Previews opens said picture in the main view
previewPane.setOnMouseClicked(e -> {
try {
centerImageView.setImage(picHandler.getPictureByID(previewPane.getSelectionModel().getSelectedIndex()).getImage());
} catch (NoPicturesLoadedException ignored) {
slideShowBtn.setOnAction(new EventHandler<>() {
Thread th;
@Override
public void handle(ActionEvent actionEvent) {
try {
if (!bSlideShowActive) {
// This statement is to catch any errors regarding no images loaded
centerImageView.setImage(picHandler.getNextPicture().getImage());
bSlideShowActive = true;
slideShowBtn.setText("Stop Slide Show");
th = new Thread(slideShowTask);
th.start();
} else {
bSlideShowActive = false;
slideShowBtn.setText("Slide Show");
th.interrupt();
}
} catch (NoPicturesLoadedException npl){
showNoPicturesLoadedWarning();
}
}
});
}
/**
* Method that will start and stop (toggle) the Slide show and set the corresponding texts to all buttons etc.
*/
private void toggleSlideShow(){
try {
if (!bSlideShowActive) {
// This statement is to catch any errors regarding no images loaded
centerImageView.setImage(picHandler.getNextPicture().getImage());
bSlideShowActive = true;
slideShowThread = new Thread(slideShowTask);
slideShowThread.start();
// Change text of slideshow switches
slideShowBtn.setText("Stop Slide Show");
menuItemStartSlideShow.setText("Stop Slide Show");
} else {
bSlideShowActive = false;
// Change text of slideshow switches
slideShowBtn.setText("Slide Show");
menuItemStartSlideShow.setText("Start Slide Show");
assert slideShowThread != null;
slideShowThread.interrupt();
}
} catch (NoPicturesLoadedException npl){
showNoPicturesLoadedWarning();
}
}
/**
* Open file-dialog allowing to select multiple pictures (via filter),
* adds them to the PictureHandler and updates the picture preview
@ -336,7 +287,7 @@ public class PVController extends Application {
if (selectedPictures != null) {
try {
picHandler.loadPictures(selectedPictures);
PVController.this.updatePreviewView();
Controller.this.updatePreviewView();
centerImageView.setImage(picHandler.getNextPicture().getImage());
} catch (NoPicturesLoadedException npl) {
showNoPicturesLoadedWarning();
@ -348,58 +299,26 @@ public class PVController extends Application {
* Method that updates the preview Pane with all PreviewPictures provided by the Picture Handler
*/
private void updatePreviewView(){
clearViewer();
//previewPane.setPadding(new Insets(5,5,5,5));
clearPreviewView();
selectionPane.setPadding(new Insets(5,5,5,5));
for(PicturePreview pp: picHandler.getPreviews()){
ImageView iv = new ImageView(pp.getImage());
iv.setFitWidth(150);
iv.setFitHeight(150);
iv.setSmooth(true);
iv.setPreserveRatio(true);
previewPane.getItems().add(iv);
selectionPane.getChildren().add(iv);
}
}
/**
* Used to clear the preview and center panel
* Used to clear the preview and center panel when the viewer is cleared
*/
private void clearViewer(){
previewPane.getItems().clear();
//previewPane.setPadding(new Insets(5,5,5,5));
private void clearPreviewView(){
selectionPane.getChildren().clear();
selectionPane.setPadding(new Insets(5,155,5,5));
centerImageView.setImage(null);
}
/**
* Show a dialogue displaying copyright information about the program.
*/
private void showAboutDialog(){
Dialog<String> aboutDialog = new Dialog<>();
aboutDialog.setTitle("About this program");
ButtonType btnType = new ButtonType("Ok", ButtonBar.ButtonData.OK_DONE);
aboutDialog.setContentText("""
Created by Tim Lukas Förster
Icons made by www.flaticon.com/authors/roundicons
for www.flaticon.com""");
//Adding buttons to the aboutDialog pane
aboutDialog.getDialogPane().getButtonTypes().add(btnType);
aboutDialog.show();
}
/**
* Displays a warning dialogue informing the user that no pictures has been loaded yet
* and the executed action is not possible.
*/
private void showNoPicturesLoadedWarning(){
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("No pictures loaded");
alert.setContentText("No pictures have been loaded" +
"\nPlease select pictures via the menu or via the open button to view them.");
alert.showAndWait().ifPresent(rs -> {
});
}
/**
* Method that handles the settings and layout of the top Panel
* @return Node, set up for displaying on the top of a GridPane
@ -407,9 +326,9 @@ public class PVController extends Application {
private Node createTop(){
SeparatorMenuItem sep = new SeparatorMenuItem();
// File Menu
fileMenu.getItems().addAll(menuItemOpenFiles, menuItemClearViewer, sep, menuItemStartSlideShow, menuItemExitViewer);
fileMenu.getItems().addAll(openFiles, clearViewer, sep, startSlideShow, exitViewer);
// About Menu
aboutMenu.getItems().addAll(menuItemShowInfo);
aboutMenu.getItems().addAll(showInfo);
// Menu bar
menuBar.getMenus().addAll(fileMenu, aboutMenu);
// Adding Menus to Top Panel
@ -453,31 +372,11 @@ public class PVController extends Application {
zoomSlider.setShowTickMarks(true);
Label zoomLabel = new Label("Zoom:");
bottomLeft.getChildren().addAll(openFilesButton, zoomLabel, zoomSlider);
//bottomLeft.getChildren().addAll(zoomLabel, zoomSlider);
bottomLeft.setSpacing(5);
bottomLowerPanel.setLeft(bottomLeft);
// Middle Bottom Part
try {
// Set pictures as prev/next buttons
ImageView prevPic = new ImageView(new Image(new FileInputStream(ARROWPREV)));
prevPic.setFitHeight(10);
prevPic.setPreserveRatio(true);
prevPicBtn.setText(null);
prevPicBtn.setGraphic(prevPic);
ImageView nextPic = new ImageView(new Image(new FileInputStream(ARROWNEXT)));
nextPic.setFitHeight(10);
nextPic.setPreserveRatio(true);
nextPicBtn.setText(null);
nextPicBtn.setGraphic(nextPic);
} catch (FileNotFoundException ignored){
// fallback to text
prevPicBtn.setGraphic(null);
nextPicBtn.setGraphic(null);
prevPicBtn.setText("<-");
nextPicBtn.setText("->");
}
bottomMid.setAlignment(Pos.CENTER);
bottomMid.setSpacing(5);
bottomMid.getChildren().addAll(prevPicBtn, slideShowBtn, nextPicBtn);
@ -492,16 +391,28 @@ public class PVController extends Application {
bottomLowerPanel.setRight(bottomRight);
// Bottom upper Part -> Picture preview
pictureSelector.setPadding(new Insets(2,2,2,2));
previewPane.setOrientation(Orientation.HORIZONTAL);
previewPane.setMaxHeight(150);
//selectionPane.setSpacing(5);
pictureSelector.getChildren().add(previewPane);
selectionPane.setPadding(new Insets(5,5,155,5));
selectionPane.autosize();
selectionPane.setSpacing(5);
pictureSelector.setContent(selectionPane);
bottomPanel.getChildren().addAll(pictureSelector, bottomLowerPanel);
return (bottomPanel);
}
/**
* Displays a warning dialogue informing the user that no pictures has been loaded yet
* and the executed action is not possible.
*/
private void showNoPicturesLoadedWarning(){
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("No pictures loaded");
alert.setContentText("No pictures have been loaded" +
"\nPlease select pictures via the menu or via the open button to view them.");
alert.showAndWait().ifPresent(rs -> {
});
}
////////////////////////////////////
//------ End Helper-Methods ------//
////////////////////////////////////

View File

@ -5,7 +5,7 @@ package de.thm.tlf.photoViewer;
* but any of the actions that involve pictures are performed.
*/
public class NoPicturesLoadedException extends Exception{
NoPicturesLoadedException(){
super("No pictures have been loaded");
NoPicturesLoadedException(String s){
super(s);
}
}

View File

@ -62,14 +62,13 @@ public final class PictureHandler {
}
/**
* Determines the next picture that should be displayed.
* Wraps around to the beginning of the list if picture after the last one is requested.
* Determines the next picture that should be displayed
* @return Picture object that should be displayed next from the ArrayList pictures
* @throws NoPicturesLoadedException Exception for when no pictures are loaded yet but tried to access them
*/
public Picture getNextPicture() throws NoPicturesLoadedException {
if(pictures.size() <= 0){
throw new NoPicturesLoadedException();
throw new NoPicturesLoadedException("No pictures have been loaded");
}
else {
currentPictureID = (currentPictureID + 1) % pictures.size();
@ -78,35 +77,19 @@ public final class PictureHandler {
}
/**
* Determines the previous picture that should be displayed.
* Wraps around to the end of the list if picture before the first one is requested.
* Determines the previous picture that should be displayed
* @return Picture object that should be displayed next from the ArrayList pictures
* @throws NoPicturesLoadedException Exception for when no pictures are loaded yet but tried to access them
*/
public Picture getPrevPicture() throws NoPicturesLoadedException{
if(pictures.size() <= 0){
throw new NoPicturesLoadedException();
throw new NoPicturesLoadedException("No pictures have been loaded");
}
else {
currentPictureID = (currentPictureID + pictures.size() - 1) % pictures.size();
return pictures.get(currentPictureID);
}
}
/**
* Method to acquire picture by the list-ID of a picture
* @param pictureID the ID of the Picture that is being requested
* @return Picture from the pictures ArrayList on position of the provided ID
* @throws NoPicturesLoadedException Exception for when no pictures are loaded yet but tried to access them
*/
public Picture getPictureByID(int pictureID) throws NoPicturesLoadedException{
if(pictures.size() <= 0){
throw new NoPicturesLoadedException();
}
else {
return pictures.get(pictureID);
}
}
//------ End Methods ------//
}