Created
January 27, 2020 13:25
-
-
Save raunaqsingh2020/6c26e161a9afd0b95fb6082b60f54197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import java.util.*; | |
import java.io.*; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.datatransfer.DataFlavor; | |
import java.awt.datatransfer.StringSelection; | |
import java.awt.datatransfer.UnsupportedFlavorException; | |
import java.io.IOException; | |
public class PasswordProgram { | |
private JFrame frame; | |
public PasswordProgram() { | |
frame = new JFrame("Password Program"); | |
frame.setSize(800, 800); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.setPreferredSize(frame.getSize()); | |
frame.add(new InnerProgram(frame.getSize())); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public static void main(String... argv) { | |
new PasswordProgram(); | |
} | |
public static class InnerProgram extends JPanel implements Runnable, MouseListener { | |
ArrayList<String> names = new ArrayList<String>(); | |
private Thread animator; | |
Dimension d; | |
int startX = 20; | |
int startY = 20; | |
String nName = ""; | |
Stack decrypt = new Stack<String>(); | |
String eName = ""; | |
Stack encrypt = new Stack<String>(); | |
boolean encrypting = true; | |
int prev = 0; | |
int shift = random(5,20); | |
public InnerProgram (Dimension dimension) { | |
setSize(dimension); | |
setPreferredSize(dimension); | |
addMouseListener(this); | |
addKeyListener(new TAdapter()); | |
setFocusable(true); | |
d = getSize(); | |
//for animating the screen - you won't need to edit | |
if (animator == null) { | |
animator = new Thread(this); | |
animator.start(); | |
} | |
setDoubleBuffered(true); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D)g; | |
g2.setColor(Color.black); | |
Color co = new Color(54,10,42); | |
g2.setColor(co); | |
g2.fillRect(0, 0,(int)d.getWidth() , (int)d.getHeight()); | |
startX = 20; | |
startY = 20; | |
int sideL = 100; | |
int sideW = 150; | |
int count=1; | |
co = new Color(255,255,255); | |
g2.setColor(co); | |
g2.fillRect(340,190,120,95); | |
g2.setColor(Color.red); | |
g2.fillRect(350,200,100,75); | |
g2.fillRect(700,220,50,50); | |
g2.setColor(co); | |
g2.drawString("Paste",705,250); | |
if(encrypting){ | |
g2.drawString("Decrypt",377,242); | |
}else{ | |
g2.drawString("Encrypt",377,242); | |
} | |
g2.drawString("Input: " + nName,25,350); | |
g2.drawString("Output: " + eName,25,500); | |
} | |
public static ArrayList<String> frequency(String[] arr){ | |
int[] counts = new int[arr.length]; | |
for (int i=0; i<arr.length; i++) | |
counts[i] = countOccurrences(arr, arr[i]); | |
boolean sorted = false; | |
int temp; | |
String temp2; | |
while(!sorted) { | |
sorted = true; | |
for (int i = 0; i < counts.length - 1; i++) { | |
if (counts[i] > counts[i+1]) { | |
temp = counts[i]; | |
counts[i] = counts[i+1]; | |
counts[i+1] = temp; | |
temp2 = arr[i]; | |
arr[i] = arr[i+1]; | |
arr[i+1] = temp2; | |
sorted = false; | |
} | |
} | |
} | |
ArrayList<String> mostFreq = new ArrayList<String>(Arrays.asList(arr)); | |
ArrayList<String> newList = new ArrayList<String>(); | |
// Traverse through the first list | |
for (String element : mostFreq) { | |
// If this element is not present in newList | |
// then add it | |
if (!newList.contains(element)) { | |
newList.add(element); | |
} | |
} | |
return newList; | |
} | |
public static int countOccurrences(String arr[], String x) | |
{ | |
int res = 0; | |
for (int i=0; i<arr.length; i++) | |
if (x.equals(arr[i])) | |
res++; | |
return res; | |
} | |
private void onPaste(){ | |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); | |
DataFlavor flavor = DataFlavor.stringFlavor; | |
if (clipboard.isDataFlavorAvailable(flavor)) { | |
try { | |
String text = (String) clipboard.getData(flavor); | |
System.out.println(text); | |
for (int i = 0; i < text.length(); i++) { | |
decrypt.push(text.charAt(i)); | |
} | |
} catch (UnsupportedFlavorException e) { | |
System.out.println(e); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
}//onPaste | |
public static int random (int a, int b){ | |
int max=a; | |
int min=b; | |
int random=(int)(Math.random() * (max - min) + min); | |
return random; | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
if(x <= 450 && x>= 350 && y >= 200 && y <= 275){ | |
encrypting = !encrypting; | |
while(decrypt.size()!=0) | |
decrypt.pop(); | |
while(encrypt.size()!=0) | |
encrypt.pop(); | |
} | |
if(x <= 750 && x>= 700 && y >= 220 && y <= 270) | |
onPaste(); | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
private class TAdapter extends KeyAdapter { | |
public void keyReleased(KeyEvent e) { | |
int keyr = e.getKeyCode(); | |
} | |
public void keyPressed(KeyEvent e) { | |
int key = e.getKeyCode(); | |
String c = KeyEvent.getKeyText(e.getKeyCode()); | |
c = Character.toString((char) key); | |
if(prev!=16){ | |
c = c.toLowerCase(); | |
} | |
prev = key; | |
if(encrypting){ | |
if(key==8){ | |
//nName = nName.substring(0,nName.length()-1); | |
if(decrypt.size()!=0) | |
decrypt.pop(); | |
if(encrypt.size()!=0) | |
encrypt.pop(); | |
} | |
if(key==10){ | |
while(decrypt.size()!=0) | |
decrypt.pop(); | |
while(encrypt.size()!=0) | |
encrypt.pop(); | |
} | |
if(key!=8 && key!=10 && key!=16){ | |
//nName += c; | |
decrypt.push(c); | |
char temp = c.charAt(0); | |
int ascii = (int)temp + shift; | |
if(ascii > 126){ | |
ascii = ascii - 95; | |
} | |
if(ascii < 32){ | |
ascii = ascii + 95; | |
} | |
encrypt.push(Character.toString((char)ascii)); | |
} | |
}else{ | |
if(key==8){ | |
//nName = nName.substring(0,nName.length()-1); | |
if(decrypt.size()!=0) | |
decrypt.pop(); | |
if(encrypt.size()!=0) | |
encrypt.pop(); | |
} | |
if(key==49 || key==50 || key==51 || key==52 || key==53){ | |
while(encrypt.size()!=0) | |
encrypt.pop(); | |
String[] arr = new String[decrypt.size()]; | |
for(int i = decrypt.size()-1; i > -1; i--){ | |
String temp = (decrypt.get(i)).toString(); | |
arr[i]=temp; | |
} | |
ArrayList<String> mostFreq1 = frequency(arr); | |
ArrayList<String> mostFreq = new ArrayList<String>(); | |
for (int i = mostFreq1.size() - 1; i >= 0; i--) { | |
mostFreq.add(mostFreq1.get(i)); | |
} | |
System.out.println(mostFreq); | |
String shiftToE = mostFreq.get(0); | |
if(key-49 < mostFreq.size()) | |
shiftToE = mostFreq.get(key-49); | |
char temp = shiftToE.charAt(0); | |
shift = 101-(int)temp; | |
//System.out.println(shift); | |
for(int i = decrypt.size()-1; i > -1; i--){ | |
String temp2 = (decrypt.get(i)).toString(); | |
char temp3 = temp2.charAt(0); | |
int ascii = (int)temp3 + shift; | |
if(ascii > 126){ | |
ascii = ascii - 95; | |
} | |
if(ascii < 32){ | |
ascii = ascii + 95; | |
} | |
encrypt.push(Character.toString((char)ascii)); | |
} | |
//while(decrypt.size()!=0) | |
//decrypt.pop(); | |
} | |
if(key!=8 && key!=10 && key!=16 && key!=49 && key!=50&& key!=51&& key!=52&& key!=53){ | |
while(encrypt.size()!=0) | |
encrypt.pop(); | |
decrypt.push(c); | |
/* | |
char temp = c.charAt(0); | |
int ascii = (int)temp + shift; | |
if(ascii > 126){ | |
ascii = 33 + ascii - 127; | |
} | |
encrypt.push(Character.toString((char)ascii)); | |
*/ | |
} | |
} | |
nName = ""; | |
eName = ""; | |
for(int i = decrypt.size()-1; i > -1; i--){ | |
nName = nName + decrypt.get(i); | |
} | |
for(int i = encrypt.size()-1; i > -1; i--){ | |
eName = eName + encrypt.get(i); | |
} | |
System.out.println(nName); | |
System.out.println(eName); | |
} | |
}//end of adapter | |
public void run() { | |
long beforeTime, timeDiff, sleep; | |
beforeTime = System.currentTimeMillis(); | |
int animationDelay = 37; | |
long time = System.currentTimeMillis(); | |
while (true) {// infinite loop | |
// spriteManager.update(); | |
repaint(); | |
try { | |
time += animationDelay; | |
Thread.sleep(Math.max(0, time - System.currentTimeMillis())); | |
} catch (InterruptedException e) { | |
System.out.println(e); | |
} // end catch | |
} // end while loop | |
}// end of run | |
}//end of class | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment