- Notifications
You must be signed in to change notification settings - Fork 8
how to use the swing console
the swing GUI console or the C class is what we use for user interface, its located inside the helper folder/package.
in this page, you will learn how to use it on its own and how to use it with the game framework.
create a new java project.
first, make sure you included C.java(get it from this repo) in your project and imported it to the page you're going to use it in (the file with the main method).
in main put this code:
C.io.setTitle("im the page title"); C.io.println("Hello World!");
the first line will set the console window title, the second line will print hello world to the console.
now we will demonstrate how to get user input: add these lines to your code
C.io.print("what is your name?"); String name = C.io.nextLine(); C.io.print("your name is "+name);
this code will ask you to input your name and then output "your name is (your input)"
and that's it, with this alone you can create a text-based game, all you need is the ability to output text and get user input.
its practically the same, but the handle for C.io is on an attribute called cio
to print you write: cio.print("bla");
to get input you use: cio.nextLine();
you can also use cio.println("bla");
to print a line.
and you can also use the normal java console output (the GUI console listens to the system output too), so you can use my console output shortcut too: S.o("bla");
also, note that the screen is cleared before menu update use the messge attribute to add a messge before the menu is printed:
messge ="bla";
or get an empty input to stop the program for a moment like "press enter to continue".
the best way to understand this is to look at a screen class and see how it works.
to get input easier I suggest you use my gyinput helper class, it does this in an easier way and validates the input too. there is an example of how to do this on almost every menu class that gets user input.
you declare gyinput as an attribute of the menu class:
gyinput ui;//ui is short for "user input"
and then you initialise it in the constructor:
ui = new gyinput(cio2);
we pass to it the cio2 from the constructor's argument (cio2 holds the c.io of the GUI console window).
and then you can use it to get user input like:
int inum = ui.get_int("select item num(0 to exit)");
this line will do 3 things, tell the user "select item num(0 to exit)", make sure the user inputs a number (validate input), and return the number so it's saved on "inum" so we can do something with that data later.
and that's how we get input.
if you want to learn more take a look in the source code in any menu (gconsole_menu) class in screens package/folder.