-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/restaurant page #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| package frontend.model; | ||
|
|
||
| import backend.entities.Restaurant; | ||
| import frontend.view.RestaurantCard; | ||
| import javafx.geometry.Rectangle2D; | ||
| import javafx.scene.image.Image; | ||
| import javafx.scene.image.ImageView; | ||
| import javafx.scene.shape.Rectangle; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class ImageHandler { | ||
| public static void scaleAndCropImage(ImageView imageView, double targetHeight, double targetWidth){ | ||
| public static void scaleAndCropImage(ImageView imageView, double targetWidth, double targetHeight){ | ||
| Image image = imageView.getImage(); | ||
|
|
||
| double width = image.getWidth(); | ||
| double height = image.getHeight(); | ||
|
|
||
| //Calculate ratio | ||
| double ratio = Math.min(width / targetWidth, height / targetHeight); | ||
| double viewWidth = targetWidth * ratio; | ||
| double viewHeight = targetHeight * ratio; | ||
|
|
@@ -23,5 +29,19 @@ public static void scaleAndCropImage(ImageView imageView, double targetHeight, d | |
| imageView.setFitWidth(targetWidth); | ||
| imageView.setFitHeight(targetHeight); | ||
| imageView.setPreserveRatio(false); | ||
| imageView.setSmooth(true); | ||
| } | ||
|
|
||
| public static Image getRestaurantImage(Restaurant restaurant) { | ||
| String imagePath = "/images/" + restaurant.getImagePath(); | ||
| try { | ||
| return new Image(Objects.requireNonNull(ImageHandler.class.getResourceAsStream(imagePath))); | ||
| } catch (Exception e) { | ||
| System.out.println("Could not find: " + imagePath); | ||
| e.printStackTrace(); | ||
| return null; | ||
| } | ||
| } | ||
|
Comment on lines
+35
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning null will cause NPE in downstream callers. When image loading fails, this method returns Consider either:
Option 1: Return a placeholder image (preferred) public static Image getRestaurantImage(Restaurant restaurant) {
String imagePath = "/images/" + restaurant.getImagePath();
try {
return new Image(Objects.requireNonNull(ImageHandler.class.getResourceAsStream(imagePath)));
} catch (Exception e) {
System.out.println("Could not find: " + imagePath);
e.printStackTrace();
- return null;
+ // Return a placeholder image or handle gracefully
+ return new Image(Objects.requireNonNull(ImageHandler.class.getResourceAsStream("/images/placeholder.png")));
}
}Option 2: Add null guard in RestaurantCardIn Image image = ImageHandler.getRestaurantImage(restaurant);
if (image != null) {
view.setImage(image);
ImageHandler.scaleAndCropImage(view, 100, 150);
}🤖 Prompt for AI Agents |
||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,35 +22,22 @@ public RestaurantCard(Restaurant restaurant, Pane pane){ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SceneHandler.switchScene(pane, "restaurant-view.fxml"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setUpImage(restaurant); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ImageView view = new ImageView(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| view.setImage(ImageHandler.getRestaurantImage(restaurant)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ImageHandler.scaleAndCropImage(view, 150, 100); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Rounds upper two corners of image, so it matches corners of VBox (container) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Rectangle clip = new Rectangle(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.widthProperty().bind(view.fitWidthProperty()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.heightProperty().bind(view.fitHeightProperty().add(50)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.setArcWidth(30); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.setArcHeight(30); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| view.setClip(clip); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.getChildren().add(view); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential NullPointerException if image loading fails.
🛠️ Proposed fix ImageView view = new ImageView();
- view.setImage(ImageHandler.getRestaurantImage(restaurant));
- ImageHandler.scaleAndCropImage(view, 100, 150);
+ Image image = ImageHandler.getRestaurantImage(restaurant);
+ if (image != null) {
+ view.setImage(image);
+ ImageHandler.scaleAndCropImage(view, 100, 150);
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setUpNameAndRating(restaurant); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void setUpImage(Restaurant restaurant){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ImageView imageView = new ImageView(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String imagePath = "/images/" + restaurant.getImagePath(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Image image = new Image(Objects.requireNonNull(RestaurantCard.class.getResourceAsStream(imagePath))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| imageView.setImage(image); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ImageHandler.scaleAndCropImage(imageView, 100, 150); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Rounds upper two corners of image, so it matches corners of VBox (container) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Rectangle clip = new Rectangle(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.widthProperty().bind(imageView.fitWidthProperty()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.heightProperty().bind(imageView.fitHeightProperty().add(50)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.setArcWidth(30); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| clip.setArcHeight(30); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| imageView.setClip(clip); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.getChildren().add(imageView); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| imageView.setImage(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Could not find: " + imagePath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void setUpNameAndRating(Restaurant restaurant){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HBox container = new HBox(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <?import javafx.scene.control.*?> | ||
| <?import javafx.scene.layout.*?> | ||
|
|
||
| <?import javafx.geometry.Insets?> | ||
| <VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="frontend.controller.BookingController"> | ||
| <padding> | ||
| <Insets top="15" bottom="10" left="20" right="40"/> | ||
| </padding> | ||
|
|
||
| <Label fx:id="statusLabel"/> | ||
| <Label text="Book a table:" styleClass="bookingForm-Header"/> | ||
|
|
||
| <VBox styleClass="bookingForm-Textfield"> | ||
| <TextField fx:id="firstNameField" promptText="First name"/> | ||
| <TextField fx:id="lastNameField" promptText="Last name"/> | ||
| <TextField fx:id="phoneField" promptText="Phone number"/> | ||
| <TextField fx:id="emailField" promptText="Email"/> | ||
| </VBox> | ||
|
|
||
|
|
||
| <HBox> | ||
| <VBox styleClass="bookingForm-Labels"> | ||
| <Label text="Guests"/> | ||
| <Label text="Date"/> | ||
| <Label text="Start time"/> | ||
| </VBox> | ||
|
|
||
| <VBox styleClass="bookingForm-Choice"> | ||
| <Spinner fx:id="guestsSpinner" prefWidth="50" styleClass="bookingForm-ChoiceBox"/> | ||
| <DatePicker fx:id="datePicker" styleClass="bookingForm-ChoiceBox"/> | ||
| <ComboBox fx:id="startTimeBox" prefWidth="100" styleClass="bookingForm-ChoiceBox"/> | ||
| </VBox> | ||
| </HBox> | ||
|
|
||
| <VBox alignment="CENTER"> | ||
| <Button text="Book" onAction="#BookTable" prefWidth="100" styleClass="bookingForm-Button"/> | ||
| </VBox> | ||
|
|
||
| </VBox> |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential NullPointerException if image loading fails.
Same issue as in
RestaurantCard:ImageHandler.getRestaurantImage()can returnnull, which will cause an NPE inscaleAndCropImage().🛠️ Proposed fix
Note: You'll need to add
import javafx.scene.image.Image;if not already imported via the wildcard.🤖 Prompt for AI Agents