Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

38
https://www.facebook.com/Oxus20 [email protected] Fal-e Hafez (Omens of Hafez) Cards in Persian Using JAVA File I/O Concept Random Concept Scanner Concept Component Orientation Concept Prepared By: Fereshteh Nourooziyan Edited By: Abdul Rahman Sherzad

description

This presentation explains step by step how to develop and code Fal-e Hafez (Omens of Hafez) Cards in Persian Using JAVA. There are several applications which are coded by different programming languages i.e. Java languages for Desktops and Mobiles, HTML and CSS and PHP for Web Pages, etc. and this shows the importance of Omens of Hafez among the Persian people.

Transcript of Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Page 1: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

https://www.facebook.com/Oxus20

[email protected]

Fal-e Hafez (Omens of Hafez) Cards in Persian

Using JAVA

File I/O Concept

Random Concept

Scanner Concept

Component Orientation Concept

Prepared By: Fereshteh Nourooziyan

Edited By: Abdul Rahman Sherzad

Page 2: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Topics

» Introduction to Hafez Omen

» File I/O Concept

» Random Concept

» Scanner Concept

» Component Orientation Concept

» Hafez Omen Implementation

2

https://www.facebook.com/Oxus20

Page 3: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Hafez Biography » Khajeh Shamseddin Mohammad Hafez Shirazi

(Persian: محمد حافظ شیرازی الدینخواجه شمس ‎ )‎, known by his pen name Hafez (حافظ also Hafiz) was a Persian poet.

» His collected works composed of series of Persian literature are to be found in the homes of most people in Afghanistan and Iran who learn his poems by heart and use them as proverbs and sayings to this day.

» His life and poems have been the subject of much analysis, commentary and interpretation, influencing post-fourteenth century Persian writing more than any other author.

3

https://www.facebook.com/Oxus20

Page 4: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Omens of Hafez

» In the Persian tradition, whenever one faces a

difficulty or a fork in the road, or even if one

has a general question in mind, one would hold

that question in mind, and then ask the Oracle

of "Khajeh Shamseddin Mohammad Hafez-s

Shirazi" for guidance.

4

https://www.facebook.com/Oxus20

Page 5: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Omens of Hafez

» There are several applications which are coded

by different programming languages such as:

˃ Java languages for Desktops and Mobiles

˃ HTML and CSS and PHP for Web Pages

» that it shows the importance of Omens of Hafez

among the Persian people.

5

https://www.facebook.com/Oxus20

Page 6: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Omens of Hafez Interface

6

https://www.facebook.com/Oxus20

Page 7: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Omens of Hafez Interface

» This application is made by using Java

Language with the help of following classes:

˃ File I/O Class

˃ Random Class

˃ Scanner Class

˃ Exception Handling

7

https://www.facebook.com/Oxus20

Page 8: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Streams

» A stream is an object that enables the flow of

data between a program and some I/O device

or file

˃ If the data flows into a program, then the stream is called an

input stream

˃ If the data flows out of a program, then the stream is called an

output stream

https://www.facebook.com/Oxus20

8

Page 9: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Streams

» Input streams can flow from the keyboard or from a file

˃ System.in is an input stream that connects to the

keyboard

Scanner keyboard = new Scanner(System.in);

» Output streams can flow to a screen or to a file

˃ System.out is an output stream that connects to the

screen

System.out.println("Output stream");

https://www.facebook.com/Oxus20

9

Page 10: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Text Files

» Files that are designed to be read by human beings,

and that can be read or written with an editor are

called text files

˃ Text files can also be called ASCII files because the data they

contain uses an ASCII encoding scheme

˃ An advantage of text files is that the are usually the same on

all computers, so that they can move from one computer to

another

https://www.facebook.com/Oxus20

10

Page 11: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File and Directory Management

» In Java Language we use an object belongs to Java.io

package.

» The object is used to store the name of the file or directory

and also the pathname.

» In addition, this object can be used to create, rename, or

delete the file or directory it represents.

» Important point to remember is that java.io.File object can

represent both File and Directory in Java. 11

https://www.facebook.com/Oxus20

Page 12: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Class Constructors

» The File object represents the actual file / directory on

the disk.

» There are following constructors to create a File object:

12

https://www.facebook.com/Oxus20

File(File parent, String child); // Creates a new File instance from a parent abstract pathname and a child pathname string.

