Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Keyboard Cursor HSA Console
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: Wed Feb 03, 2010 9:35 pm   Post subject: Keyboard Cursor HSA Console

In the following program I am prompting user input and I'm using a carriage return and line feed to locate my cursor at the very bottom. The problem I'm having is if the user holds down the enter key at a prompt, my graphics "Scroll Up". I am porting the code into a JFrame using KeyListener, hoping it will fix my problem, but it's taking quite a while and I'd rather submit in my assignment ASAP then get some sleep. I attached the StackeQueueTester.java and stack.class files if you wish to run the code for a visual glance.

Java:

/***********************************

    Program:    Stack and Queue Tester
    Date:       February 3, 2010

***********************************/

import hsa.Console;     // Holtsoft's console
import java.awt.*;      // Graphics

public class StackQueueTester
{

    static Console c;

    public static boolean isNumeric (String number)  // Checks if param is a number
    {
        try
        {
            Integer.parseInt (number);      // Attempt to convert number
        }
        catch (Exception e)
        {
            return false;                   // Fail
        }
        return true;                        // Succeess
    }


    public static void main (String[] args)
    {
        c = new Console ();

        boolean quit = false;
        boolean initialized = false;
        String terminal = "";

        // Positions main output
        int paragraphX = 30;
        int paragraphY = 30;

        Color bgcolor = Color.black;
        Color fgcolor = Color.blue;
        Color textcolor = Color.white;

        String escapeChar = "q";                                // Used to quit entering values

        int data[] = {};                                        // Data to be manipulated on the stack
        stack s = new stack ();                                 // New stack instance

        while (!quit)
        {

            c.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");   // Moves cursor to bottom

            c.setColor (bgcolor);
            c.fillRect (0, 0, c.getWidth (), c.getHeight ());


            c.setColor (fgcolor);
            c.fillRect (paragraphX - 10, paragraphY - 10, 250, 70);
            c.fillRect (paragraphX - 10, 120, 500, 30);


            c.setColor (textcolor);
            c.setFont (new Font ("Verdana", Font.BOLD, 12));
            c.drawString ("Program:    Stacks", paragraphX, paragraphY + 10);
            c.drawString ("Date:           February 3, 2010", paragraphX, paragraphY + 30);


            c.drawString ("1 - Create New   2 - Is Empty   3 - Add to   4 - Pop Off   5 - View Top", paragraphX, paragraphY + 110);

            c.drawString ("Type a number in the menu and press Enter", paragraphX, paragraphY + 150);

            c.setColor (Color.green);
            c.drawString (terminal, paragraphX, paragraphY + 170);

            String choice = c.readString ();

            if (isNumeric (choice))
            {
                if (Integer.parseInt (choice) >= 1 && Integer.parseInt (choice) <= 5)
                {
                    if (Integer.parseInt (choice) == 1) // Creates a new stack
                    {
                        s.init ();           // Initializes the stack
                        initialized = true;
                        terminal = "Stack is now initialized!";
                    }
                    else if (Integer.parseInt (choice) == 2)
                    {
                        if (initialized)
                        {
                            if (s.empty ())
                                terminal = "Stack is empty!";
                            else
                                terminal = "Stack is not empty!";
                        }
                        else
                            terminal = "Must initialize a stack first!";
                    }
                    else if (Integer.parseInt (choice) == 3)
                    {
                        if (initialized)
                        {
                            terminal = "";
                            String val = " ";
                            String all = " ";
                            while (!val.equalsIgnoreCase (escapeChar))
                            {
                                c.setColor (bgcolor);
                                c.fillRect (0, 0, c.getWidth (), c.getHeight ());
                                c.setColor (textcolor);
                                c.drawString ("Type a number then press ", paragraphX, paragraphY + 10);
                                c.drawString (" to add to the stack", paragraphX + 220, paragraphY + 10);

                                c.drawString ("Type '  ' then press", paragraphX, paragraphY + 30);
                                c.drawString ("to return to the Main Menu", paragraphX + 178, paragraphY + 30);
                                c.setColor (Color.pink);
                                c.drawString (escapeChar.toUpperCase (), paragraphX + 38, paragraphY + 30);
                                c.drawString ("ENTER", paragraphX + 130, paragraphY + 30);
                                c.drawString ("ENTER", paragraphX + 175, paragraphY + 10);
                                c.setColor (new Color (150, 150, 255));
                                c.drawString (all, paragraphX - 7, paragraphY + 80);
                                c.setColor (Color.green);
                                c.drawString (terminal, paragraphX, paragraphY + 110);

                                val = c.readString ();

                                if (isNumeric (val))
                                {
                                    s.push (val);
                                    all += "   " + val;
                                    terminal = "";
                                }
                                else if (val.equalsIgnoreCase (escapeChar))
                                    break;
                                else if (!isNumeric (val))
                                    terminal = "Please enter a numeric value";
                            }
                        }
                        else
                            terminal = "Must initialize a stack first!";
                    }
                    else if (Integer.parseInt (choice) == 4)
                    {
                        if (initialized)
                        {
                            if (!s.empty ())
                            {
                                s.pop ();
                                terminal = "Popped value from top of the stack";
                            }
                            else
                                terminal = "Stack is empty! Cannot pop";
                        }
                        else
                            terminal = "Must initialize a stack first!";
                    }
                    else if (Integer.parseInt (choice) == 5)
                    {
                        if (initialized)
                            terminal = "Top of the stack: " + s.top ();
                        else
                            terminal = "Must initialize a stack first!";
                    }
                }
                else
                    terminal = "Please enter a number from the menu";
            }
            else
                terminal = "Please enter a numeric value";
        }
    }
}



StackQueue.zip
 Description:

Download
 Filename:  StackQueue.zip
 Filesize:  2.11 KB
 Downloaded:  99 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: