Hey All,
I was wondering what would be the easiest process in converting a java program I wrote, into a Applet. I tried to do some googling, but was unable to find anything solid. Any direction or advice would be much appreciated.
The code block below is for my main method. While the second code block is called in the main.
Thanks,
I was wondering what would be the easiest process in converting a java program I wrote, into a Applet. I tried to do some googling, but was unable to find anything solid. Any direction or advice would be much appreciated.
The code block below is for my main method. While the second code block is called in the main.
Thanks,
import java.applet.Applet; import java.io.*; import javax.swing.JOptionPane; import java.util.Scanner; public class Shopping List{ public static void main( String[] args ) { //If&Else statement to check if there are 2 arguments in the commandline (The two .txt files) if (args.length != 1 ){ System.out.println ("Please choose your output file destination"); } else { File f = new File(args[0]); //If&Else statement to check if specified file exists if(f.exists() ) { MyQueue<String> myQueue = new MyQueue<String>(10); uiMenu(myQueue); outputToFile(myQueue, args[0]); } else { System.out.println ("Could not find file: " + args[0] ); System.out.println ("First argument should be a file name of the input file. Please check your input and try again."); } } } /** * uiMenu() Method is the UI for the the program. It creates a JPane with option listed * below. There is a switch statement below that displays certain output depending on * the user input. * *@param myQueue parameter for the Queue to be read */ public static void uiMenu(MyQueue<String> myQueue){ String[] choices = {"Add Item", "Pickup Item", "Checkout"}; String outputLine = ""; String queueElement = ""; boolean showUi = true; while (showUi) { String input; int response = JOptionPane.showOptionDialog( null // Center in window. , "Hello! I am your personalized shopper. Please review the instructions below. \n\nAdd Item: Allows you to add a item to your grocery list. \nPickup Item: Removes the next item on your grocery list. \nCheckout: Closes the program. Do only once you are completely done shopping. \n\nYour Shopping List:\n" + myQueue.toString()// Message , "Shopping List 2012" // Title in titlebar , JOptionPane.YES_NO_OPTION // Option type , JOptionPane.PLAIN_MESSAGE // messageType , null // Icon (none) , choices // Button text as above. , "Button" // Default button's label ); //Uses a switch statement to check which button was clicked. switch (response) { case 0: input = JOptionPane.showInputDialog ( "Enter your grocery product:" ); if (input != null && !input.equals("") ){ if (!myQueue.offer(input) ) JOptionPane.showMessageDialog(null, "Your shopping list is full. You will not be able to carry that many items. Please edit your list."); } break; case 1: if (myQueue.empty() ) { outputLine = "Your shopping list is empty! It seems you have everything you need. Please Procede to the Checkout."; } else { queueElement = myQueue.poll(); outputLine ="You have just picked up " + queueElement + ", it has been crossed off your shopping list."; } JOptionPane.showMessageDialog(null, outputLine); break; case -1: showUi = false; break; default: showUi = false; break; } outputLine = ""; queueElement = ""; } } /** * outputToFile() Method writes to the output file * *@param outputFileName is the parameter for the output file *@param myQueue parameter for the Queue to be read */ public static void outputToFile(MyQueue myQueue, String outputFileName){ try{ // Create file FileWriter fstream = new FileWriter(outputFileName); BufferedWriter out = new BufferedWriter(fstream); String s = myQueue.toString() + "\n "; out.write(s); //Close the output stream out.close(); } catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } } //myQueue class class MyQueue<T> extends ArrayQueue<T>{ //constructor public MyQueue(int maxSize){ super(maxSize); } /** * toString() Method returns a string that displays each name on a different line * *@return s returns the string to be printed */ public String toString(){ String s = ""; int tempIndex = frontIndex; if(currentSize == 0){ return "Your shopping list is empty! Please add items to your cart or continue to the checkout line."; } while(tempIndex <= endIndex){ s = s + array[tempIndex] + "\n"; tempIndex = tempIndex + 1; } return s; } }
/** * A Queue class implemented with a circular array * */ public class ArrayQueue<T> implements QueueInterface<T> { // data fields // Index of element at front of the queue protected int frontIndex = 0; // Index of element at end of the queue // Value is -1, because we have not added any elements yet. protected int endIndex = -1; // Current number of elements in queue protected int currentSize = 0; // Maximum number of elements in queue (set by constructor) protected int maxSize; // Array to store elements of T type protected T array[ ]; /** * Constructor * * @param max is the maximum number of elements in the queue */ public ArrayQueue(int max) { maxSize = max; // Have to do casting, because we cannot instantiate // an array unless we know its type. // Casting is putting a class name in parenthesis, // which tells the compiler that a variable // is of a specific type. // protected T array[] = new T[arraySize]; //wrong syntax /* * May create following error message, but can't be avoided with arrays. * Note: ArrayQueue.java uses unchecked or unsafe operations. Note: * Recompile with -XlInteger:unchecked for details. */ array = (T[ ]) new Object[maxSize]; } /** * Tests if the Queue is empty * * @return true/false if empty/not empty */ public boolean empty() { // see if size of array is 0 boolean emptyArray = (currentSize == 0); return emptyArray; } /** * Tests if the Queue is full * * @return true/false if full/not full */ private boolean full() { // See if size of array is at maximum boolean fullArray = (currentSize == maxSize); return fullArray; } /** * Adds an element to the end of the Queue * * @param element * is added to the end of the Queue * @return true if it was possible to add the element to Queue, else false */ public boolean offer(T element) { // check to see if full if (!this.full()) { // increment the endIndex // use modulus to "wrap around" the array endIndex = (endIndex + 1) % maxSize; // assign element to array array[endIndex] = element; // increment current size of queue currentSize++; // successfully added to array return true; } // if full, cannot add to array return false; } /** * Retrieves and removes the from the front of Queue, or null if Queue * is empty * * @return the front of Queue, or null if Queue is empty */ public T poll() { // create a variable of class T T element = null; // if array is not empty, then get element from front of queue if (!this.empty()) { // get the front element's address element = array[frontIndex]; // increment frontIndex // take the modulus to wrap around the array frontIndex = (frontIndex + 1) % maxSize; // decrement current size of queue currentSize--; } // return either null or the element return element; } public T peek() { // create a variable T element = null; // check to see if not empty if (!this.empty()) { // get the address of front element element = array[frontIndex]; } // return either null or element return element; } }// end of class