text base programs in java - java

i want to create a terminal application in java, but i don't know how to replace text lines that are already printed (kind of "edit the lines that already printed"). \r only returns to the beginning of the last line, and i want to display a 2 dimensional grid.
this is a sample for what i want to print:
System.out.println("################");
System.out.println("#--------------#");
System.out.println("#--------------#");
System.out.println("################");
System.out.println("\r################");
System.out.println("#-------X------#");
System.out.println("#--------------#");
System.out.println("################");

you should try JLine library. It provides many userful functions for command line applications. http://jline.sourceforge.net/javadoc/

Using System.out.println whatever text is printed to the console is already flushed and cannot be edited. What you can do is you can clear the console and reprint using a fresh set Sys Outs. But for a graphical application I would suggest you have a look at JAVA AWT which will give you functions like paint() and repaint() and other rich UI functions.
For clearing the console you can refer to this: clear console
For JAVA AWT: AWT tutorial

Related

Recognize arrow keys in Java Scanner or Console application

I am writing a Java application that uses both Scanner and Console to get input from the user. I would like to add command history support so that the user can use the ARROW KEYS to search previous inputs (similar to a terminal). Is there a way to do this? Right now, when I use either the Scanner or the Console, I get weird symbols like ^[[A when pressing the arrow keys.
I have read about KeyListener and KeyEvent, but my application does not use a GUI.
Thanks!
Good question - +1'd. On Windows, I would SetConsoleMode to change the console to take raw input, but on *Nix it looks like there is more work involved. Unfortunately I don't have any code that I can show you right now, but have a look at this link and see if it helps.
http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/

how to set a specific position though command line?

String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.

How do I create a gui for java that acts like a console

I created a text-based game similar to Zork and I need a gui to run it outside of Eclipse. I want to run it as a jar. (by the way I'm on a mac if that changes anything). I only need an output field and an input field. What would be the easiest way to achieve this?
And how much of my code would I need to change? (I used System.out.print for output and a Scanner for input)
If you want to crate GUI like console the simple way to do it is to add textarea component to your frame or or panel that has scroll bars through the viewport. Create a stream that feeds the component with text. Then simply redirect standard output to that stream. Finish. Start your GUI and enjoy the console.
If you don't want to run this on a terminal, you should probably use Swing with a JTextArea in which you append all the messages to the user, and a simple JTextField for the user to enter his commands.
Here's a quick example of JTextArea so you get an idea. You'll need to read more about events on Swing to make things like reacting to the user pressing the ENTER key to read the contents of the text field and run the game logic.
Note that the screenshot on the example above uses the "Metal" look and feel, but it should look much closer to a native application on the Mac.

combine multiple windows to a single window

I am developing an application which have the following windows
If the information entered in the windows are correct than only the user will be prompted with the windows in the above sequence. Now the customer has demanded me with this user interface.
Now I have to add all these windows in the last window format, with the specification's as the user will be allowed in the 2nd portion of the last image if the first information entered is correct.And the user when launches this app see the last image and can change the values as any time in the respective portions of the last window.
I have coded it in Swing Java.I am new to Java. I am working in Netbeans 7.1.2 I have three files as
1)Login.java
-containing my LoginDemo class which have main and form object of extended Jframe class
-Login class extending J frame and implementing action listener(this class creates an J frame of next file Enter the information.
2)Algorithm.java
creates new J frame object of next file if information is correct.
3)TravellingSalesmanProblem.java
gives the output as shown in Optimal Travel Route window.
I am accessing the information using REST call to a website.
So can anyone help me in this?
This will depend on how you structured you code. If you simply placed components directly onto the the windows/frames themselves, then you might be in for some work.
Alternatively, if you used panels, which you then placed onto windows, this might save you some time.
Anyway. Assuming you only have windows.
for each window do
myLastWindow.add(window.getContentPane());
This is pretty simple, but you'll also need to know the layout you want. I'd suggest something like GridLayout or VerticalLayout from the SwingX project.

Colours in Java console

basically, I was wondering if there is any way of specifying the colours of the background and text in a Java console application. I am writing a console menu. Also, if there is any easy way of clearing the console.
Thanks.
You can use ANSI escape codes to specify the foreground and background colors of text for console apps on many platforms, assuming the terminal you're using to run the application supports ANSI mode. You don't need any additional libraries to use these codes, you can just embed them directly in your strings. However, since they're a bit messy looking, you can use a library such as JCurses to make it a bit easier to apply the various ANSI codes.
Here's an example program:
public static void main(String args[]) {
System.out.println((char)27+"[01;31m;This text is red."+(char)27+"[00;00m");
System.out.println((char)27+"[01;32m;This text is green."+(char)27+"[00;00m");
}
As a bonus, ANSI escape codes will help you with screen clearing and cursors positioning, as well.

Categories