File(String pathname); // Creates a new File instance by converting the given pathname string into an abstract pathname.

File(String parent, String child); // Creates a new File instance from a parent pathname string and a child pathname string.

File(URI uri); // Creates a new File instance by converting the given file: URI into an abstract pathname.

Page 13: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Class Methods Summary I

13

https://www.facebook.com/Oxus20

S.N. Method & Description

1 boolean canExecute() This method tests whether the application can execute the file denoted by this abstract pathname.

2 boolean canRead() This method tests whether the application can read the file denoted by this abstract pathname.

3 boolean canWrite() This method tests whether the application can modify the file denoted by this abstract pathname.

4 int compareTo(File pathname) This method compares two abstract pathnames lexicographically.

5 boolean createNewFile() This method atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

6 static File createTempFile(String prefix, String suffix) This method creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

7 static File createTempFile(String prefix, String suffix, File directory) This method Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

8 boolean delete() This method deletes the file or directory denoted by this abstract pathname.

9 void deleteOnExit() This method requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

10 boolean equals(Object obj) This method tests this abstract pathname for equality with the given object.

Page 14: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Class Methods Summary II

14

https://www.facebook.com/Oxus20

S.N. Method & Description

11 boolean exists() This method tests whether the file or directory denoted by this abstract pathname exists..

12 File getAbsoluteFile() This method returns the absolute form of this abstract pathname.

13 String getAbsolutePath() This method returns the absolute pathname string of this abstract pathname.

14 File getCanonicalFile() This method returns the canonical form of this abstract pathname.

15 String getCanonicalPath() This method returns the canonical pathname string of this abstract pathname.

16 long getFreeSpace() This method returns the number of unallocated bytes in the partition named by this abstract path name.

17 String getName() This method returns the name of the file or directory denoted by this abstract pathname.

18 String getParent() This method returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

19 File getParentFile() This method returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

48 …

Page 15: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Creation Demo

15

https://www.facebook.com/Oxus20

import java.io.File;

public class FileCreationDemo {

public static void main(String[] args) {

File f = null;

boolean bool = false;

try {

// create new file

f = new File("E:\\FileDemo\\myFile.txt");

// tries to create new file in the system

bool = f.createNewFile();

// prints

System.out.println("File created: " + bool);

} catch (Exception e) {

System.err.println("Error creating the file!");

}

}

}

Page 16: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Creation Output

16

https://www.facebook.com/Oxus20

Page 17: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Direction Creation Demo

17

https://www.facebook.com/Oxus20

import java.io.File;

public class DirectoryCreationDemo {

public static void main(String[] args) {

File file = new File("E:\\FileDemo\\myDirectory");

if (!file.exists()) {

if (file.mkdir()) {

System.out.println("Directory is created!");

} else {

System.out.println("Failed to create directory!");

}

}

}

}

Page 18: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Direction Creation Output

18

https://www.facebook.com/Oxus20

Page 19: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Methods Demo import java.io.File; public class FileMethodsDemo { public static void main(String[] args) { File f = new File("E:\\FileDemo\\myFile.txt"); if (f.exists()) { System.out.println(f.getName() + " exists."); System.out.println(f.getName() + " size is " + f.length() + " bytes"); if (f.canRead()) System.out.println(f.getName() + " can be read."); if (f.canWrite()) System.out.println(f.getName() + " can be write."); if (f.isFile()) System.out.println(f.getName() + " is a file."); } else { System.out.println(f.getName() + " does not exists!"); } } }

19

https://www.facebook.com/Oxus20

Page 20: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

File Methods Output

» myFile.txt exists.

» myFile.txt size is 0 bytes

» myFile.txt can be read.

» myFile.txt can be write.

» myFile.txt is a file.

20

https://www.facebook.com/Oxus20

Page 21: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Introduction to Random » java.util.Random class generates a stream of pseudo-

random numbers.

» To create a new random number generator, use one of the following methods: ˃ new Random(); //This creates a new random number generator

˃ new Random(long seed); //This creates a new random number generator using a single long seed

» Using of Random Class: ˃ Random class has many usages in many programs and application which need

selecting randomly without prior planning, such as:

+ Lottery Games and Applications

+ Quotes and Speech of the Day

+ Random Advertisements 21

https://www.facebook.com/Oxus20

Page 22: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Some Methods of Random Class » protected int next(int bits)

