import java.util.ArrayList;
import java.util.Scanner;
public class Cryptogram {
final String ENCODED_MESSAGE = "WXZP XM JCB TQVP";
ArrayList<Character> encodedList, decodedList;
Scanner kb = new Scanner(System.in);
public Cryptogram(){
// initialize lists
encodedList = new ArrayList<Character>();
decodedList = new ArrayList<Character>();
for (int i = 0; i < ENCODED_MESSAGE.length(); i++){
char ch = ENCODED_MESSAGE.charAt(i);
encodedList.add(new Character(ch));
if (Character.isLetter(ch)){
decodedList.add(new Character('*'));
}
else
decodedList.add(new Character(ch));
}
}
/** Run the program */
public void run(){
boolean done = false;
while (!done){
// get a letter
Character letter = getLetter();
// get a replacing letter
Character replacingLetter = getReplacingLetter();
// replace letters
for (int i = 0; i < encodedList.size(); i++)
if (encodedList.get(i).equals(letter))
decodedList.set(i, replacingLetter);
// display messages
display(encodedList, "En Message: ");
display(decodedList, "De Message: ");
// check no asterisks
if (!decodedList.contains(new Character('*'))){
// ask user to continue or not
System.out.print("Is the message decoded? (y/n) ");
if (kb.next().toUpperCase().charAt(0) == 'Y')
done = true;
}
System.out.println();
}
}
/** Get a letter */
private Character getLetter(){
Character letter = null;
boolean done = false;
while (!done){
try {
System.out.print("Pick a letter from message: ");
letter = kb.next().toUpperCase().charAt(0);
if (!encodedList.contains(letter) || !Character.isLetter(letter))
throw new LetterMissingException();
done = true;
} catch (LetterMissingException e) {
System.out.println("No such letter in encoded message. Please try again!");
}
}
return letter;
}
/** Get a replacing letter */
private Character getReplacingLetter(){
Character letter = null;
boolean done = false;
while (!done){
try {
System.out.print("Replace: ");
letter = kb.next().toUpperCase().charAt(0);
if (!(Character.isLetter(letter) || letter.equals(new Character('*'))))
throw new InvalidInputException();
done = true;
} catch (InvalidInputException e) {
System.out.println("Invalid input. Please try again!");
}
}
return letter;
}
/** Display message */
private void display(ArrayList<Character> list, String messageName){
System.out.print(messageName);
for (Character ch : list)
System.out.print(ch);
System.out.println();
}
public static void main(String[] args){
new Cryptogram().run();
}
}
Java GUI
Page 1 of 11 Replies - 139 Views - Last Post: 17 July 2012 - 02:34 AM
#1
Java GUI
Posted 16 July 2012 - 11:38 PM
I'm trying to make this code I wrote into GUI. I'm new to Java and was hoping if anyone can point the direction I should take with this project.
Replies To: Java GUI
#2
Re: Java GUI
Posted 17 July 2012 - 02:34 AM
Think about how this program would look in a GUI. Maybe 2 text fields, one for entry of the message, another for output of the encoded/ciphered message - or vice-versa. Maybe a button to encode/decode. (I glanced quickly through your code, so I'm not sure exactly what it does.)
Once you have a rough idea how the user interface would look for this program, learn to make a simple JFrame containing a JPanel with the needed components: a couple JTextFields and a JButton. I suggest you start with the Swing tutorials here. There's a series of five that are pretty good, then others here, and still many others available throughout the Internet. You'll end up doing several before you get the hang of it.
Once you have the basic user interface designed and built, then you can add the logic you've shown us here to the GUI. Obviously, there will be some translation involved, and in the end very little of the actual code you've posted here may be used exactly as it appears, but the same general design will work. You should be able to use the logic you have as an outline for how to program the logic of the GUI.
OR
For a more simplistic GUI, you could communicate with the user through Dialogs. It's similar to how you're doing it now through the console but using a graphical interface.
Good luck, and come back when you need help.
Once you have a rough idea how the user interface would look for this program, learn to make a simple JFrame containing a JPanel with the needed components: a couple JTextFields and a JButton. I suggest you start with the Swing tutorials here. There's a series of five that are pretty good, then others here, and still many others available throughout the Internet. You'll end up doing several before you get the hang of it.
Once you have the basic user interface designed and built, then you can add the logic you've shown us here to the GUI. Obviously, there will be some translation involved, and in the end very little of the actual code you've posted here may be used exactly as it appears, but the same general design will work. You should be able to use the logic you have as an outline for how to program the logic of the GUI.
OR
For a more simplistic GUI, you could communicate with the user through Dialogs. It's similar to how you're doing it now through the console but using a graphical interface.
Good luck, and come back when you need help.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|