Java program that creates a photo album application. Which will load a collectio
ID: 672032 • Letter: J
Question
Java program that creates a photo album application. Which will load a collection of images and displays them in a album layout. The program will allow the user to tag images with metadata:
•Title for the photo
. Limited to 100 characters.
•Description for the photo
. Limited to 300 characters.
•Date taken
•Place taken
Functional Specs
1.Your album should be able to display the pictures sorted by Title, Date, and Place.
2.When the user clicks on a photo, data should be displayed.
3.The user should be able to add or remove pics.
*Please note the swing library cannot be used only scene (vBox,hBox, etc...)
*This is what I have so far - I can display and select images but Im not sure how to apply the buttons to remove pic, add pic, and add data (title, date, place) Someone tried to explain, but I really just got more confused. please provide import classes so I can have a better understanding of where everything is coming from.
public class PhotoAlbum extends Application {
Stage stage;
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
ScrollPane root = new ScrollPane();
TilePane tile = new TilePane();
root.setStyle("-fx-background-color: DAE6F3;");
tile.setPadding(new Insets(15, 15, 15, 15));
tile.setHgap(15);
String path = "src/image/";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (final File file : listOfFiles) {
ImageView imageView;
imageView = createImageView(file);
tile.getChildren().addAll(imageView);
}
root.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); // Horizontal
root.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Vertical scroll bar
root.setFitToWidth(true);
root.setContent(tile);
primaryStage.setWidth(Screen.getPrimary().getVisualBounds().getWidth());
primaryStage.setHeight(Screen.getPrimary().getVisualBounds()
.getHeight());
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private ImageView createImageView(final File imageFile) {
// DEFAULT_THUMBNAIL_WIDTH is a constant you need to define
// The last two arguments are: preserveRatio, and use smooth (slower)
// resizing
ImageView imageView = null;
try {
final Image image = new Image(new FileInputStream(imageFile), 150, 0, true,
true);
imageView = new ImageView(image);
imageView.setFitWidth(150);
imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
if(mouseEvent.getClickCount() == 2){
try {
BorderPane borderPane = new BorderPane();
ImageView imageView = new ImageView();
Image image = new Image(new FileInputStream(imageFile));
imageView.setImage(image);
imageView.setStyle("-fx-background-color: BLACK");
imageView.setFitHeight(stage.getHeight() - 10);
imageView.setPreserveRatio(true);
imageView.setSmooth(true);
imageView.setCache(true);
borderPane.setCenter(imageView);
borderPane.setStyle("-fx-background-color: BLACK");
Stage newStage = new Stage();
newStage.setWidth(stage.getWidth());
newStage.setHeight(stage.getHeight());
newStage.setTitle(imageFile.getName());
Scene scene = new Scene(borderPane,Color.BLACK);
newStage.setScene(scene);
newStage.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
});
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
return imageView;
}
public static void main(String[] args) {
launch(args);
}
}
Explanation / Answer
ALBUM.java
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.image.ImageView;
import java.util.*;
public class Album extends Observable{
private static List<ImageData> imageData = new ArrayList<>();
ObservableList<ImageData> observableList = FXCollections.observableList(imageData);
public ObservableList<ImageData> getObsList(){return observableList;}
private BooleanProperty changed = new SimpleBooleanProperty();
public final boolean getChanged(){return changed.get();}
public final void setChanged(boolean value){changed.set(value);}
public BooleanProperty changedProperty(){return changed;}
public Album(){}
public static List<ImageData> getImageData(){ return imageData; }
public void addImage(String image, String title, String desc, String loc) {
imageData.add(0, new ImageData(image, title, desc, loc));
}
public void removeImage(ImageView img){
List<ImageData> toRemove = new ArrayList<>();
for(ImageData a: imageData){
if(a.getImageView() == img){
toRemove.add(a);
//observableList.remove(a);
break;
}
}
imageData.removeAll(toRemove);
if (this.getChanged() == true)
this.setChanged(false);
else
this.setChanged(true);
}
public void sortAlbumTitle(Album album) {
System.out.println("Sort by Title");
Collections.sort(album.getImageData(), new Comparator<ImageData>() {
public int compare(ImageData img1, ImageData img2) {
return img1.getTitle().compareToIgnoreCase(img2.getTitle());
}
});
}
public void sortAlbumDate(Album album) {
System.out.println("Sort by Date");
Collections.sort(album.getImageData(), new Comparator<ImageData>() {
public int compare(ImageData img1, ImageData img2) {
return img1.getDate().compareTo(img2.getDate());
}
});
}
public void sortAlbumLoc(Album album) {
System.out.println("Sort by description");
Collections.sort(album.getImageData(), new Comparator<ImageData>() {
public int compare(ImageData img1, ImageData img2) {
return img1.getLocation().compareToIgnoreCase(img2.getLocation());
}
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.