Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created April 13, 2021 17:10
Show Gist options
  • Save jananpatel2002/12af5827499c7bce741caa68e766de53 to your computer and use it in GitHub Desktop.
Save jananpatel2002/12af5827499c7bce741caa68e766de53 to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 4/12/2020
* Course Number: CSC 112
* Course Name: Intermediate topics in Java Programming
* Problem Number: 10
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem
* Steps on creating a GUI Project:
* Add JavaFX Library to your project
* Add VM Argument to Run Configuration: --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml
*/
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class EmailGUI extends Application {
private static final int APPWIDTH = 700;
private static final int APPHEIGHT = 325;
private static final String TITLE = "The Email Program";
private TextField tfSubject = new TextField();
private TextField tfStatus = new TextField();
private TextField tfMailFrom = new TextField();
private TextArea textArea = new TextArea();
private TextField tfMailTo = new TextField();
private final static String SERVER = "localhost";
private final static int PORT = 7500;
// All the HBoxes/VBoxes and extra components that will be needed through the
// program.
@Override
public void start(Stage primaryStage) throws Exception {
AppGUI scene = new AppGUI();
Scene scene1 = new Scene(scene, APPWIDTH, APPWIDTH);
primaryStage.setTitle(TITLE);
primaryStage.setScene(scene1);
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
launch(args);
}
private class AppGUI extends BorderPane {
public AppGUI() {
HandVBoxTop topBox = new HandVBoxTop();
this.setTop(topBox);
HandVBoxBot botBox = new HandVBoxBot();
this.setBottom(botBox);
HandVBoxMiddle centerBox = new HandVBoxMiddle();
this.setCenter(centerBox);
}
public class HandVBoxTop extends VBox {
public HandVBoxTop() {
HBox mailTo = new HBox();
HBox mailFrom = new HBox();
HBox mailSubject = new HBox();
Labeled lbMailTo = new Label("Mail To: ");
Labeled lbMailFrom = new Label("Mail From: ");
Labeled lbSubject = new Label("Subject: ");
lbMailTo.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
tfMailTo.setPrefColumnCount(40);
tfMailTo.setAlignment(Pos.CENTER);
tfMailTo.setPrefHeight(50);
mailTo.setSpacing(33);
mailTo.getChildren().addAll(lbMailTo, tfMailTo);
lbMailFrom.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
tfMailFrom.setPrefColumnCount(40);
tfMailFrom.setAlignment(Pos.CENTER);
tfMailFrom.setPrefHeight(50);
mailFrom.getChildren().addAll(lbMailFrom, tfMailFrom);
lbSubject.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
tfSubject.setPrefColumnCount(40);
tfSubject.setAlignment(Pos.CENTER);
tfSubject.setPrefHeight(50);
mailSubject.setSpacing(29);
mailSubject.getChildren().addAll(lbSubject, tfSubject);
this.setAlignment(Pos.BASELINE_LEFT);
this.setPadding(new Insets(20));
this.setSpacing(10);
this.getChildren().addAll(mailTo, mailFrom, mailSubject);
}
}
private class HandVBoxBot extends VBox {
public HandVBoxBot() {
Label lbStatus = new Label("Status: ");
HBox status = new HBox();
Button sendMail = new Button("Send Mail");
sendMail.setPrefWidth(100);
sendMail.setPrefHeight(50);
sendMail.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt;");
lbStatus.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
tfStatus.setPrefColumnCount(40);
tfStatus.setAlignment(Pos.BASELINE_CENTER);
tfStatus.setPrefHeight(50);
tfStatus.setEditable(false);
status.setSpacing(50);
status.getChildren().addAll(lbStatus, tfStatus);
sendMail.setOnAction(e -> {
try {
calculate();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
this.setAlignment(Pos.CENTER);
this.getChildren().addAll(sendMail, status);
this.setPadding(new Insets(20));
this.setSpacing(20);
}
}
}
private class HandVBoxMiddle extends VBox {
public HandVBoxMiddle() {
Labeled lbInstruction = new Label("Enter your message below: ");
lbInstruction.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
lbInstruction.setAlignment(Pos.CENTER);
textArea.setPrefColumnCount(5);
textArea.setWrapText(true);
textArea.setStyle("-fx-font-size: 12pt;");
textArea.setPadding(new Insets(15));
textArea.setPrefRowCount(10);
this.setAlignment(Pos.CENTER);
this.getChildren().addAll(lbInstruction, textArea);
}
}
private void calculate() throws InterruptedException {
Socket client;
PrintWriter output;
Scanner input;
try {
if (tfMailFrom.getText() != ("") && tfMailTo.getText() != ("") && tfSubject.getText() != ("")
&& textArea.getText() != ("")) {
client = new Socket(SERVER, PORT);
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream());
pStr("SUCCESS!!!");
String ansStr = input.nextLine();
System.out.println(ansStr);
output.flush();
output.println("HELO cs.stcc.edu");
output.flush();
ansStr = input.nextLine();
System.out.println(ansStr);
output.println("MAIL FROM: " + tfMailFrom.getText());
output.flush();
ansStr = input.nextLine();
System.out.println(ansStr);
output.println("RCPT TO: " + tfMailFrom.getText());
output.flush();
ansStr = input.nextLine();
System.out.println(ansStr);
output.println("DATA");
output.flush();
ansStr = input.nextLine();
System.out.println(ansStr);
output.println("Subject: " + tfSubject.getText());
output.flush();
output.println("To: " + tfMailTo.getText());
output.flush();
output.println("From: " + tfMailFrom.getText());
output.flush();
output.println();
output.flush();
output.println(textArea.getText());
output.flush();
output.println(".");
output.flush();
ansStr = input.nextLine();
System.out.println(ansStr);
output.println("QUIT");
output.flush();
ansStr = input.nextLine();
System.out.println(ansStr);
tfStatus.setText("Message Successfully Sent");
input.close();
output.close();
} else {
tfStatus.setText("Please include all inputs.");
}
} catch (Exception ex) {
tfStatus.setText("ERROR ");
}
}
private static void pStr(String p) {
System.out.println(p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment