();
67 |
68 | public void registration() {
69 | if (!regName.getText().equalsIgnoreCase("")
70 | && !regPass.getText().equalsIgnoreCase("")
71 | && !regEmail.getText().equalsIgnoreCase("")
72 | && !regFirstName.getText().equalsIgnoreCase("")
73 | && !regPhoneNo.getText().equalsIgnoreCase("")
74 | && (male.isSelected() || female.isSelected())) {
75 | if(checkUser(regName.getText())) {
76 | if(checkEmail(regEmail.getText())) {
77 | User newUser = new User();
78 | newUser.name = regName.getText();
79 | newUser.password = regPass.getText();
80 | newUser.email = regEmail.getText();
81 | newUser.fullName = regFirstName.getText();
82 | newUser.phoneNo = regPhoneNo.getText();
83 | if (male.isSelected()) {
84 | newUser.gender = "Male";
85 | } else {
86 | newUser.gender = "Female";
87 | }
88 | users.add(newUser);
89 | goBack.setOpacity(1);
90 | success.setOpacity(1);
91 | makeDefault();
92 | if (controlRegLabel.getOpacity() == 1) {
93 | controlRegLabel.setOpacity(0);
94 | }
95 | if (nameExists.getOpacity() == 1) {
96 | nameExists.setOpacity(0);
97 | }
98 | } else {
99 | checkEmail.setOpacity(1);
100 | setOpacity(nameExists, goBack, controlRegLabel, success);
101 | }
102 | } else {
103 | nameExists.setOpacity(1);
104 | setOpacity(success, goBack, controlRegLabel, checkEmail);
105 | }
106 | } else {
107 | controlRegLabel.setOpacity(1);
108 | setOpacity(success, goBack, nameExists, checkEmail);
109 | }
110 | }
111 |
112 | private void setOpacity(Label a, Label b, Label c, Label d) {
113 | if(a.getOpacity() == 1 || b.getOpacity() == 1 || c.getOpacity() == 1 || d.getOpacity() == 1) {
114 | a.setOpacity(0);
115 | b.setOpacity(0);
116 | c.setOpacity(0);
117 | d.setOpacity(0);
118 | }
119 | }
120 |
121 |
122 | private void setOpacity(Label controlRegLabel, Label checkEmail, Label nameExists) {
123 | controlRegLabel.setOpacity(0);
124 | checkEmail.setOpacity(0);
125 | nameExists.setOpacity(0);
126 | }
127 |
128 | private boolean checkUser(String username) {
129 | for(User user : users) {
130 | if(user.name.equalsIgnoreCase(username)) {
131 | return false;
132 | }
133 | }
134 | return true;
135 | }
136 |
137 | private boolean checkEmail(String email) {
138 | for(User user : users) {
139 | if(user.email.equalsIgnoreCase(email)) {
140 | return false;
141 | }
142 | }
143 | return true;
144 | }
145 |
146 | private void makeDefault() {
147 | regName.setText("");
148 | regPass.setText("");
149 | regEmail.setText("");
150 | regFirstName.setText("");
151 | regPhoneNo.setText("");
152 | male.setSelected(true);
153 | setOpacity(controlRegLabel, checkEmail, nameExists);
154 | }
155 |
156 |
157 | public void login() {
158 | username = userName.getText();
159 | password = passWord.getText();
160 | boolean login = false;
161 | for (User x : users) {
162 | if (x.name.equalsIgnoreCase(username) && x.password.equalsIgnoreCase(password)) {
163 | login = true;
164 | loggedInUser.add(x);
165 | System.out.println(x.name);
166 | gender = x.gender;
167 | break;
168 | }
169 | }
170 | if (login) {
171 | changeWindow();
172 | } else {
173 | loginNotifier.setOpacity(1);
174 | }
175 | }
176 |
177 | public void changeWindow() {
178 | try {
179 | Stage stage = (Stage) userName.getScene().getWindow();
180 | Parent root = FXMLLoader.load(this.getClass().getResource("Room.fxml"));
181 | stage.setScene(new Scene(root, 330, 560));
182 | stage.setTitle(username + "");
183 | stage.setOnCloseRequest(event -> {
184 | System.exit(0);
185 | });
186 | stage.setResizable(false);
187 | stage.show();
188 | } catch (IOException e) {
189 | e.printStackTrace();
190 | }
191 | }
192 |
193 | @FXML
194 | private void handleButtonAction(ActionEvent event) {
195 | if (event.getSource().equals(btnSignUp)) {
196 | new FadeIn(pnSignUp).play();
197 | pnSignUp.toFront();
198 | }
199 | if (event.getSource().equals(getStarted)) {
200 | new FadeIn(pnSignIn).play();
201 | pnSignIn.toFront();
202 | }
203 | loginNotifier.setOpacity(0);
204 | userName.setText("");
205 | passWord.setText("");
206 | }
207 |
208 | @FXML
209 | private void handleMouseEvent(MouseEvent event) {
210 | if (event.getSource() == btnBack) {
211 | new FadeIn(pnSignIn).play();
212 | pnSignIn.toFront();
213 | }
214 | regName.setText("");
215 | regPass.setText("");
216 | regEmail.setText("");
217 | }
218 | }
--------------------------------------------------------------------------------
/Java/Messenger/src/Client/Room.java:
--------------------------------------------------------------------------------
1 | package Client;
2 |
3 | import Client.Controller.*;
4 | import animatefx.animation.FadeIn;
5 | import animatefx.animation.FadeOut;
6 | import javafx.embed.swing.SwingFXUtils;
7 | import javafx.event.ActionEvent;
8 | import javafx.fxml.FXML;
9 | import javafx.fxml.Initializable;
10 | import javafx.geometry.NodeOrientation;
11 | import javafx.scene.Node;
12 | import javafx.scene.control.*;
13 | import javafx.scene.control.Button;
14 | import javafx.scene.control.Label;
15 | import javafx.scene.control.TextArea;
16 | import javafx.scene.control.TextField;
17 | import javafx.scene.effect.DropShadow;
18 | import javafx.scene.image.Image;
19 | import javafx.scene.image.ImageView;
20 | import javafx.scene.input.KeyEvent;
21 | import javafx.scene.input.MouseEvent;
22 | import javafx.scene.layout.Pane;
23 | import javafx.scene.layout.VBox;
24 | import javafx.scene.paint.Color;
25 | import javafx.scene.paint.ImagePattern;
26 | import javafx.scene.shape.Circle;
27 | import javafx.stage.FileChooser;
28 | import javafx.stage.Stage;
29 | import org.apache.commons.lang3.StringUtils;
30 |
31 | import javax.imageio.ImageIO;
32 | import java.awt.*;
33 | import java.awt.image.BufferedImage;
34 | import java.io.*;
35 | import java.net.Socket;
36 | import java.net.URL;
37 | import java.util.ResourceBundle;
38 |
39 | import static Client.Controller.loggedInUser;
40 | import static Client.Controller.users;
41 |
42 | public class Room extends Thread implements Initializable {
43 | @FXML
44 | public Label clientName;
45 | @FXML
46 | public Button chatBtn;
47 | @FXML
48 | public Pane chat;
49 | @FXML
50 | public TextField msgField;
51 | @FXML
52 | public TextArea msgRoom;
53 | @FXML
54 | public Label online;
55 | @FXML
56 | public Label fullName;
57 | @FXML
58 | public Label email;
59 | @FXML
60 | public Label phoneNo;
61 | @FXML
62 | public Label gender;
63 | @FXML
64 | public Pane profile;
65 | @FXML
66 | public Button profileBtn;
67 | @FXML
68 | public TextField fileChoosePath;
69 | @FXML
70 | public ImageView proImage;
71 | @FXML
72 | public Circle showProPic;
73 | private FileChooser fileChooser;
74 | private File filePath;
75 | public boolean toggleChat = false, toggleProfile = false;
76 |
77 | BufferedReader reader;
78 | PrintWriter writer;
79 | Socket socket;
80 |
81 |
82 |
83 | public void connectSocket() {
84 | try {
85 | socket = new Socket("localhost", 8889);
86 | System.out.println("Socket is connected with server!");
87 | reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
88 | writer = new PrintWriter(socket.getOutputStream(), true);
89 | this.start();
90 | } catch (IOException e) {
91 | e.printStackTrace();
92 | }
93 | }
94 |
95 | @Override
96 | public void run() {
97 | try {
98 | while (true) {
99 | String msg = reader.readLine();
100 | String[] tokens = msg.split(" ");
101 | String cmd = tokens[0];
102 | System.out.println(cmd);
103 | StringBuilder fulmsg = new StringBuilder();
104 | for(int i = 1; i < tokens.length; i++) {
105 | fulmsg.append(tokens[i]);
106 | }
107 | System.out.println(fulmsg);
108 | if (cmd.equalsIgnoreCase(Controller.username + ":")) {
109 | continue;
110 | } else if(fulmsg.toString().equalsIgnoreCase("bye")) {
111 | break;
112 | }
113 | msgRoom.appendText(msg + "\n");
114 | }
115 | reader.close();
116 | writer.close();
117 | socket.close();
118 | } catch (Exception e) {
119 | e.printStackTrace();
120 | }
121 | }
122 |
123 |
124 | public void handleProfileBtn(ActionEvent event) {
125 | if (event.getSource().equals(profileBtn) && !toggleProfile) {
126 | new FadeIn(profile).play();
127 | profile.toFront();
128 | chat.toBack();
129 | toggleProfile = true;
130 | toggleChat = false;
131 | profileBtn.setText("Back");
132 | setProfile();
133 | } else if (event.getSource().equals(profileBtn) && toggleProfile) {
134 | new FadeIn(chat).play();
135 | chat.toFront();
136 | toggleProfile = false;
137 | toggleChat = false;
138 | profileBtn.setText("Profile");
139 | }
140 | }
141 |
142 | public void setProfile() {
143 | for (User user : users) {
144 | if (Controller.username.equalsIgnoreCase(user.name)) {
145 | fullName.setText(user.fullName);
146 | fullName.setOpacity(1);
147 | email.setText(user.email);
148 | email.setOpacity(1);
149 | phoneNo.setText(user.phoneNo);
150 | gender.setText(user.gender);
151 | }
152 | }
153 | }
154 |
155 | public void handleSendEvent(MouseEvent event) {
156 | send();
157 | for(User user : users) {
158 | System.out.println(user.name);
159 | }
160 | }
161 |
162 |
163 | public void send() {
164 | String msg = msgField.getText();
165 | writer.println(Controller.username + ": " + msg);
166 | msgRoom.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
167 | msgRoom.appendText("Me: " + msg + "\n");
168 | msgField.setText("");
169 | if(msg.equalsIgnoreCase("BYE") || (msg.equalsIgnoreCase("logout"))) {
170 | System.exit(0);
171 | }
172 | }
173 |
174 | // Changing profile pic
175 |
176 | public boolean saveControl = false;
177 |
178 | public void chooseImageButton(ActionEvent event) {
179 | Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
180 | fileChooser = new FileChooser();
181 | fileChooser.setTitle("Open Image");
182 | this.filePath = fileChooser.showOpenDialog(stage);
183 | fileChoosePath.setText(filePath.getPath());
184 | saveControl = true;
185 | }
186 |
187 | public void sendMessageByKey(KeyEvent event) {
188 | if (event.getCode().toString().equals("ENTER")) {
189 | send();
190 | }
191 | }
192 |
193 | public void saveImage() {
194 | if (saveControl) {
195 | try {
196 | BufferedImage bufferedImage = ImageIO.read(filePath);
197 | Image image = SwingFXUtils.toFXImage(bufferedImage, null);
198 | proImage.setImage(image);
199 | showProPic.setFill(new ImagePattern(image));
200 | saveControl = false;
201 | fileChoosePath.setText("");
202 | } catch (IOException e) {
203 | System.err.println(e.getMessage());
204 | }
205 | }
206 | }
207 |
208 | @Override
209 | public void initialize(URL location, ResourceBundle resources) {
210 | showProPic.setStroke(Color.valueOf("#90a4ae"));
211 | Image image;
212 | if(Controller.gender.equalsIgnoreCase("Male")) {
213 | image = new Image("icons/user.png", false);
214 | } else {
215 | image = new Image("icons/female.png", false);
216 | proImage.setImage(image);
217 | }
218 | showProPic.setFill(new ImagePattern(image));
219 | clientName.setText(Controller.username);
220 | connectSocket();
221 | }
222 | }
223 |
--------------------------------------------------------------------------------
/Java/Notepad/src/FontHelper.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 | import javax.swing.event.ListSelectionEvent;
3 | import javax.swing.event.ListSelectionListener;
4 | import java.awt.*;
5 |
6 | public class FontHelper extends JDialog implements ListSelectionListener {
7 | JPanel pan1, pan2, pan3;
8 | JLabel fontLabel, sizeLabel, typeLabel, previewLabel;
9 | JTextField label, fontText, sizeText, typeText;
10 | JScrollPane fontScroll, typeScroll, sizeScroll;
11 | JList fontList, sizeList, typeList;
12 | JButton ok, cancel;
13 | GridBagLayout gbl; // GridBagLayout use korle gridBagLayout er ekti constrains dorkar hoi
14 | GridBagConstraints gbc;
15 |
16 |
17 | public FontHelper() {
18 | setTitle("Choose Font");
19 | setSize(300, 400);
20 | setResizable(false);
21 | gbl = new GridBagLayout();
22 | setLayout(gbl);
23 |
24 | gbc = new GridBagConstraints();
25 |
26 | /* For fonts */
27 | gbc.gridx = 1;
28 | gbc.gridy = 1;
29 | gbc.gridwidth = 1;
30 | gbc.gridheight = 1;
31 | gbc.anchor = GridBagConstraints.WEST;
32 | fontLabel = new JLabel("Fonts:");
33 | getContentPane().add(fontLabel, gbc);
34 |
35 |
36 | /* For size */
37 | gbc.gridx = 2;
38 | gbc.gridy = 1;
39 | gbc.gridwidth = 1;
40 | gbc.gridheight = 1;
41 | gbc.anchor = GridBagConstraints.WEST;
42 | sizeLabel = new JLabel("Sizes:");
43 | getContentPane().add(sizeLabel, gbc);
44 |
45 | gbc.gridx = 3;
46 | gbc.gridy = 1;
47 | gbc.gridwidth = 1;
48 | gbc.gridheight = 1;
49 | gbc.anchor = GridBagConstraints.WEST;
50 | typeLabel = new JLabel("Types:");
51 | getContentPane().add(typeLabel, gbc);
52 |
53 | gbc.gridx = 1;
54 | gbc.gridy = 2;
55 | gbc.gridwidth = 1;
56 | gbc.gridheight = 1;
57 | gbc.anchor = GridBagConstraints.WEST;
58 | fontText = new JTextField("Arial", 12);
59 | getContentPane().add(fontText, gbc);
60 |
61 | gbc.gridx = 2;
62 | gbc.gridy = 2;
63 | gbc.gridwidth = 1;
64 | gbc.gridheight = 1;
65 | gbc.anchor = GridBagConstraints.WEST;
66 | sizeText = new JTextField("8", 4);
67 | getContentPane().add(sizeText, gbc);
68 |
69 | gbc.gridx = 3;
70 | gbc.gridy = 2;
71 | gbc.gridwidth = 1;
72 | gbc.gridheight = 1;
73 | gbc.anchor = GridBagConstraints.WEST;
74 | typeText = new JTextField("Regular", 6);
75 | getContentPane().add(typeText, gbc);
76 |
77 | gbc.gridx = 1;
78 | gbc.gridy = 3;
79 | gbc.gridwidth = 1;
80 | gbc.gridheight = 1;
81 | gbc.anchor = GridBagConstraints.WEST;
82 |
83 | /* Creating Font List*/
84 | String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
85 | fontList = new JList(fonts);
86 | fontList.setFixedCellWidth(110);
87 | fontList.addListSelectionListener(this);
88 | fontList.setSelectedIndex(0);
89 |
90 | /* Adding scroll bar of the fontList */
91 | fontScroll = new JScrollPane(fontList);
92 |
93 | getContentPane().add(fontScroll, gbc);
94 |
95 |
96 |
97 |
98 | /* For Font Size */
99 | gbc.gridx = 2;
100 | gbc.gridy = 3;
101 | gbc.gridwidth = 1;
102 | gbc.gridheight = 1;
103 | gbc.anchor = GridBagConstraints.WEST;
104 |
105 | /* Creating Font List*/
106 | String[] sizes = {"8", "10", "12", "16", "18", "20", "24", "28", "32", "48", "72"};
107 | sizeList = new JList(sizes);
108 | sizeList.setFixedCellWidth(20);
109 | sizeList.addListSelectionListener(this);
110 | sizeList.setSelectedIndex(0);
111 |
112 | /* Adding scroll bar of the fontList */
113 | sizeScroll = new JScrollPane(sizeList);
114 |
115 | getContentPane().add(sizeScroll, gbc);
116 |
117 |
118 | /* For type list */
119 | gbc.gridx = 3;
120 | gbc.gridy = 3;
121 | gbc.gridwidth = 1;
122 | gbc.gridheight = 1;
123 | gbc.anchor = GridBagConstraints.WEST;
124 |
125 | /* Creating Font List*/
126 | String[] types = {"Regular", "Bold", "Italic", "Bold Italic"};
127 | typeList = new JList(types);
128 | typeList.setFixedCellWidth(60);
129 | typeList.addListSelectionListener(this);
130 | typeList.setSelectedIndex(0);
131 |
132 | /* Adding scroll bar of the fontList */
133 | typeScroll = new JScrollPane(typeList);
134 |
135 | getContentPane().add(typeScroll, gbc);
136 |
137 |
138 | /* Preview Panel */
139 | gbc.gridx = 1;
140 | gbc.gridy = 4;
141 | gbc.gridwidth = 3;
142 | gbc.gridheight = 1;
143 | gbc.anchor = GridBagConstraints.CENTER;
144 | pan1 = new JPanel();
145 | pan1.setLayout(new FlowLayout());
146 | previewLabel = new JLabel("Preview: ");
147 | pan1.add(previewLabel);
148 | getContentPane().add(pan1, gbc);
149 |
150 |
151 | /* Label */
152 | gbc.gridx = 1;
153 | gbc.gridy = 5;
154 | gbc.gridwidth = 3;
155 | gbc.gridheight = 1;
156 | gbc.anchor = GridBagConstraints.CENTER;
157 | pan2 = new JPanel();
158 | pan2.setLayout(new FlowLayout());
159 | label = new JTextField("AaBbCcDdEeFfGgHhIiJj");
160 | label.setEditable(false);
161 | label.setBorder(BorderFactory.createEtchedBorder());
162 | label.setFont(new Font("Arial", Font.PLAIN, 20));
163 | pan2.add(label);
164 | getContentPane().add(pan2, gbc);
165 |
166 |
167 | /* Ok and Cancel */
168 | gbc.gridx = 1;
169 | gbc.gridy = 6;
170 | gbc.gridwidth = 3;
171 | gbc.gridheight = 1;
172 | gbc.anchor = GridBagConstraints.CENTER;
173 | pan3 = new JPanel();
174 | pan3.setLayout(new FlowLayout());
175 | ok = new JButton("Ok");
176 | cancel = new JButton("Cancel");
177 | pan3.add(ok);
178 | pan3.add(cancel);
179 | getContentPane().add(pan3, gbc);
180 |
181 |
182 | }
183 |
184 | @Override
185 | public void valueChanged(ListSelectionEvent e) {
186 | try {
187 | if(e.getSource() == fontList) {
188 | Font f1 = new Font(String.valueOf(fontList.getSelectedValue()), typeList.getSelectedIndex(), Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
189 | fontText.setText(String.valueOf(fontList.getSelectedValue()));
190 | label.setFont(f1);
191 | } else if(e.getSource() == sizeList) {
192 | Font f2 = new Font(String.valueOf(fontList.getSelectedValue()), typeList.getSelectedIndex(), Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
193 | sizeText.setText(String.valueOf(sizeList.getSelectedValue()));
194 | label.setFont(f2);
195 | } else {
196 | Font f3 = new Font(String.valueOf(fontList.getSelectedValue()), typeList.getSelectedIndex(), Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
197 | typeText.setText(String.valueOf(typeList.getSelectedValue()));
198 | label.setFont(f3);
199 | }
200 | } catch (Exception ee) {
201 |
202 | }
203 | }
204 |
205 |
206 | public Font font() {
207 | Font font = new Font(String.valueOf(fontList.getSelectedValue()), typeList.getSelectedIndex(), Integer.parseInt(String.valueOf(sizeList.getSelectedValue())));
208 | return font;
209 | }
210 |
211 |
212 | public JButton getOk() {
213 | return ok;
214 | }
215 |
216 | public JButton getCancel() {
217 | return cancel;
218 | }
219 | }
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
--------------------------------------------------------------------------------
/Java/HangMan/src/sample/Controller.java:
--------------------------------------------------------------------------------
1 | package sample;
2 |
3 |
4 | import com.sun.tools.javac.comp.Enter;
5 | import javafx.event.ActionEvent;
6 | import javafx.event.Event;
7 | import javafx.event.EventHandler;
8 | import javafx.fxml.FXML;
9 | import javafx.fxml.FXMLLoader;
10 | import javafx.fxml.Initializable;
11 | import javafx.scene.Node;
12 | import javafx.scene.Parent;
13 | import javafx.scene.Scene;
14 | import javafx.scene.control.Button;
15 | import javafx.scene.control.Label;
16 | import javafx.scene.control.TextField;
17 | import javafx.scene.image.ImageView;
18 | import javafx.scene.input.KeyCode;
19 | import javafx.scene.input.KeyEvent;
20 | import javafx.stage.Stage;
21 |
22 | import javax.swing.*;
23 | import java.io.IOException;
24 | import java.net.URL;
25 | import java.util.Arrays;
26 | import java.util.ResourceBundle;
27 |
28 |
29 | public class Controller implements Initializable {
30 |
31 | @FXML
32 | private ImageView head;
33 | @FXML
34 | private ImageView leftHand;
35 | @FXML
36 | private ImageView body;
37 | @FXML
38 | private ImageView rightHand;
39 | @FXML
40 | private ImageView leftLeg;
41 | @FXML
42 | private ImageView rightLeg;
43 | @FXML
44 | private Label firstInstruction;
45 | @FXML
46 | private Label guessingLabel;
47 | @FXML
48 | private Label leftChance;
49 | @FXML
50 | private Label guessingLabelTwo;
51 | @FXML
52 | private Label attemptChance;
53 | @FXML
54 | private Label guessingWord;
55 | @FXML
56 | private Label instructions;
57 | @FXML
58 | private Label here;
59 | @FXML
60 | private Label vowel;
61 | @FXML
62 | private Label consonent;
63 | @FXML
64 | private Button attempt;
65 | @FXML
66 | private Button startButton;
67 | @FXML
68 | private TextField userInput;
69 |
70 | int badGuessCounter = 0, congratsInfo = 0, goodGuessCounter = 0, index = 0;;
71 | int badGuessCheckerOne, badGuessCheckerTwo, random, badGuessResult;
72 | String generatedWord;
73 | private char[] guessingArray = new char[30];
74 | char[] repetedItem = new char[30];
75 | char inputLetter;
76 | static String sharedWord;
77 |
78 |
79 | String[] countryName = {"afghanistan", "armenia", "mexico", "bangladesh",
80 | "argentina", "australia", "brazil", "canada", "china",
81 | "bhutan", "india", "morocco", "belgium", "bolivia",
82 | "nepal", "netherlands", "cambodia", "chile", "pakistan",
83 | "panama", "peru", "portugal", "denmark", "russia", "egypt",
84 | "finland", "france", "spain", "sweden", "switzerland", "syria",
85 | "germany", "ghana", "greece", "thailand", "turkey", "indonesia",
86 | "iran", "italy", "uganda", "japan", "zimbabwe", "korea", "libya"};
87 |
88 |
89 | public void playAGame() {
90 | reset();
91 | manD_0();
92 | manD_1();
93 | manD_2();
94 | manD_3();
95 | manD_4();
96 | manD_5();
97 | makeOpacity();
98 | userInput.setDisable(false);
99 | attempt.setDisable(false);
100 | badGuessResult = 6;
101 | badGuessCounter = 0;
102 | leftChance.setText(badGuessResult + "");
103 | guessingLabel.setText(null);
104 | startButton.setText("Reset!");
105 | random = (int) (Math.random() * ((43 - 0) + 1)) + 0;
106 | generatedWord = countryName[random];
107 | sharedWord = generatedWord;
108 | //Arrays.fill(repetedItem, '0');
109 |
110 |
111 | for (int i = 0; i < generatedWord.length(); i++) {
112 | if ((generatedWord.charAt(i) == 'a') || (generatedWord.charAt(i) == 'e') || (generatedWord.charAt(i) == 'i') || (generatedWord.charAt(i) == 'o') || (generatedWord.charAt(i) == 'u')) {
113 | guessingArray[i] = 'X';
114 | } else
115 | guessingArray[i] = '_';
116 | }
117 | guessingLabel.setText(String.valueOf(guessingArray));
118 | }
119 |
120 | public void reset() {
121 | generatedWord = "";
122 | guessingLabel.setText("");
123 | firstInstruction.setText("");
124 | userInput.setText("");
125 | congratsInfo = 0;
126 | Arrays.fill(guessingArray, ' ');
127 | Arrays.fill(repetedItem, ' ');
128 | }
129 |
130 | public void makeOpacity() {
131 | here.setOpacity(1);
132 | vowel.setOpacity(1);
133 | consonent.setOpacity(1);
134 | guessingWord.setOpacity(1);
135 | instructions.setOpacity(1);
136 | attemptChance.setOpacity(1);
137 | guessingLabelTwo.setOpacity(1);
138 | }
139 |
140 | public void action(ActionEvent actionEvent) {
141 | try {
142 | String userInputText = userInput.getText();
143 | inputLetter = userInputText.charAt(0);
144 | badGuessCheckerTwo = 0;
145 | badGuessCheckerOne = 0;
146 | repetedItem[index] = inputLetter;
147 | index++;
148 |
149 | if (isRepeat()) {
150 | for (int i = 0; i < generatedWord.length(); i++) {
151 | if (inputLetter == (generatedWord.charAt(i))) {
152 | int index = i;
153 | goodGuessCounter = 1;
154 | guessingArray[index] = inputLetter;
155 | guessingLabel.setText(String.valueOf(guessingArray));
156 | badGuessCheckerTwo = 1;
157 | congratsInfo++;
158 | } else {
159 | badGuessCheckerOne = 1;
160 | firstInstruction.setText("Bad Guess!!");
161 | }
162 | }
163 |
164 | if (goodGuessCounter == 1) {
165 | firstInstruction.setText("Good Guess!!");
166 | goodGuessCounter = 0;
167 | }
168 |
169 | if (congratsInfo == generatedWord.length())
170 | congrats(actionEvent);
171 |
172 |
173 | if ((badGuessCheckerOne == 1) && (badGuessCheckerTwo == 0)) {
174 | badGuessCounter++;
175 | badGuessResult = 6 - badGuessCounter;
176 | leftChance.setText(badGuessResult + "");
177 | if (badGuessCounter == 1) {
178 | man_0();
179 | } else if (badGuessCounter == 2) {
180 | man_1();
181 | } else if (badGuessCounter == 3) {
182 | man_2();
183 | } else if (badGuessCounter == 4) {
184 | man_3();
185 | } else if (badGuessCounter == 5) {
186 | man_4();
187 | } else if (badGuessCounter == 6) {
188 | man_5(actionEvent);
189 | }
190 | }
191 |
192 | } else {
193 | firstInstruction.setText("You have already selected this letter!!");
194 | }
195 | userInput.setText("");
196 | } catch (RuntimeException e) {
197 | firstInstruction.setText("Please enter a letter!");
198 | System.out.println(e);
199 | } catch (IOException e) {
200 | e.printStackTrace();
201 | }
202 | }
203 |
204 |
205 |
206 | private boolean isRepeat() {
207 | int counter = 0;
208 | for (int i = 0; i < repetedItem.length; i++) {
209 | if (repetedItem[i] == inputLetter) {
210 | counter++;
211 | }
212 | }
213 | if (counter > 1)
214 | return false;
215 | else if (counter == 0 || counter == 1)
216 | return true;
217 | return false;
218 | }
219 |
220 | public void congrats(ActionEvent event) throws IOException {
221 | Parent congratsPage = FXMLLoader.load(getClass().getResource("Congrats.fxml"));
222 | Scene congo = new Scene(congratsPage);
223 | Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
224 | window.setTitle("Congratulations");
225 | window.setScene(congo);
226 | window.show();
227 | }
228 |
229 | public void gameOver(ActionEvent event) throws IOException {
230 | Parent gameOverPage = FXMLLoader.load(getClass().getResource("gameOver.fxml"));
231 | Scene gameOver = new Scene(gameOverPage);
232 | Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
233 | window.setTitle("Game Over");
234 | window.setScene(gameOver);
235 | window.show();
236 | }
237 |
238 | private void man_5(ActionEvent event) throws IOException {
239 | rightLeg.setOpacity(1);
240 | gameOver(event);
241 | }
242 |
243 | private void man_4() {
244 | leftLeg.setOpacity(1);
245 | }
246 |
247 | private void man_3() {
248 | rightHand.setOpacity(1);
249 | }
250 |
251 | private void man_2() {
252 | leftHand.setOpacity(1);
253 | }
254 |
255 | private void man_1() {
256 | body.setOpacity(1);
257 | }
258 |
259 | private void man_0() {
260 | head.setOpacity(1);
261 | }
262 |
263 |
264 | private void manD_5() {
265 | rightLeg.setOpacity(0);
266 | }
267 |
268 | private void manD_4() {
269 | leftLeg.setOpacity(0);
270 | }
271 |
272 | private void manD_3() {
273 | rightHand.setOpacity(0);
274 | }
275 |
276 | private void manD_2() {
277 | leftHand.setOpacity(0);
278 | }
279 |
280 | private void manD_1() {
281 | body.setOpacity(0);
282 | }
283 |
284 | private void manD_0() {
285 | head.setOpacity(0);
286 | }
287 |
288 | @Override
289 | public void initialize(URL location, ResourceBundle resources) {
290 |
291 | }
292 | }
293 |
--------------------------------------------------------------------------------
/Java/Messenger/src/Client/sample.fxml:
--------------------------------------------------------------------------------
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 |
47 |
52 |
57 |
62 |
67 |
72 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
100 |
105 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
134 |
139 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
163 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
--------------------------------------------------------------------------------
/Java/Messenger/out/production/Project/Client/sample.fxml:
--------------------------------------------------------------------------------
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 |
47 |
52 |
57 |
62 |
67 |
72 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
100 |
105 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
134 |
139 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
163 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
--------------------------------------------------------------------------------
/Java/Notepad/src/FindAndReplace.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 | import java.awt.*;
3 | import java.awt.event.*;
4 |
5 | public class FindAndReplace extends JDialog implements ActionListener {
6 | boolean foundOne, isReplace;
7 | JTextField searchText, replaceText;
8 | JCheckBox cbCase, cbWhole;
9 | JRadioButton up, down;
10 | JLabel statusInfo;
11 | JFrame owner;
12 | JPanel north, center, south;
13 |
14 |
15 | public FindAndReplace(JFrame owner, boolean isReplace) {
16 | super(owner, true);
17 | this.isReplace = isReplace;
18 |
19 | north = new JPanel();
20 | center = new JPanel();
21 | south = new JPanel();
22 |
23 | if (isReplace) {
24 | setTitle(" Find and Replace");
25 | setReplacePanel(north);
26 | } else {
27 | setTitle("Find");
28 | setFindPanel(north);
29 | }
30 | addComponent(center);
31 | statusInfo = new JLabel("Status Info: ");
32 | south.add(statusInfo);
33 |
34 | addWindowListener(new WindowAdapter() {
35 | @Override
36 | public void windowClosing(WindowEvent e) {
37 | dispose();
38 | }
39 | });
40 |
41 | getContentPane().add(north, BorderLayout.NORTH);
42 | getContentPane().add(center, BorderLayout.CENTER);
43 | getContentPane().add(south, BorderLayout.SOUTH);
44 | pack(); // ekti window er vitor component gulo jototuku jaiga niye ache tototuku jaiga niyei thakbe
45 | setDefaultCloseOperation(DISPOSE_ON_CLOSE);
46 | setResizable(false);
47 | int x = (owner.getWidth() * 3 / 5) - (getWidth() / 2);
48 | int y = (owner.getHeight() * 3 / 5) - (getHeight() / 2);
49 | setLocation(x, y);
50 | setVisible(true);
51 | }
52 |
53 | private void addComponent(JPanel center) {
54 | JPanel east = new JPanel();
55 | JPanel west = new JPanel();
56 |
57 | center.setLayout(new GridLayout(1, 2));
58 | east.setLayout(new GridLayout(2, 1));
59 | west.setLayout(new GridLayout(2, 1));
60 |
61 | cbCase = new JCheckBox("Match Case", true);
62 | cbWhole = new JCheckBox("Match Word", true);
63 |
64 | ButtonGroup group = new ButtonGroup();
65 | up = new JRadioButton("Search Up", false);
66 | down = new JRadioButton("Search Down", true);
67 |
68 | group.add(up);
69 | group.add(down);
70 | east.add(cbCase);
71 | east.add(cbWhole);
72 | east.setBorder(BorderFactory.createTitledBorder("Search Options: "));
73 | west.add(up);
74 | west.add(down);
75 | west.setBorder(BorderFactory.createTitledBorder("Search Direction: "));
76 | center.add(east);
77 | center.add(west);
78 | }
79 |
80 | private void setFindPanel(JPanel north) {
81 | final JButton NEXT = new JButton("Find Next");
82 | NEXT.addActionListener(this);
83 | NEXT.setEnabled(false);
84 | searchText = new JTextField(20);
85 | searchText.addActionListener(this);
86 | searchText.addKeyListener(new KeyAdapter() {
87 | public void keyReleased(KeyEvent e) {
88 | boolean state = (searchText.getDocument().getLength() > 0);
89 | NEXT.setEnabled(state);
90 | foundOne = false;
91 | }
92 | });
93 |
94 | if (searchText.getText().length() > 0) {
95 | NEXT.setEnabled(true);
96 | }
97 |
98 | north.add(new JLabel("Find Word:"));
99 | north.add(searchText);
100 | north.add(NEXT);
101 |
102 | }
103 |
104 | /* Replace Panel */
105 | private void setReplacePanel(JPanel north) {
106 | GridBagLayout grid = new GridBagLayout();
107 | north.setLayout(grid);
108 | GridBagConstraints con = new GridBagConstraints();
109 | con.fill = GridBagConstraints.HORIZONTAL;
110 | JLabel lblFword = new JLabel("Find Word:");
111 | JLabel lblRword = new JLabel("Replace Word:");
112 | final JButton NEXT = new JButton("Replace Text");
113 | NEXT.addActionListener(this);
114 | NEXT.setEnabled(false);
115 |
116 | final JButton REPLACE = new JButton("Replace All");
117 | REPLACE.addActionListener(this);
118 | REPLACE.setEnabled(false);
119 | searchText = new JTextField(20);
120 | replaceText = new JTextField(20);
121 |
122 | replaceText.addKeyListener(new KeyAdapter() {
123 | @Override
124 | public void keyReleased(KeyEvent e) {
125 | boolean state = (replaceText.getDocument().getLength() > 0);
126 | NEXT.setEnabled(state);
127 | REPLACE.setEnabled(state);
128 | foundOne = false;
129 | }
130 | });
131 |
132 | con.gridx = 0;
133 | con.gridy = 0;
134 | grid.setConstraints(lblFword, con);
135 | north.add(lblFword);
136 |
137 | con.gridx = 1;
138 | con.gridy = 0;
139 | grid.setConstraints(searchText, con);
140 | north.add(searchText);
141 |
142 | con.gridx = 2;
143 | con.gridy = 0;
144 | grid.setConstraints(NEXT, con);
145 | north.add(NEXT);
146 |
147 | con.gridx = 0;
148 | con.gridy = 1;
149 | grid.setConstraints(lblRword, con);
150 | north.add(lblRword);
151 |
152 | con.gridx = 1;
153 | con.gridy = 1;
154 | grid.setConstraints(replaceText, con);
155 | north.add(replaceText);
156 |
157 | con.gridx = 2;
158 | con.gridy = 1;
159 | grid.setConstraints(REPLACE, con);
160 | north.add(REPLACE);
161 |
162 |
163 | }
164 |
165 |
166 | @Override
167 | public void actionPerformed(ActionEvent e) {
168 | if (e.getSource().equals(searchText) || e.getSource().equals(replaceText)) {
169 | validate(); // validate method er maddhome amara process er moddhe j poriborton gulo korechi seta update hobe. eti use na korle amra poriborton gulo dekhte parbo na
170 | }
171 | process();
172 |
173 | if(e.getActionCommand().equals("Replace All")) {
174 | replaceAll();
175 | }
176 | }
177 |
178 | private void process() {
179 | if (isReplace) {
180 | statusInfo.setText("Replacing " + searchText.getText());
181 | } else {
182 | statusInfo.setText("Searching For " + searchText.getText());
183 | }
184 |
185 | /* This is the parameter for passing in to the search method */
186 | int caret = Notepad.getArea().getCaretPosition();
187 | String word = getWord();
188 | String text = getAllText();
189 |
190 | caret = search(text, word, caret);
191 | if (caret < 0) {
192 | endResult(false, 0);
193 | }
194 | }
195 |
196 | private void endResult(boolean isReplaceAll, int tally) {
197 | String message = "";
198 | if (isReplaceAll) {
199 | if(tally == 0) {
200 | message = searchText.getText() + " not found";
201 | } else if(tally == 1) {
202 | message = "One change was made to " + searchText.getText();
203 | } else {
204 | message = "" + tally + " changes were made to " + searchText.getText();
205 | }
206 | } else {
207 | String str = "";
208 | if (isScarchDown()) {
209 | str = "Search Down";
210 | } else {
211 | str = "Search Up";
212 | }
213 | if (foundOne && !isReplace) {
214 | message = "End of " + str + " for " + searchText.getText();
215 | } else if (foundOne && isReplace) {
216 | message = "End of Replace " + searchText.getText() + " with " + replaceText.getText();
217 | }
218 | }
219 | statusInfo.setText(message);
220 | }
221 |
222 | private int search(String text, String word, int caret) {
223 | boolean found = false;
224 | int all = text.length();
225 | int check = word.length();
226 | if (isScarchDown()) {
227 | int add = 0;
228 | for (int i = caret + 1; i < (all - check); i++) {
229 | String temp = text.substring(i, (i + check));
230 | if (temp.equals(word)) {
231 | if (wholeWordIsSelected()) { /////////////////////////
232 | if (checkForWholeWord(check, text, add, caret)) { /// ekhane text er bodole all deya lagte pare
233 | caret = i;
234 | found = true;
235 | break;
236 | }
237 | } else { // this is also found bt it is not for whole word
238 | caret = i;
239 | found = true;
240 | break;
241 | }
242 | }
243 | }
244 | } else {
245 | int add = caret;
246 | for (int i = caret - 1; i >= check; i--) {
247 | add--;
248 | String temp = text.substring((i - check), i);
249 | if (temp.equals(word)) {
250 | if (wholeWordIsSelected()) {
251 | if (checkForWholeWord(check, text, add, caret)) {
252 | caret = i;
253 | found = true;
254 | break;
255 | }
256 | } else { // this is also found bt it is not for whole word
257 | caret = i;
258 | found = true;
259 | break;
260 | }
261 | }
262 | }
263 | }
264 | Notepad.getArea().setCaretPosition(0);
265 | if (found) {
266 | Notepad.getArea().requestFocus();
267 | if (isScarchDown()) {
268 | Notepad.getArea().select(caret, caret + check);
269 | } else {
270 | Notepad.getArea().select(caret - check, caret);
271 | }
272 |
273 | /* For Replace*/
274 | if(isReplace) {
275 | String replace = replaceText.getText();
276 | Notepad.getArea().replaceSelection(replace);
277 |
278 | if(isScarchDown()) {
279 | Notepad.getArea().select(caret, caret + replace.length());
280 | } else {
281 | Notepad.getArea().select(caret - replace.length(), caret);
282 | }
283 | }
284 |
285 |
286 | foundOne = true;
287 | return caret;
288 | }
289 | return -1;
290 | }
291 |
292 |
293 | /* For getting the Text*/
294 | private String getAllText() {
295 | if (caseNotSelected()) {
296 | return Notepad.getArea().getText().toLowerCase();
297 | }
298 | return Notepad.getArea().getText();
299 | }
300 |
301 |
302 | /* For getting the word */
303 | private String getWord() {
304 | if (caseNotSelected()) {
305 | return searchText.getText().toLowerCase();
306 | }
307 | return searchText.getText();
308 | }
309 |
310 | // getWord ti case sensetive or case insensetive hote pare, er jonno case er jonno notun method
311 | private boolean caseNotSelected() {
312 | return !cbCase.isSelected();
313 | }
314 |
315 | private boolean isScarchDown() {
316 | return down.isSelected();
317 | }
318 |
319 | private boolean wholeWordIsSelected() {
320 | return cbWhole.isSelected();
321 | }
322 |
323 | private boolean checkForWholeWord(int check, String text, int add, int caret) {
324 | int offsetLeft = (caret + add) - 1;
325 | int offsetRight = (caret + add) + check;
326 | if ((offsetLeft < 0) || (offsetRight > text.length())) {
327 | return true;
328 | }
329 | return ((!Character.isLetterOrDigit(text.charAt(offsetLeft))) && (!Character.isLetterOrDigit(text.charAt(offsetRight))));
330 |
331 | }
332 |
333 | private void replaceAll() {
334 | String word = searchText.getText();
335 | String text = Notepad.getArea().getText();
336 | String insert = replaceText.getText();
337 |
338 | StringBuffer sb = new StringBuffer(text);
339 | int diff = insert.length() - word.length();
340 | int offset = 0;
341 | int tally = 0;
342 |
343 | for(int i = 0; i <= (text.length() - word.length()); i++) {
344 | String temp = text.substring(i, i + word.length());
345 |
346 | if((temp.equals(word) && checkForWholeWord(word.length(), text, 0, i))) {
347 | tally++;
348 | sb.replace(i + offset, i + offset + word.length(), insert);
349 | offset += diff;
350 | }
351 | }
352 |
353 | Notepad.getArea().setText(sb.toString());
354 | endResult(true, tally);
355 | Notepad.getArea().setCaretPosition(0);
356 | }
357 | }
358 |
--------------------------------------------------------------------------------
/Java/Notepad/src/Notepad.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 | import javax.swing.event.UndoableEditEvent;
3 | import javax.swing.event.UndoableEditListener;
4 | import javax.swing.undo.CannotRedoException;
5 | import javax.swing.undo.CannotUndoException;
6 | import javax.swing.undo.UndoManager;
7 | import java.awt.*;
8 | import java.awt.event.ActionEvent;
9 | import java.awt.event.ActionListener;
10 | import java.awt.event.KeyEvent;
11 | import javax.swing.KeyStroke;
12 | import java.awt.Toolkit;
13 | import java.io.*;
14 | import java.util.StringTokenizer;
15 |
16 | public class Notepad extends JFrame {
17 | static JTextArea mainArea;
18 | JMenuBar menuBar;
19 | JMenu menuFile, menuEdit, menuFormat, menuHelp;
20 | // File Menu Item
21 | JMenuItem itemNew, itemOpen, itemSave, itemSaveAs, itemExit;
22 | // Edit Menu Item
23 | JMenuItem itemCut, itemCopy, itemPaste;
24 | // Format Menu Item
25 | JMenuItem itemFontColor, itemFind, itemReplace, itemFont;
26 | JMenuItem itemAbout;
27 | JCheckBoxMenuItem wordWarp;
28 | String fileName;
29 | JFileChooser jc;
30 | String fileContent;
31 | UndoManager undo;
32 | UndoAction undoAction;
33 | RedoAction redoAction;
34 | String findText;
35 | int fnext = 1;
36 |
37 | // For finding purpose
38 | public static Notepad frmNotepad = new Notepad();
39 |
40 | // Helper class for font
41 | FontHelper font;
42 |
43 |
44 | /* Constructors */
45 | public Notepad() {
46 | initComponent();
47 |
48 |
49 | /* Action Listeners */
50 |
51 | /* Listener for Save*/
52 | itemSave.addActionListener(new ActionListener() {
53 | @Override
54 | public void actionPerformed(ActionEvent e) {
55 | save();
56 | }
57 | });
58 |
59 | /* Listener for Save As*/
60 | itemSaveAs.addActionListener(new ActionListener() {
61 | @Override
62 | public void actionPerformed(ActionEvent e) {
63 | saveAs();
64 | }
65 | });
66 |
67 | /* Listener for Open*/
68 | itemOpen.addActionListener(new ActionListener() {
69 | @Override
70 | public void actionPerformed(ActionEvent e) {
71 | open();
72 | }
73 | });
74 |
75 | /* Listener for Open New*/
76 | itemNew.addActionListener(new ActionListener() {
77 | @Override
78 | public void actionPerformed(ActionEvent e) {
79 | openNew();
80 | }
81 | });
82 |
83 | /* Listener for Exit */
84 | itemExit.addActionListener(new ActionListener() {
85 | @Override
86 | public void actionPerformed(ActionEvent e) {
87 | System.exit(0);
88 | }
89 | });
90 |
91 | /* Listener for Cut */
92 | itemCut.addActionListener(new ActionListener() {
93 | @Override
94 | public void actionPerformed(ActionEvent e) {
95 | mainArea.cut();
96 | }
97 | });
98 |
99 | /* Listener for Copy */
100 | itemCopy.addActionListener(new ActionListener() {
101 | @Override
102 | public void actionPerformed(ActionEvent e) {
103 | mainArea.copy();
104 | }
105 | });
106 |
107 | /* Listener for Paste */
108 | itemPaste.addActionListener(new ActionListener() {
109 | @Override
110 | public void actionPerformed(ActionEvent e) {
111 | mainArea.paste();
112 | }
113 | });
114 |
115 | /* Adding Undo and Redo */
116 | mainArea.getDocument().addUndoableEditListener(new UndoableEditListener() {
117 | @Override
118 | public void undoableEditHappened(UndoableEditEvent e) {
119 | undo.addEdit(e.getEdit());
120 | undoAction.Update();
121 | redoAction.Update();
122 | }
123 | });
124 |
125 |
126 | /* Listener for Ward Warp */
127 | wordWarp.addActionListener(new ActionListener() {
128 | @Override
129 | public void actionPerformed(ActionEvent e) {
130 | if (wordWarp.isSelected()) {
131 | mainArea.setLineWrap(true);
132 | mainArea.setWrapStyleWord(true);
133 | } else {
134 | mainArea.setLineWrap(false);
135 | mainArea.setWrapStyleWord(false);
136 | }
137 | }
138 | });
139 |
140 | /* Listener for Font Color */
141 | itemFontColor.addActionListener(new ActionListener() {
142 | @Override
143 | public void actionPerformed(ActionEvent e) {
144 | Color c = JColorChooser.showDialog(rootPane, "Coose Font Color", Color.black);
145 | mainArea.setForeground(c);
146 | }
147 | });
148 |
149 |
150 | itemFind.addActionListener(new ActionListener() {
151 | @Override
152 | public void actionPerformed(ActionEvent e) {
153 | new FindAndReplace(frmNotepad, false);
154 | }
155 | });
156 |
157 |
158 | itemReplace.addActionListener(new ActionListener() {
159 | @Override
160 | public void actionPerformed(ActionEvent e) {
161 | new FindAndReplace(frmNotepad, true);
162 | }
163 | });
164 |
165 | itemFont.addActionListener(new ActionListener() {
166 | @Override
167 | public void actionPerformed(ActionEvent e) {
168 | font.setVisible(true);
169 | }
170 | });
171 |
172 | font.getOk().addActionListener(new ActionListener() {
173 | @Override
174 | public void actionPerformed(ActionEvent e) {
175 | mainArea.setFont(font.font());
176 | font.setVisible(false);
177 | }
178 | });
179 |
180 | font.getCancel().addActionListener(new ActionListener() {
181 | @Override
182 | public void actionPerformed(ActionEvent e) {
183 | font.setVisible(false);
184 | }
185 | });
186 |
187 | itemAbout.addActionListener(new ActionListener() {
188 | @Override
189 | public void actionPerformed(ActionEvent e) {
190 | final String aboutText =
191 | "Your Javapad
"
192 | + "Developed By Niloy
"
193 | + "
I Used jdk11.1 to compile the source code.
"
194 | + "Thanx for using Notepad!
"
195 | + "Ur Comments as well as bug reports r very welcome at
"
196 | + "
nkkundu2018@gmail.com
";
197 | JOptionPane.showMessageDialog(rootPane, aboutText, "Dedicated 2 u!", JOptionPane.INFORMATION_MESSAGE);
198 | }
199 | });
200 | }
201 |
202 |
203 | private void initComponent() {
204 | /* File Chooser */
205 | jc = new JFileChooser(".");
206 |
207 | /* Default Configuration */
208 | mainArea = new JTextArea();
209 | getContentPane().add(mainArea);
210 | getContentPane().add(new JScrollPane(mainArea), BorderLayout.CENTER);
211 | setTitle("Untitled Notepad");
212 | setSize(800, 600);
213 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
214 | setLocationRelativeTo(null);
215 |
216 | // for Font
217 | font = new FontHelper();
218 |
219 | /* Undo Part */
220 | undo = new UndoManager();
221 | // IconImage for Undo
222 | ImageIcon iconUndo = new ImageIcon(getClass().getResource("img/undo.png"));
223 | ImageIcon iconRedo = new ImageIcon(getClass().getResource("img/redo.png"));
224 | undoAction = new UndoAction(iconUndo);
225 | redoAction = new RedoAction(iconRedo);
226 |
227 |
228 | /* Menu Bar */
229 | menuBar = new JMenuBar();
230 |
231 | // Menu
232 | menuFile = new JMenu("File");
233 | menuEdit = new JMenu("Edit");
234 | menuFormat = new JMenu("Format");
235 | menuHelp = new JMenu("Help");
236 |
237 | // add Icon to Menu Item
238 | ImageIcon iconNew = new ImageIcon(getClass().getResource("img/new.png"));
239 | ImageIcon icoOpen = new ImageIcon(getClass().getResource("img/open.PNG"));
240 | ImageIcon iconSave = new ImageIcon(getClass().getResource("img/save.PNG"));
241 | ImageIcon iconSaveAs = new ImageIcon(getClass().getResource("img/saveas.png"));
242 | ImageIcon iconExit = new ImageIcon(getClass().getResource("img/exit.PNG"));
243 | ImageIcon iconCut = new ImageIcon(getClass().getResource("img/cut.png"));
244 | ImageIcon iconCopy = new ImageIcon(getClass().getResource("img/copy.png"));
245 | ImageIcon iconPaste = new ImageIcon(getClass().getResource("img/paste.png"));
246 | ImageIcon iconFind = new ImageIcon(getClass().getResource("img/find.png"));
247 | ImageIcon iconReplace = new ImageIcon(getClass().getResource("img/replace.png"));
248 | ImageIcon iconFont = new ImageIcon(getClass().getResource("img/font.PNG"));
249 |
250 | /* Menu Item */
251 | itemNew = new JMenuItem("New", iconNew);
252 | itemOpen = new JMenuItem("Open", icoOpen);
253 | itemSave = new JMenuItem("Save", iconSave);
254 | itemSaveAs = new JMenuItem("Save As", iconSaveAs);
255 | itemExit = new JMenuItem("Exit", iconExit);
256 | itemCut = new JMenuItem("Cut", iconCut);
257 | itemCopy = new JMenuItem("Copy", iconCopy);
258 | itemPaste = new JMenuItem("Paste", iconPaste);
259 | itemFind = new JMenuItem("Find", iconFind);
260 | itemReplace = new JMenuItem("Replace", iconReplace);
261 | wordWarp = new JCheckBoxMenuItem("Word Wrap");
262 | itemFontColor = new JMenuItem("Font Color");
263 | itemFont = new JMenuItem("Font", iconFont);
264 | itemAbout = new JMenuItem("About Notepad");
265 |
266 |
267 | /* Adding Shortcut Key */
268 | itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
269 | itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
270 | itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
271 |
272 |
273 | /* For Mac OS Shortcut Key */
274 | itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
275 | itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
276 | itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
277 |
278 |
279 | /* Add Menu Item */
280 | // Adding in Menu File
281 | menuFile.add(itemNew);
282 | menuFile.add(itemOpen);
283 | menuFile.add(itemSave);
284 | menuFile.add(itemSaveAs);
285 | menuFile.addSeparator();
286 | menuFile.add(itemExit);
287 |
288 | // Adding for Menu Edit
289 | menuEdit.add(undoAction);
290 | menuEdit.add(redoAction);
291 | menuEdit.addSeparator();
292 | menuEdit.add(itemCut);
293 | menuEdit.add(itemCopy);
294 | menuEdit.add(itemPaste);
295 | menuEdit.add(itemFind);
296 | menuEdit.add(itemReplace);
297 | menuEdit.add(itemFont);
298 |
299 |
300 | // Adding for Menu Format
301 | menuFormat.add(wordWarp);
302 | menuFormat.add(itemFontColor);
303 |
304 | // Adding for Menu help
305 | menuHelp.add(itemAbout);
306 |
307 |
308 |
309 |
310 | /* Add Menu Item to Menu Bar */
311 | menuBar.add(menuFile);
312 | menuBar.add(menuEdit);
313 | menuBar.add(menuFormat);
314 | menuBar.add(menuHelp);
315 |
316 | // add Menu bar to frame
317 | setJMenuBar(menuBar);
318 |
319 |
320 | }
321 |
322 |
323 | /* Save Method */
324 | private void save() {
325 | PrintWriter fout = null;
326 | //int returnValue = -1;
327 |
328 | try {
329 | if (fileName == null) {
330 | saveAs();
331 | } else {
332 | fout = new PrintWriter(new FileWriter(fileName));
333 |
334 | String s = mainArea.getText();
335 | StringTokenizer st = new StringTokenizer(s, System.getProperty("line.separator"));
336 | while (st.hasMoreElements()) {
337 | fout.println(st.nextToken()); // ekti ekti kore line (jekhane \n nei) seti write korbe
338 | }
339 | JOptionPane.showMessageDialog(rootPane, "File Saved!");
340 | fileContent = mainArea.getText();
341 |
342 | }
343 | } catch (IOException e) {
344 |
345 | } finally {
346 | if (fout != null)
347 | fout.close();
348 | }
349 | }
350 |
351 |
352 | /* Save As */
353 | private void saveAs() {
354 | PrintWriter fout = null;
355 | int returnValue = -1;
356 | try {
357 | returnValue = jc.showSaveDialog(this);
358 |
359 | if (returnValue == JFileChooser.APPROVE_OPTION) {
360 | if (jc.getSelectedFile().exists()) {
361 | int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to replace the file?", "Confirmation", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
362 |
363 | if (option == 0) {
364 | fout = new PrintWriter(new FileWriter(jc.getSelectedFile()));
365 | String s = mainArea.getText();
366 | StringTokenizer st = new StringTokenizer(s, System.getProperty("line.separator"));
367 | while (st.hasMoreElements()) {
368 | fout.println(st.nextToken()); // ekti ekti kore line (jekhane \n nei) seti write korbe
369 | }
370 | JOptionPane.showMessageDialog(rootPane, "File Saved!");
371 | fileContent = mainArea.getText();
372 | fileName = jc.getSelectedFile().getName();
373 | setTitle(fileName = jc.getSelectedFile().getName());
374 | } else {
375 | saveAs();
376 | }
377 | } else {
378 | fout = new PrintWriter(new FileWriter(jc.getSelectedFile()));
379 | String s = mainArea.getText();
380 | StringTokenizer st = new StringTokenizer(s, System.getProperty("line.separator"));
381 | while (st.hasMoreElements()) {
382 | fout.println(st.nextToken()); // ekti ekti kore line (jekhane \n nei) seti write korbe
383 | }
384 | JOptionPane.showMessageDialog(rootPane, "File Saved!");
385 | fileContent = mainArea.getText();
386 | fileName = jc.getSelectedFile().getName();
387 | setTitle(fileName = jc.getSelectedFile().getName());
388 | }
389 | }
390 | } catch (IOException e) {
391 | e.printStackTrace();
392 | } finally {
393 | if(fout != null) {
394 | fout.close();
395 | }
396 | }
397 | }
398 |
399 | /* Open Method */
400 | private void open() {
401 | try {
402 | int returnValue = jc.showOpenDialog(this);
403 | if (returnValue == JFileChooser.APPROVE_OPTION) {
404 | mainArea.setText(null);
405 | Reader in = new FileReader(jc.getSelectedFile());
406 | char[] buffer = new char[10000000];
407 | int nch;
408 | while ((nch = in.read(buffer, 0, buffer.length)) != -1) {
409 | mainArea.append(new String(buffer, 0, nch));
410 | }
411 |
412 | fileName = jc.getSelectedFile().getName();
413 | setTitle(fileName);
414 | }
415 | } catch (Exception e) {
416 | e.printStackTrace();
417 | }
418 | }
419 |
420 | /* Open New */
421 | private void openNew() {
422 | if (!mainArea.getText().equals("") && !mainArea.getText().equals(fileContent)) {
423 | if (fileName == null) {
424 | int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to save the changes?");
425 | if (option == 0) {
426 | saveAs();
427 | clear();
428 | } else if (option == 2) {
429 |
430 | } else {
431 | clear();
432 | }
433 | } else {
434 | int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to save the changes?");
435 | if (option == 0) {
436 | save();
437 | clear();
438 | } else if (option == 2) {
439 |
440 | } else {
441 | clear();
442 | }
443 | }
444 | } else {
445 | clear();
446 | }
447 | }
448 |
449 | /* Clear Method*/
450 | private void clear() {
451 | mainArea.setText(null);
452 | setTitle("Untitled Notepad");
453 | fileName = null;
454 | fileContent = null;
455 | }
456 |
457 | /* Class for Undo */
458 | class UndoAction extends AbstractAction {
459 | public UndoAction(ImageIcon undoIcon) {
460 | super("Undo", undoIcon);
461 | setEnabled(false);
462 | }
463 |
464 | @Override
465 | public void actionPerformed(ActionEvent e) {
466 | try {
467 | undo.undo();
468 | } catch (CannotUndoException ex) {
469 | ex.printStackTrace();
470 | }
471 |
472 | Update();
473 | redoAction.Update();
474 | }
475 |
476 | /* Update Undo */
477 | private void Update() {
478 | if (undo.canUndo()) {
479 | setEnabled(true);
480 | putValue(Action.NAME, "Undo");
481 | } else {
482 | setEnabled(false);
483 | putValue(Action.NAME, "Undo");
484 | }
485 | }
486 | }
487 |
488 | /* Class for Redo */
489 | class RedoAction extends AbstractAction {
490 | public RedoAction(ImageIcon redoIcon) {
491 | super("Rodo", redoIcon);
492 | setEnabled(false);
493 | }
494 |
495 | @Override
496 | public void actionPerformed(ActionEvent e) {
497 | try {
498 | undo.redo();
499 | } catch (CannotRedoException ex) {
500 | ex.printStackTrace();
501 | }
502 |
503 | Update();
504 | undoAction.Update();
505 | }
506 |
507 | /* Update Redo */
508 | private void Update() {
509 | if (undo.canRedo()) {
510 | setEnabled(true);
511 | putValue(Action.NAME, "Redo");
512 | } else {
513 | setEnabled(false);
514 | putValue(Action.NAME, "Redo");
515 | }
516 | }
517 | }
518 |
519 |
520 | public static void main(String[] args) {
521 | Notepad jn = new Notepad();
522 | jn.setVisible(true);
523 | }
524 |
525 | public static JTextArea getArea() {
526 | return mainArea;
527 | }
528 | }
529 |
--------------------------------------------------------------------------------