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.
Related
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/
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
Please find the below sample code, The UTF-8 character properly displaying in windows machine. But, its not proper for Linux machine (Ubuntu).
import javax.swing.JOptionPane;
public class JContPaneTest
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3002\u30c7\u30fc\u30bf\u30d9\u30fc", "Error",JOptionPane.ERROR_MESSAGE);
}
}
Is there any way to solve this problem?
Not all fonts contain the full unicode set of glyphs. The blobs are probably the result of a deficient font, but in java it's hard to determine what font is actually being used, and I don't know a way to determine if these blobs are being emitted except by seeing them.
I had huge problems with unicode in pop-up menus, which probably is a very similar problem
to yours.
2 things to try.
1) Write a font test to display your string in all fonts that are available to java.
2) Try using a non-swing component. Non-swing components use the underlying OS font support.
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.
I'm essentially trying to mimic the default windows xp simple calculator. When I change the background colours of the buttons in Java it makes them look very flat and "boring". I want to make the buttons look as close as possible to the buttons in the Windows XP calculator.
Here is an image comparing mine to WinXp's:
Is there some kind of method I can use to change the style of the buttons much like you can do in Visual Basic to make the buttons almost pop more or look 3D like the Windows Xp Calculator.
The default buttons in Java are sort of what I'm looking for except there not white there more of a blue kind of colour in a gradient.
Is this possible, or am I stuck with ugly button?
Try setting the system look and feel at the beginning of the main method:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
This will load all Swing widgets with a native-ish look.
If you are insterested in what you are actually doing with this command, Oracle has a nice tutorial regarding look and feels: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html