Display console output on a separate window [java] - java

I'm making a tic-tac-toe game in Java and so far it's going well, but as of now, I've written my program to print to console. Is there a command to get what would currently be displayed on my console output to show up on a separate window? Something like a JOptionPane?
My console right now looks like a ticTacToe grid, and I want to maybe get a while loop to display the currentBoard to a pop-up-dialog after every set of turns. So the player can see the updated Board after every turn set.
Is this possible?

You can redirect System.out messages to a JTextArea or JTextPane.
Check out the Message Console class.

Related

System.err.print(ln) on the same line as a system.out.println? JAVA

I have a CLI game that I have to make for a uni project and (I know its bad practice) want the lives to be printed in red. I was planning on using the System.err.print to print the hearts in red which works, however they won't join onto my System.out.print("") output.
Is this possible or can I provide some sort of override func?
When using the code;
System.out.print("Player Lives: ");
System.out.print("♥ ♥ ♥ ♥ ♥") My output will be on the same line as expected. However, when I change the code to: System.out.print("Player Lives: ");
System.err.print("♥ ♥ ♥ ♥ ♥") the output will be split on to two seperate lines...
Netbeans IDE v12.0...
any help or ideas would be greatly appreciated!
Thank you!
If the hearts don't represent an error, don't print them on an error stream.
You want the hearts in red, well that is pretty easy to do. You need to learn a bit about consoles (not too much) and what it means to print in color on a console.
Consoles are programs that replaced phyiscal devices. In the old days, a console was something like a television screen hooked up to a communications channel. This TV screen had a keyboard too. The console was responsible for sending information to the "computer" and for receiving data from the computer (console control codes) and presenting that information.
Most of the data sent and received was text. Today, we simulate the console with a program, and the "tty device" is the simulation of the communications channel (a device file today, instead of a modem or serial line). When you type, the data is written to the "tty" attached to your console, and when the computer displays text, the console reads the tty and shows whatever it read.
The reason this is important for color is because you need to print console control codes in your output. The first console control code is to shift the console into displaying red text, the second is to shift the console into displaying "normal" text. With a little more research, you can display (easily) 16 different colors, combined with 16 different background colors, flashing text, etc. Some consoles support more "modes" of operation, you can experiment with yours. There is a standard set of modes, widely supported by nearly all consoles, called ANSI modes.
The default information to a console is text, which is then displayed as text. To get a console to accept a command, it will not be plain text.
public static final String RED = "\u001B[31m";
public static final String RESET = "\u001B[0m";
the first string will shift the console into red text, the second will reset the color mode to default.
So to print a red "hello" you would then do:
System.out.println(RED + "hello" + RESET);
And you could make a function to print red stuff
public void printRed(String message) {
System.out.println(RED + message + RESET);
}
or a function to build a "wrapped" red string, etc.
The simple answer to this is: Just don't do it!
When you send output to System.out and System.err, you have no control over how the output is going to interleave when displayed on a "console". The interleaving is going to depend on a number of factors that are outside of the control of a Java program.
You may be able to make use of your IDE's "feature" of displaying System.err output in red on its console window, but you will find that it doesn't work in other contexts1. For example, from a typical command shell.
It is often possible to get a console to output text in different colors. However this is relying on the console to support (typically) ANSI Escape Codes. These are not necessarily enabled on your end user's console / terminal / terminal emulator. If they are not enabled, the user gets a bunch of weird stuff on their screen.
There are ways to deal with this (e.g. using a Java terminal library), but there will always be rough edges. For example, if your Lecturer / TA runs your app, captures the output and tries to view it with (say) less, they are likely to see weird stuff rather than colors.
My advice: unless it is a specific assignment requirement to output colorized text (and dingbat characters like "♥") ... don't do it. You are making your assignment more difficult, and setting yourself for losing marks if you don't get it right in the context that the lecturer / ta uses to run and mark your code.
(You might argue that this is a useful thing to learn to do. My counter is that it is a lot less useful than you would think. Most applications these days use a GUI framework or a web browser to implement their user interface. Console based interfaces are typically viewed as old fashioned.)
1 - I note your comment that your lecturer is expecting to be given the entire NetBeans project, and will run the code in the same IDE.

How do I create a LibGDX text input inside a window? (no pop up)

I have been working on a libGDX project and was wondering if I could create a login screen with the use of some kind of text inputs. I have been looking all around for text input fields, but the only one I can seem to find are those that pop up, which I don't really want to use.
My project is a desktop game only and uses the screen implementations combinede with a game super class.
Is there any other way to get text input inside the window, so not as a pop up?
As said in the comment, the right way to make a login screen is to use Scene2d.ui and its TextField. Check out the Scene2d.ui wiki page and Skin Composer to get going.

Java : Interactive Console using jTextArea

I am developing a cmd like console using jTextArea, for an IDE. What I want to do is as I click the execute button, it provides me interactive input/output screen in the jTextArea.
I am able to grab the output contents of the cmd screen, but what if user wants to run a command like take two integers as input from stdin (here from my jTextArea) and display the sum, on my console screen, as if it happened in a normal cmd/terminal.
How do I send input to that instance/session?
See java.lang.Process, method java.lang.Process.getOutputStream()
The name OutputStream is a doubtful choice because it's the input of the underlying process, the output for your program.

Continuous Updating in Java Panel or Dialog Box

I am a huge newbie and I have a program that normally prints items to the Java console window. I would like this program to become a window in which the user can interact with. The reason why I have not resorted to dialog boxes and panels is because this program require multiple prints to the console window. A traditional dialog box does not continuously update or compound on data that has already been printed on the box. I realize that there is another way of doing this by creating a program that mimics the Java console window. Because I am a noob, all of the java console redirecting questions and answers on this site have blown over my head. Can anyone please help me?
See maybe How to Use Editor Panes and Text Panes will be helpful and give you some ideas.
The short answer is, every time you want to update the contents of a text box, call the setText function again. There's no "append" function on the contents: you have to give the entire contents each time. If you want something that mimics a console window, where messages continue to scroll, the simplest thing to do is to keep the entire contents in a StringBuilder. Each time you get new text append to the StringBuilder, then setText(myStringBuilder.toString).
You could, I supppose, write mybox.setText(mybox.getText()+"new contents"). That would be a little inefficient but probably not a big deal.
I don't know exactly what you're up to, but trying to redirect console output to a text box sounds like more nuisance that it's worth. Just put your data in the text box: don't write it somewhere else, then try to get it back and put it where you want it. I suppose if you have thousands of lines of code writing to the console and now you want it to go a text box, there might be value in not having to change all that code. But the structure of a console app is so different from the structure of a GUI app that changing the output statements would probably be the least of the things you'd have to rework.

How to redirect all console output to a GUI textbox?

I currently have a program that prints lines of text to the screen in various manners such as 'System.out.println()' statements and for loops the print all elements in an array to screen.
I am now adding a GUI to this program. My problem is that I want to print everything that prints to eclipse's console to a textbox in my GUI instead. Is this possible and if so how would I go about doing this.
Thanks in Advance.
Check out this blog article, entitled Redirecting System.out and System.err to JTextPane or JTextArea. It describes almost everything you need.
The basic idea is that you create your own specialized output stream. In your implementation of the write() methods, you call some code to append the new data to your text box. Then, you set this new output stream as your System.out by calling System.setOut() or System.setErr().
NOTE: that article is missing one thing. You need to start your program in a separate thread.
An idea:
Create your own PrintStream that outputs everything to this textbox.
Then set this new PrintStream to be the standard output stream like that:
System.setOut(myPrintStream());

Categories

Resources