˃ This method generates the next pseudorandom number.

» Boolean nextBoolean()

˃ This method returns the next pseudorandom, uniformly distributed Boolean value from this random number generator's sequence.

» void nextBytes(byte[] bytes)

˃ This method generates random bytes and places them into a user-supplied byte array.

» double nextDouble()

˃ This method returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

» loat nextFloat()

˃ This method returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence

» long nextLong()

˃ This method returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

22

https://www.facebook.com/Oxus20

Page 23: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Random Demo import java.util.Random; public class RandomDemo { public static void main(String args[]) { Random r = new Random(); int arr[] = new int[25]; for (int i = 0; i < 1000; i++) { arr[r.nextInt(25)]++; } for (int i = 0; i < 25; i++) { System.out.println(i + " was generated " + arr[i] + " times."); } } //end of main } //end of class

23

https://www.facebook.com/Oxus20

Page 24: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Random Demo Output

24

https://www.facebook.com/Oxus20

Page 25: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Scanner Class

» The java.util.Scanner class is a simple text scanner

which can parse primitive types and strings using

Regular Expressions.

» Following are the important points about Scanner

class:

˃ A Scanner breaks its input into tokens using a delimiter pattern, which by default

matches whitespace (blanks, tabs, and newline).

˃ A scanning operation may block waiting for input.

˃ A Scanner is not safe for multithreaded use without external synchronization.

25

https://www.facebook.com/Oxus20

Page 26: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Scanner Read File Demo import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class ScannerReadFileDemo {

public static void main(String[] args) {

String output = "";

Scanner in = null;

String fn = "E:\\FileDemo\\myFile.txt";

try {

in = new Scanner(new File(fn));

while (in.hasNext()) {

output += in.nextLine();

output += "\n";

}

} catch (FileNotFoundException e) {

System.out.println("File does not exist !");

} finally {

in.close();

}

System.out.println(output);

}

} 26

https://www.facebook.com/Oxus20

Page 27: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Scanner Read File Demo Output

27

https://www.facebook.com/Oxus20

Page 28: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Scanner Read Keyboard Demo import java.util.Scanner; public class ScannerReadKeyboardDemo { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the subject:"); String subject = keyboard.nextLine(); System.out.println("Enter the score:"); double score = keyboard.nextDouble(); System.out.println("Subject: " + subject); System.out.println("Score: " + score); } } 28

https://www.facebook.com/Oxus20

Page 29: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Scanner Read Keyboard Output

29

https://www.facebook.com/Oxus20

Page 30: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Introduction to ComponentOrientation

» java.awt.ComponentOrientation allows components to

encapsulate language-sensitive orientation information.

» Followings are the capability of this Class is:

˃ Vertically alignment

˃ Horizontally alignment

˃ Left-to-right

˃ Right-to-left

» Therefore, by using this class capabilities we can make and

develop multi-lingual applications in JAVA. 30

https://www.facebook.com/Oxus20

Page 31: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

ComponentOrientation Demo import java.awt.ComponentOrientation;

import java.awt.Font;

import javax.swing.JFrame;

import javax.swing.JTextArea;

public class ComponentOrintationDemo extends JFrame {

JTextArea txtArea;

public ComponentOrintationDemo() {

setTitle("Component Orintation Demo");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setSize(300, 300);

txtArea = new JTextArea();

txtArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

txtArea.setFont(new Font("arial", Font.BOLD, 27));

add(txtArea);

setVisible(true);

}

public static void main(String[] args) {

new ComponentOrintationDemo();

}

} 31

https://www.facebook.com/Oxus20

Page 32: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

ComponentOrientation Output Lift to Right Right to Lift

32

https://www.facebook.com/Oxus20

Page 33: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Omens of Hafez Implementation package OmensOfHafez;

import java.awt.Color;

import java.awt.ComponentOrientation;

import java.awt.Cursor;

import java.awt.Font;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Random;

import java.util.Scanner;

import javax.swing.BorderFactory;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class OmensOfHafez {

private JFrame win;

private JTextArea txtContent;

private JScrollPane scroll;

private JLabel lblCloseImage, lblCloseHover, lblBackground, lblChooser, lblBack;

private String filenames[] = { "1", "2", "3", "4", "5" }; 33

https://www.facebook.com/Oxus20

Page 34: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

public OmensOfHafez() {

win = new JFrame();

win.setUndecorated(true);

win.setLayout(null);

win.setSize(480, 605);

win.setLocationRelativeTo(null);

lblBackground = new JLabel(new ImageIcon(getClass().getResource("Interface.PNG")));

lblCloseImage = new JLabel(new ImageIcon(getClass().getResource("btnClose.PNG")));

lblCloseHover = new JLabel(new ImageIcon(getClass().getResource("btncloseHover.PNG")));

lblBack = new JLabel(new ImageIcon(getClass().getResource("lblBack.PNG")));

win.add(lblCloseHover).setBounds(455, 0, 20, 20);

win.add(lblCloseImage).setBounds(455, 0, 20, 20);

lblBackground.add(lblBack).setBounds(0, 535, 59, 45);

lblBack.setVisible(false);

lblBack.setToolTipText("Back");

lblCloseHover.setVisible(false);

lblCloseHover.setToolTipText("Close");

txtContent = new JTextArea();

txtContent.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

txtContent.setFont(new Font("Traditional Arabic", Font.BOLD, 15));

txtContent.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

txtContent.setLineWrap(true);

txtContent.setWrapStyleWord(true);

txtContent.setOpaque(false);

scroll = new JScrollPane(txtContent);

scroll.setOpaque(false);

scroll.getViewport().setOpaque(false);

scroll.setVisible(false);

lblBackground.add(scroll).setBounds(15, 57, 440, 470); 34

https://www.facebook.com/Oxus20

Page 35: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

lblChooser = new JLabel(new ImageIcon(getClass().getResource("lblchooser.png")));

lblBackground.add(lblChooser).setBounds(275, 520, 230, 100);

MouseListener l = new MouseAdapter() {

public void mousePressed(MouseEvent e) {

if (e.getSource() == lblCloseHover) {

System.exit(0);

} else if (e.getSource() == lblChooser) {

lblBackground.setIcon(new ImageIcon(getClass().getResource("BackroundImage.PNG")));

scroll.setVisible(true);

Random random = new Random();

int t = random.nextInt(filenames.length);

txtContent.setText(getContent(filenames[t]));

lblChooser.setVisible(false);

lblBack.setVisible(true);

// Fix the scroll area for the JTextArea

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

scroll.getVerticalScrollBar().setValue(0);

}

});

} else if (e.getSource() == lblBack) {

lblBackground.setIcon(new ImageIcon(getClass().getResource("Interface.PNG")));

scroll.setVisible(false);

lblBack.setVisible(false);

lblChooser.setVisible(true);

}

} 35

https://www.facebook.com/Oxus20

Page 36: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

public void mouseEntered(MouseEvent e) {

if (e.getSource() == lblCloseImage) {

lblCloseHover.setVisible(true);

} else if (e.getSource() == lblBack) {

lblBack.setIcon(new ImageIcon(getClass().getResource("lblBackHover.PNG")));

}

}

public void mouseExited(MouseEvent e) {

if (e.getSource() == lblBack) {

lblBack.setIcon(new ImageIcon(getClass().getResource("lblBack.PNG")));

}

}

};

lblCloseImage.addMouseListener(l);

lblCloseImage.setCursor(new Cursor(Cursor.HAND_CURSOR));

lblCloseHover.addMouseListener(l);

lblCloseHover.setCursor(new Cursor(Cursor.HAND_CURSOR));

lblBack.addMouseListener(l);

lblBack.setCursor(new Cursor(Cursor.HAND_CURSOR));

lblChooser.addMouseListener(l);

lblChooser.setCursor(new Cursor(Cursor.HAND_CURSOR));

win.getContentPane().setBackground(Color.BLACK);

win.add(lblBackground).setBounds(5, 20, 470, 580);

win.setVisible(true);

} 36

https://www.facebook.com/Oxus20

Page 37: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

private String getContent(Object filename) {

String output = "";

Scanner in = null;

String fn = filename.toString() + ".txt";

try {

in = new Scanner(new File(fn));

while (in.hasNext()) {

output += in.nextLine();

output += "\n";

}

} catch (FileNotFoundException e) {

System.out.println(filename + ".txt does not exist !");

} finally {

in.close();

}

return output;

}

public static void main(String[] args) {

new OmensOfHafez();

}

} 37

https://www.facebook.com/Oxus20

Page 38: Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java

Omens of Hafiz Output

https://www.facebook.com/Oxus20

38