| Real'sHowTo |
|
|
Custom Search
|
| Real'sHowTo |
|
|
Custom Search
|
To clear the screen, you can use many System.out.println();, that's about it!
for (int i=0; i<25; i++)
System.out.println();
... Or use some JNI functions, see this HowTo.
Fortunately many Open Source solutions are coming to the rescue and Jansi and JCurses are two of them.
import org.fusesource.jansi.AnsiConsole;
public class Test {
public static final String ANSI_CLS = "\u001b[2J";
public static final String ANSI_HOME = "\u001b[H";
public static final String ANSI_BOLD = "\u001b[1m";
public static final String ANSI_AT55 = "\u001b[10;10H";
public static final String ANSI_REVERSEON = "\u001b[7m";
public static final String ANSI_NORMAL = "\u001b[0m";
public static final String ANSI_WHITEONBLUE = "\u001b[37;44m";
public static void main(String args[]){
AnsiConsole.systemInstall();
AnsiConsole.out.println(ANSI_CLS);
AnsiConsole.out.println
(ANSI_AT55 + ANSI_REVERSEON + "Hello world" + ANSI_NORMAL);
AnsiConsole.out.println
(ANSI_HOME + ANSI_WHITEONBLUE + "Hello world" + ANSI_NORMAL);
AnsiConsole.out.print
(ANSI_BOLD + "Press a key..." + ANSI_NORMAL);
try {System.in.read();}catch(Exception e){}
AnsiConsole.out.println(ANSI_CLS);
AnsiConsole.systemInstall();
}
}
The above example shows that it's possible to use ANSI codes directly but Jansi provides a neat mechanism to help building the required ANSI sequence.
import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;
...
System.out.println( ansi().eraseScreen().fg(RED).a("Hello").fg.(GREEN).a(" World").reset() )
Jansi works on Linux32/64, Windows 32/64 and OS/X.
JCurses works on Unix and Windows (32 bit only, on a 64-bit OS you need to use JCurses with a 32-bit JVM).
This example will display a character-based window with a label, a textfield and a button (don't click with you mouse, use the keyboard!).
import jcurses.system.*;
import jcurses.widgets.*;
import jcurses.util.*;
import jcurses.event.*;
public class Test2 extends Window implements ItemListener, ActionListener,
ValueChangedListener, WindowListener, WidgetsConstants {
static Test2 window = null;
static TextField textfield = null;
static Button button = null;
public Test2(int width, int height) {
super(width, height, true, "JCurses Test");
}
public static void main(String[] args) throws Exception {
window = new Test2(30, 20);
window.init();
}
public void init() {
DefaultLayoutManager mgr = new DefaultLayoutManager();
mgr.bindToContainer(window.getRootPanel());
mgr.addWidget(
new Label("Hello World!",
new CharColor(CharColor.WHITE, CharColor.GREEN)),
0, 0, 20, 10,
WidgetsConstants.ALIGNMENT_CENTER,
WidgetsConstants.ALIGNMENT_CENTER);
textfield = new TextField(10);
mgr.addWidget(textfield, 0, 0, 20, 20,
WidgetsConstants.ALIGNMENT_CENTER,
WidgetsConstants.ALIGNMENT_CENTER);
button = new Button("Quit");
mgr.addWidget(button, 0, 0, 20, 30,
WidgetsConstants.ALIGNMENT_CENTER,
WidgetsConstants.ALIGNMENT_CENTER);
button.setShortCut('q');
button.addListener(this);
window.addListener((WindowListener) this);
window.show();
}
public void actionPerformed(ActionEvent event) {
Widget w = event.getSource();
if (w == button) {
new Message("HowTo", "You are about to quit", "OK").show();
window.close();
}
}
public void stateChanged(ItemEvent e) { }
public void valueChanged(ValueChangedEvent e) { }
public void windowChanged(WindowEvent event) {
if (event.getType() == WindowEvent.CLOSING) {
event.getSourceWindow().close();
// Toolkit.clearScreen(new CharColor(CharColor.WHITE, CharColor.BLACK));
}
}
}
Written and compiled by Réal Gagnon ©1998-2013
[ home ]