-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScreen.java
More file actions
298 lines (245 loc) · 10.7 KB
/
MainScreen.java
File metadata and controls
298 lines (245 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;
import java.io.InputStream;
public class MainScreen extends Application {
// Enum to define post content type
enum ContentType { TEXT, IMAGE }
// Post class
static class Post {
String username;
String profilePicPath;
String timestamp;
String title;
ContentType contentType;
String contentText; // used if TEXT
String contentImagePath; // used if IMAGE
Post(String username, String profilePicPath, String timestamp, String title,
ContentType contentType, String contentText, String contentImagePath) {
this.username = username;
this.profilePicPath = profilePicPath;
this.timestamp = timestamp;
this.title = title;
this.contentType = contentType;
this.contentText = contentText;
this.contentImagePath = contentImagePath;
}
}
// Friend class
static class Friend {
String username ;
String profilePicPath ;
boolean isOnline ;
Friend(String username , String profilePicPath , boolean isOnline ) {
this.username = username ;
this.profilePicPath = profilePicPath ;
this.isOnline = isOnline ;
}
}
// Method to create a friend ui element
// Updated method to create a friend UI element
private VBox createFriendBox(Friend friend) {
VBox box = new VBox(5);
box.setPadding(new Insets(5));
box.setStyle("-fx-background-color: #f5f5f5; -fx-background-radius: 5px;");
HBox top = new HBox(10);
top.setAlignment(Pos.CENTER_LEFT);
// Load profile image
Image profileImg;
if (friend.profilePicPath != null && !friend.profilePicPath.isEmpty()) {
InputStream is = getClass().getResourceAsStream(friend.profilePicPath);
if (is != null) {
profileImg = new Image(is);
} else {
System.out.println("Image not found! Using default.");
profileImg = new Image(getClass().getResourceAsStream("default_pfp.png"));
}
} else {
// no path provided → use default
profileImg = new Image(getClass().getResourceAsStream("default_pfp.png"));
}
ImageView profilePic = new ImageView(profileImg);
profilePic.setFitWidth(40);
profilePic.setFitHeight(40);
// Make it circular
Circle clip = new Circle(20, 20, 20);
profilePic.setClip(clip);
Label usernameLabel = new Label(friend.username);
usernameLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 14px;");
// Online/offline dot
Circle statusDot = new Circle(6);
statusDot.setFill(friend.isOnline ? javafx.scene.paint.Color.GREEN : javafx.scene.paint.Color.RED);
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
top.getChildren().addAll(profilePic, usernameLabel, spacer, statusDot);
box.getChildren().add(top);
return box;
}
// Method to create a post UI element
private VBox createPost(Post post) {
VBox postBox = new VBox(5);
postBox.setPadding(new Insets(10));
postBox.setStyle("-fx-background-color: #f0f0f0; -fx-background-radius: 5px;");
// Top box: profile picture, username, timestamp
HBox topBox = new HBox(5);
// Load profile image
Image profileImg;
if (post.profilePicPath != null && !post.profilePicPath.isEmpty()) {
InputStream is = getClass().getResourceAsStream(post.profilePicPath);
if (is != null) {
profileImg = new Image(is);
} else {
System.out.println("Image not found! Using default.");
profileImg = new Image(getClass().getResourceAsStream("default_pfp.png"));
}
} else {
// no path provided → use default
profileImg = new Image(getClass().getResourceAsStream("default_pfp.png"));
}
ImageView profilePic = new ImageView(profileImg);
profilePic.setFitWidth(40);
profilePic.setFitHeight(40);
// Make it circular
Circle clip = new Circle(20, 20, 20);
profilePic.setClip(clip);
Label usernameLabel = new Label(post.username);
usernameLabel.setStyle(
"-fx-font-size: 18px;" +
"-fx-text-fill: Blue;"
);
Label timestampLabel = new Label(post.timestamp);
timestampLabel.setStyle(
"-fx-font-size: 12px;" +
"-fx-text-fill: black;"
);
topBox.getChildren().addAll(profilePic, usernameLabel, timestampLabel);
topBox.setAlignment(Pos.CENTER_LEFT);
// Title
Label titleLabel = new Label(post.title);
titleLabel.setStyle(
"-fx-font-size: 18px;" +
"-fx-text-fill: black;"
);
postBox.getChildren().addAll(topBox, titleLabel);
// Content
if (post.contentType == ContentType.IMAGE) {
try {
Image postImg = new Image(getClass().getResourceAsStream(post.contentImagePath));
ImageView imgView = new ImageView(postImg);
imgView.setFitWidth(500); // Limit width
imgView.setPreserveRatio(true); // Keep aspect ratio
// Wrap in a VBox for styling
VBox imgBox = new VBox(imgView);
imgBox.setAlignment(Pos.CENTER);
imgBox.setPadding(new Insets(10));
imgBox.setStyle(
"-fx-background-color: white;" +
"-fx-background-radius: 10;" +
"-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 5);" // subtle shadow
);
postBox.getChildren().add(imgBox);
} catch (Exception e) {
Label fallback = new Label("[Image not found]");
postBox.getChildren().add(fallback);
}
} else if (post.contentType == ContentType.TEXT) {
Label contentLabel = new Label(post.contentText);
contentLabel.setWrapText(true); // wrap long text
contentLabel.setStyle(
"-fx-font-size: 14px;" +
"-fx-text-fill: black;"
);
// Wrap in a VBox for styling
VBox textBox = new VBox(contentLabel);
textBox.setPadding(new Insets(10));
textBox.setStyle(
"-fx-background-color: white;" +
"-fx-background-radius: 10;" +
"-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.2), 10, 0, 0, 5);"
);
postBox.getChildren().add(textBox);
}
// Action buttons
HBox actionBox = new HBox(10);
Button likeButton = new Button("Like");
Button commentButton = new Button("Comment");
actionBox.getChildren().addAll(likeButton, commentButton);
postBox.getChildren().add(actionBox);
return postBox;
}
@Override
public void start(Stage stage) {
BorderPane root = new BorderPane();
// Top navigation bar
HBox topNav = new HBox();
topNav.setPadding(new Insets(10));
topNav.setStyle("-fx-background-color: #ffffff;");
topNav.setAlignment(Pos.CENTER_LEFT);
TextField searchBar = new TextField();
searchBar.setPromptText("Search ...");
searchBar.setPrefWidth(200);
searchBar.setStyle(
"-fx-background-radius: 5px;" +
"-fx-border-radius: 5px;" +
"-fx-border-color: #ddd;" +
"-fx-padding: 5 10 5 10;"
);
Button notificationsButton = new Button("🔔News");
Button settingsButton = new Button("Settings");
HBox.setMargin(notificationsButton, new Insets(0, 5, 0, 5));
HBox.setMargin(settingsButton, new Insets(0, 5, 0, 5));
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
topNav.getChildren().addAll(searchBar, spacer, notificationsButton, settingsButton);
root.setTop(topNav);
// Feed area
VBox feed = new VBox(10);
feed.setAlignment(Pos.TOP_CENTER); //Center all posts horizontally
feed.setStyle("-fx-background-color: #A8D2FF;"); // light gray background
feed.setFillWidth(true);
ScrollPane scrollPane = new ScrollPane(feed);
scrollPane.setFitToWidth(true);
scrollPane.setStyle("-fx-background: transparent; -fx-background-color: transparent;"); // optional
root.setCenter(scrollPane);
// Sample posts
Post post1 = new Post("Alice", "alice.jpg", "10m ago", "Hello World!", ContentType.TEXT, "This is my first post!", null);
Post post2 = new Post("Bob", "", "30m ago", "Vacation time", ContentType.IMAGE, null, "content.jpg");
Post post3 = new Post("Charlie", "", "1h ago", "JavaFX is cool", ContentType.TEXT, "Just learning social app layout!", null);
// Add posts wrapped in HBox to center them and limit width
for (Post p : new Post[]{post1, post2, post3}) {
VBox postBox = createPost(p);
postBox.setMaxWidth(600);
postBox.setPrefWidth(600);
HBox wrapper = new HBox();
wrapper.setAlignment(Pos.CENTER);
wrapper.getChildren().add(postBox);
feed.getChildren().add(wrapper);
}
// Friends list (right side)
VBox friendsList = new VBox(10);
friendsList.setPadding(new Insets(10));
friendsList.getChildren().addAll(
createFriendBox(new Friend("Alice", "alice.jpg", true)),
createFriendBox(new Friend("Bob", "", false)),
createFriendBox(new Friend("Charlie", "", true))
);
ScrollPane friendsScroll = new ScrollPane(friendsList);
friendsScroll.setFitToWidth(true);
root.setRight(friendsScroll);
Scene scene = new Scene(root, 1200, 600);
scene.getStylesheets().add(getClass().getResource("styles/MainScreenStyle.css").toExternalForm());
stage.setTitle("SocialClone Main Screen");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}