473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Clear the Screen

Hi,

I have done some research, trying to Clear The Screen in java code.

The first option was the obv:
system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");

then i heard about this method:
System.out.print((char)27 + "[2J");

However, it doen't work unless "ansi.sys" is loaded and very few WinXP
user's have this.

Any help appreciated in trying to solve this problem.

Cheers,
Dave
Jul 17 '05 #1
19 105708
It's kind of a hack, but what about this:

Runtime.exec("cls");

This makes your application platform-dependent, but it looks like your
stated alternative does this as well.

-Nathan

"Dave" <bigdavepotnoodle*SPAM*hotmail.com> wrote in message news:<3f***********************@news-text.dial.pipex.com>...
Hi,

I have done some research, trying to Clear The Screen in java code.

The first option was the obv:
system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");

then i heard about this method:
System.out.print((char)27 + "[2J");

However, it doen't work unless "ansi.sys" is loaded and very few WinXP
user's have this.

Any help appreciated in trying to solve this problem.

Cheers,
Dave

Jul 17 '05 #2
Ive seen this question before- with no real solution.
Surely it wouldnt have been difficult to provide a clear screen, and even
basic screen cursor positioning?
It would make text-mode java alot more useful.

"Dave" <bigdavepotnoodle*SPAM*hotmail.com> wrote in message
news:3f***********************@news-text.dial.pipex.com...
Hi,

I have done some research, trying to Clear The Screen in java code.

The first option was the obv:
system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");

then i heard about this method:
System.out.print((char)27 + "[2J");

However, it doen't work unless "ansi.sys" is loaded and very few WinXP
user's have this.

Any help appreciated in trying to solve this problem.

Cheers,
Dave

Jul 17 '05 #3

"Dave" <bigdavepotnoodle*SPAM*hotmail.com> wrote in message
news:3f***********************@news-text.dial.pipex.com...
Hi,

I have done some research, trying to Clear The Screen in java code.

The first option was the obv:
system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");

then i heard about this method:
System.out.print((char)27 + "[2J");

However, it doen't work unless "ansi.sys" is loaded and very few WinXP
user's have this.

Any help appreciated in trying to solve this problem.

Cheers,
Dave


Look at jcurses on sourceforge.

Silvio Bierman
Jul 17 '05 #4
Printing out the form-feed character will clear the screen

System.out.print("\f");

Hope this helps!

Christian
Jul 17 '05 #5

"Denz" <RU*****@RUBBISHhotmail.com> wrote in message
news:Kv***************@news-server.bigpond.net.au...

Ive seen this question before- with no real solution.
Surely it wouldnt have been difficult to provide a clear
screen, and even basic screen cursor positioning?

It would make text-mode java alot more useful.


Unfortunately these tasks are inherently operating system-specific, and
cannot be implemented in any standard way across platforms. Some platforms,
in fact, don't even understand the concepts of 'screen', or 'cursor', so
wouldn't even have any use for classes that implemented these abstractions.
Even the 'standard' C and C++ languages which, like Java, aim for platform
independance, do not implement such functionality.

System-specific tasks such as these may be performed via:

* 'Runtime.getRuntime().exec(...)'

* Writing Java Native Interface [JNI] routines which tap into
the relevant operating system functionality

The first option should really be seen as a 'quick and dirty' approach - it
allows you to perform certain system-specific tasks but with very little
control. A program, to be considered robust, would make no more than sparing
use of this facility. If you find you are relying on it heavily you may need
to seriously question whether Java is the ideal development tool for this
task [a scripting language (Perl, Python) might be more suitable ?], or more
of a development commitment needs be made by using JNI.

There is nothing inherently wrong with JNI except that its use helps defeat
one of Java's most important qualities - platform independance - and it
introduces 'weaknesses' into the application because program execution
occurs outside the control of the JVM, and any failure here could be
catastrophic. On the flipside, Java would be a very limited development
environment without it since it allows for the tapping into system-specific
functionality in a controlled, and efficient way.

So, to provide the facilities you seek, you need to implement suitable JNI
routines. Again, options are available:

* Tap into someone else's work. As another respondant has
already meantioned, the JCurses package provides a
reasonable suit of screen handling routines for the *NIX
and Windows-familiy platforms

* Write your own. This isn't as difficult as you might imagine,
and might be the best approach if all you require is a
few simple routines like:

- Clearing the screen
- Moving the cursor to specified positions
- Accepting input [say a single keystroke] without pressing
ENTER

A good start would be the JNI tutorial at the Sun site, and,
of course, perusal of the documentation for the system
functions implementing this functionality

Anyway, apologies for the longwindedness of the response. Hopefully, though,
some useful insights have been provided.

Cheers,

Anthony Borla
Jul 17 '05 #6

"Christian" <ja******@emailuser.net> wrote in message
news:32**************************@posting.google.c om...

Printing out the form-feed character will clear the screen

System.out.print("\f");

Hope this helps!


Sadly, it doesn't do as you expect - it's behaviour various with the
platform. Nice try, though :)

Cheers,

Anthony Borla
Jul 17 '05 #7

"Nathan Zumwalt" <na*****@hotmail.com> wrote in message
news:52**************************@posting.google.c om...
It's kind of a hack, but what about this:

Runtime.exec("cls");

This makes your application platform-dependent, but it looks
like your stated alternative does this as well.


Actually, this won't work. Yes, the 'cls' command is invoked, *but* it is
invoked in a new process ['command.com' or 'cmd.exe' is exected creating a
new process], and *does not* clear the Java application's console.

Cheers,

Anthony Borla
Jul 17 '05 #8
nos
But if they can implement "beep", and they do,
they should be able to implement "cls" too.

"Anthony Borla" <aj*****@bigpond.com> wrote in message
news:g0****************@news-server.bigpond.net.au...

"Denz" <RU*****@RUBBISHhotmail.com> wrote in message
news:Kv***************@news-server.bigpond.net.au...

Ive seen this question before- with no real solution.
Surely it wouldnt have been difficult to provide a clear
screen, and even basic screen cursor positioning?

It would make text-mode java alot more useful.

Unfortunately these tasks are inherently operating system-specific, and
cannot be implemented in any standard way across platforms. Some

platforms, in fact, don't even understand the concepts of 'screen', or 'cursor', so
wouldn't even have any use for classes that implemented these abstractions. Even the 'standard' C and C++ languages which, like Java, aim for platform
independance, do not implement such functionality.

System-specific tasks such as these may be performed via:

* 'Runtime.getRuntime().exec(...)'

* Writing Java Native Interface [JNI] routines which tap into
the relevant operating system functionality

The first option should really be seen as a 'quick and dirty' approach - it allows you to perform certain system-specific tasks but with very little
control. A program, to be considered robust, would make no more than sparing use of this facility. If you find you are relying on it heavily you may need to seriously question whether Java is the ideal development tool for this
task [a scripting language (Perl, Python) might be more suitable ?], or more of a development commitment needs be made by using JNI.

There is nothing inherently wrong with JNI except that its use helps defeat one of Java's most important qualities - platform independance - and it
introduces 'weaknesses' into the application because program execution
occurs outside the control of the JVM, and any failure here could be
catastrophic. On the flipside, Java would be a very limited development
environment without it since it allows for the tapping into system-specific functionality in a controlled, and efficient way.

So, to provide the facilities you seek, you need to implement suitable JNI
routines. Again, options are available:

* Tap into someone else's work. As another respondant has
already meantioned, the JCurses package provides a
reasonable suit of screen handling routines for the *NIX
and Windows-familiy platforms

* Write your own. This isn't as difficult as you might imagine,
and might be the best approach if all you require is a
few simple routines like:

- Clearing the screen
- Moving the cursor to specified positions
- Accepting input [say a single keystroke] without pressing
ENTER

A good start would be the JNI tutorial at the Sun site, and,
of course, perusal of the documentation for the system
functions implementing this functionality

Anyway, apologies for the longwindedness of the response. Hopefully, though, some useful insights have been provided.

Cheers,

Anthony Borla

Jul 17 '05 #9
While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
black dots on a white screen, and they fell thus:
But if they can implement "beep", and they do,
they should be able to implement "cls" too.

<snip top of upside-down reply>

That's because BEL is a standard ASCII character (code 7) and it's
pretty much standard that any terminal/console would render it by
beeping. OTOH, for some reason I can't imagine FF (12) doesn't
standardly mean clear screen, and nor does any other ASCII string.

But I agree that having a portable means of screen clearing would be
handy. BTW, have the rest of you people stopped telling people to try
what you can't be bothered to try for yourself?

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #10
"Denz" <RU*****@RUBBISHhotmail.com> wrote in message news:<Kv***************@news-server.bigpond.net.au>...
Ive seen this question before- with no real solution.
Surely it wouldnt have been difficult to provide a clear screen, and even
basic screen cursor positioning?
It would make text-mode java alot more useful.


Changing the cursor positions, clearing the screen, setting console
colurs...these are all functions of the console in use, not of Java.
One of the clear screen solutions touched on this by feeding the
correct escape sequence to the console to manipulate it with the
desired effect.

As you can imagine, this is tedious and time consuming. JCurses has
already been mentioned. It is an implementation of nCurses which
determines the console type being used, and wraps escape sequences
with a nice API, independant of the type of console being used.

It should be here:
http://sourceforge.net/projects/javacurses/

However, I can't help but wonder that if you need this type of
functionality from the console, perhaps you should consider the more
standard Java/Swing and make a nice GUI interface to your application.

---
Jared Dykstra
http://www.bork.org/~jared
Jul 17 '05 #11
mmm... interesting
Just tried- it works on the Linux box, but unfortunately it doesnt on
Windows- just prints a gender symbol. darn.
Going to look at jcurses

"Christian" <ja******@emailuser.net> wrote in message
news:32**************************@posting.google.c om...
Printing out the form-feed character will clear the screen

System.out.print("\f");

Hope this helps!

Christian

Jul 17 '05 #12
http://www.xdweb.net/~dibblego/javafaq/javafaq.html#q30

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
"Dave" <bigdavepotnoodle*SPAM*hotmail.com> wrote in message
news:3f***********************@news-text.dial.pipex.com...
Hi,

I have done some research, trying to Clear The Screen in java code.

The first option was the obv:
system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n");

then i heard about this method:
System.out.print((char)27 + "[2J");

However, it doen't work unless "ansi.sys" is loaded and very few WinXP
user's have this.

Any help appreciated in trying to solve this problem.

Cheers,
Dave

Jul 17 '05 #13

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bt**********@sun-cc204.lut.ac.uk...
While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
black dots on a white screen, and they fell thus:
But if they can implement "beep", and they do,
they should be able to implement "cls" too. <snip top of upside-down reply>

That's because BEL is a standard ASCII character
(code 7) and it's pretty much standard that any terminal/console
would render it by beeping.


Try it on an IBM mainframe - they use EBCDIC, not ASCII ;) !

OTOH, for some reason I can't imagine FF (12) doesn't
standardly mean clear screen, and nor does any other
ASCII string.

You'll find that 'plain vanilla' *NIX / Linux systems *do* exhibit fairly
consistent console behaviour; it's the Windows-family environments that
exhibit such inconsistencies.

But I agree that having a portable means of screen clearing
would be handy.

Maybe a nested class could be added to 'System', containing all the
system-specific console-management routines, something like:

class System
{
...
public class Console
{
...
public static native void cls();
public static native void setCurPos(int row, int col);
public static native String inputString();
public static native double inputNumeric();
...
}
...
}

You could then do something like:

System.Console.cls();

to clear the screen, and something like:

String name;

System.Console.setCurPos(10, 35);
System.out.print("Enter your name: ");
name = System.Console.inputString();

Platforms that did not support consoles or the like could then simply have
dummy [i.e. empty] methods ?

Maybe if the Sun Java developers were lobbied by enough developers they
might consider building this functionality into the API ?

I suspect, though, they will simply leave it to developers requiring console
management support to use JNI, either directly, or by downloading a package
like JCurses.

BTW, have the rest of you people stopped telling people
to try what you can't be bothered to try for yourself?


I think their intentions were sincere, and probably did do some testing on
their own systems. The error was in assuming that the same behaviour could
be expected on other systems. My general view is that any constructive
response is to be welcomed and appreciated.

Cheers,

Anthony Borla
Jul 17 '05 #14
nos

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bt**********@sun-cc204.lut.ac.uk...
While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
black dots on a white screen, and they fell thus:
But if they can implement "beep", and they do,
they should be able to implement "cls" too. <snip top of upside-down reply>

That's because BEL is a standard ASCII character (code 7) and it's
pretty much standard that any terminal/console would render it by
beeping. OTOH, for some reason I can't imagine FF (12) doesn't
standardly mean clear screen, and nor does any other ASCII string.

I thought I would try to find out how beep works by looking in the java
sdk files but i could not figure it out. All i found was

public abstract void beep();
in java/awt/Toolkit.java
But I agree that having a portable means of screen clearing would be
handy. BTW, have the rest of you people stopped telling people to try
what you can't be bothered to try for yourself?

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.

Jul 17 '05 #15
Something like this Console class would be great.
Ive just downloaded JCurses but so far looks like overkill- when just a few
methods like clear screen and cursor position would go such a long way...

"Anthony Borla" <aj*****@bigpond.com> wrote in message
news:yA***************@news-server.bigpond.net.au...

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bt**********@sun-cc204.lut.ac.uk...
While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
black dots on a white screen, and they fell thus:
But if they can implement "beep", and they do,
they should be able to implement "cls" too. <snip top of upside-down reply>

That's because BEL is a standard ASCII character
(code 7) and it's pretty much standard that any terminal/console
would render it by beeping.


Try it on an IBM mainframe - they use EBCDIC, not ASCII ;) !

OTOH, for some reason I can't imagine FF (12) doesn't
standardly mean clear screen, and nor does any other
ASCII string.


You'll find that 'plain vanilla' *NIX / Linux systems *do* exhibit fairly
consistent console behaviour; it's the Windows-family environments that
exhibit such inconsistencies.

But I agree that having a portable means of screen clearing
would be handy.


Maybe a nested class could be added to 'System', containing all the
system-specific console-management routines, something like:

class System
{
...
public class Console
{
...
public static native void cls();
public static native void setCurPos(int row, int col);
public static native String inputString();
public static native double inputNumeric();
...
}
...
}

You could then do something like:

System.Console.cls();

to clear the screen, and something like:

String name;

System.Console.setCurPos(10, 35);
System.out.print("Enter your name: ");
name = System.Console.inputString();

Platforms that did not support consoles or the like could then simply have
dummy [i.e. empty] methods ?

Maybe if the Sun Java developers were lobbied by enough developers they
might consider building this functionality into the API ?

I suspect, though, they will simply leave it to developers requiring

console management support to use JNI, either directly, or by downloading a package like JCurses.

BTW, have the rest of you people stopped telling people
to try what you can't be bothered to try for yourself?


I think their intentions were sincere, and probably did do some testing on
their own systems. The error was in assuming that the same behaviour could
be expected on other systems. My general view is that any constructive
response is to be welcomed and appreciated.

Cheers,

Anthony Borla

Jul 17 '05 #16
It would defeat the purpose of platform-independance, since not all
platforms have the concept of a "console".

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software

"Denz" <RU*****@RUBBISHhotmail.com> wrote in message
news:Hz****************@news-server.bigpond.net.au...
Something like this Console class would be great.
Ive just downloaded JCurses but so far looks like overkill- when just a few methods like clear screen and cursor position would go such a long way...

"Anthony Borla" <aj*****@bigpond.com> wrote in message
news:yA***************@news-server.bigpond.net.au...

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:bt**********@sun-cc204.lut.ac.uk...
While it was 9/1/04 3:04 pm throughout the UK, nos sprinkled little
black dots on a white screen, and they fell thus:

> But if they can implement "beep", and they do,
> they should be able to implement "cls" too.
<snip top of upside-down reply>

That's because BEL is a standard ASCII character
(code 7) and it's pretty much standard that any terminal/console
would render it by beeping.


Try it on an IBM mainframe - they use EBCDIC, not ASCII ;) !

OTOH, for some reason I can't imagine FF (12) doesn't
standardly mean clear screen, and nor does any other
ASCII string.


You'll find that 'plain vanilla' *NIX / Linux systems *do* exhibit fairly consistent console behaviour; it's the Windows-family environments that
exhibit such inconsistencies.

But I agree that having a portable means of screen clearing
would be handy.


Maybe a nested class could be added to 'System', containing all the
system-specific console-management routines, something like:

class System
{
...
public class Console
{
...
public static native void cls();
public static native void setCurPos(int row, int col);
public static native String inputString();
public static native double inputNumeric();
...
}
...
}

You could then do something like:

System.Console.cls();

to clear the screen, and something like:

String name;

System.Console.setCurPos(10, 35);
System.out.print("Enter your name: ");
name = System.Console.inputString();

Platforms that did not support consoles or the like could then simply have dummy [i.e. empty] methods ?

Maybe if the Sun Java developers were lobbied by enough developers they
might consider building this functionality into the API ?

I suspect, though, they will simply leave it to developers requiring

console
management support to use JNI, either directly, or by downloading a

package
like JCurses.

BTW, have the rest of you people stopped telling people
to try what you can't be bothered to try for yourself?


I think their intentions were sincere, and probably did do some testing on their own systems. The error was in assuming that the same behaviour could be expected on other systems. My general view is that any constructive
response is to be welcomed and appreciated.

Cheers,

Anthony Borla


Jul 17 '05 #17

"Denz" <RU*****@RUBBISHhotmail.com> wrote in message
news:Hz****************@news-server.bigpond.net.au...

Something like this Console class would be
great. Ive just downloaded JCurses but so far looks
like overkill- when just a few methods like clear screen
and cursor position would go such a long way...


I quite like JCurses, *but* I fully agree with you - too much functionality.
Having thought about it a little more, all that is minimally required:

* Console information accessor [e.g. get max lines, columns, etc]
* A clear screen method
* Cursor position set / get methods
* A character read method i.e. accept a sing;le character
without needing to press ENTER [could be designed to
also accept 'special' keys like F1, though best, I think, to
keep it simple and ignore non-standard keys]

with other functionality such as:

* A string read method [e.g. up to X characters in
length without pressing ENTER; ESC could abort
input process; extend to not echo typed keys so
as to allow password-type input]
* Support character atributes like colour etc
* Cursor hide / unhide

would be purely optional.

It is easy enough to make a start with this by creating a Console class:

public class Console
{
public static native int getMaxRows();
public static native int getMaxCols();

public static native void cls();

public static native int getCurRow();
public static native int getCurCol();

public static native void setCurPos(int row, int col);

public static native char inputKey(int[] keyData);
}

Below you will find a JNI implementation for such a class. Since we're
dealing with JNI such code is strictly system-specific, so will work only on
the targeted platform, in this case, Win32 i.e. it should run ok on all
Win9x and up platforms.

The included files:

* A batch file called 'mkConsole.bat' used to document / generate
the steps in creating the native code

* The Java source files:

- Console.java, the Java interface to the JNI routines
- TestConsole.java, a minimal test harness for these routines

* C++ source files:

- Console.h
- ConsoleImp.cpp

Assuming you use a Win32 system [I hope so :)] you will need to provide:

* C++ Compiler capable of generating Win32 DLL's
* Suitable 'make' file for this compiler

The code was tested with Borland 5.5.1 and J2SDK 1.4.1, and appears to work
ok. The code is provided 'as is' for educational purposes. It goes without
saying that I accept no reponsibility for this code's use by others.

It may be worthwhile for anyone contemplating the use of this code to first:

* Read the JNI tutorial on the Sun site
* Consult Win32 documentation regarding 'console applications'
for some insight into the code

Sadly, the codes its provision alienates a substantial segment of the Java
community. It is hoped, though, that other may be motivated to implement
this, or similar functionality, for their own platform, and - hopefully -
post it. It would be interesting to see how these simple tasks are performed
on other platforms.

I hope this helps.

Anthony Borla

// FILE: mkConsole.bat ========================
@echo off

:: Compile JAVA Source containing Native Method calls
javac -deprecation TestConsole.java

:: Create .h header from Java Source
javah -jni TestConsole

:: Implement Native Method (C++) and create a .DLL
:: 1) Compile ConsoleImp.cpp
:: 2) Create ConsoleImp.dll
:: *** You need to implement this for your compiler ***
make -fConsoleImp.mak

:: Execute Java Program which calls Native Method
:: 1) Loads ConsoleImp.dll
:: 2) Calls 'Console' native method
java TestConsole
// FILE: Console.java ========================
public class Console
{
// Load DLL
static
{
try
{
System.loadLibrary("ConsoleImp");
}

catch (UnsatisfiedLinkError e)
{
System.out.println("Could not locate DLL");
System.exit(1);
}
}

public static native int getMaxRows();
public static native int getMaxCols();

public static native void cls();

public static native int getCurRow();
public static native int getCurCol();

public static native void setCurPos(int row, int col);

public static native char inputKey(int[] keyData);
}

// FILE: TestConsole.java =====================
public class TestConsole
{
public static void main(String[] args)
{
System.out.println("Start...");

Console.cls();

System.out.println(Console.getMaxRows());
System.out.println(Console.getMaxCols());

Console.setCurPos(10, 5);

System.out.println(Console.getCurRow());
System.out.println(Console.getCurCol());

char key;
int[] keyData = new int[]{0, 0};

System.out.print("Press a key: ");
key = Console.inputKey(keyData);

System.out.print(key + ", " + (int)key);

if (key == 0)
System.out.println("A special key was pressed");

System.out.println("Key Code: " + keyData[0]);
System.out.println("Key Scan: " + keyData[1]);

System.out.println("End...");
}
}
// FILE: Console.h ========================
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Console */

#ifndef _Included_Console
#define _Included_Console
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Console
* Method: getMaxRows
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getMaxRows
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getMaxCols
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getMaxCols
(JNIEnv *, jclass);

/*
* Class: Console
* Method: cls
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Console_cls
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getCurRow
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getCurRow
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getCurCol
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getCurCol
(JNIEnv *, jclass);

/*
* Class: Console
* Method: setCurPos
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_Console_setCurPos
(JNIEnv *, jclass, jint, jint);

/*
* Class: Console
* Method: inputKey
* Signature: ([I)C
*/
JNIEXPORT jchar JNICALL Java_Console_inputKey
(JNIEnv *, jclass, jintArray);

#ifdef __cplusplus
}
#endif
#endif

// FILE: ConsoleImp.cpp ======================
// *** Plaform-specific Header(s)
// *** Here is for Win32
#include <windows.h>
// ***

#include <jni.h>
#include "Console.h"

JNIEXPORT jint JNICALL Java_Console_getMaxRows(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwSize.Y;
}

JNIEXPORT jint JNICALL Java_Console_getMaxCols(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwSize.X;
}

JNIEXPORT void JNICALL Java_Console_cls(JNIEnv *, jclass)
{
COORD coordScreen = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO csbi;

DWORD cCharsWritten;
DWORD dwConSize;

HANDLE hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);

// Get the number of character cells in the current buffer
::GetConsoleScreenBufferInfo(hConsole, &csbi);

// Compute Console size
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

// Fill the entire screen with blanks
::FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);

// Get the current text attribute
::GetConsoleScreenBufferInfo(hConsole, &csbi);

// Set the buffer's attributes accordingly
::FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);

// Home the cursor at (0, 0)
::SetConsoleCursorPosition(hConsole, coordScreen);

return;
}

JNIEXPORT jint JNICALL Java_Console_getCurRow(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwCursorPosition.Y;
}

JNIEXPORT jint JNICALL Java_Console_getCurCol(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwCursorPosition.X;
}

JNIEXPORT void JNICALL Java_Console_setCurPos(JNIEnv* env, jclass obj, jint
row, jint col)
{
COORD coord; coord.X = col; coord.Y = row;

::SetConsoleCursorPosition(
::GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

JNIEXPORT jchar JNICALL Java_Console_inputKey(JNIEnv* env, jclass obj,
jintArray keyData)
{
const char NUL = '\0',
ESC = '\x1B';

const int TIMEOUT_MILLIS = 1000;

jsize len = env->GetArrayLength(keyData);
jint* keyData_ = env->GetIntArrayElements(keyData, 0);

DWORD omode, nmode; DWORD itemsRead = 0;
INPUT_RECORD ir;
char inputBuffer[4] = { NUL };

HANDLE hConsole = ::GetStdHandle(STD_INPUT_HANDLE);

::GetConsoleMode(hConsole, &omode);
nmode = omode & ~ENABLE_LINE_INPUT & ~ENABLE_ECHO_INPUT;
::SetConsoleMode(hConsole, nmode);

do
{
::FlushConsoleInputBuffer(hConsole);
::WaitForSingleObject(hConsole, TIMEOUT_MILLIS);
::PeekConsoleInput(hConsole, &ir, 1, &itemsRead);

if (itemsRead > 0)
{
if (ir.EventType == KEY_EVENT)
{
// Discard all non-keydown events
if (ir.Event.KeyEvent.bKeyDown == FALSE)
{
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
continue;
}

// Got ASCII key, so extract, flush, and exit
if (ir.Event.KeyEvent.uChar.AsciiChar != NUL)
{
if (ir.Event.KeyEvent.uChar.AsciiChar == ESC)
{
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
inputBuffer[0] = ESC;
}
else
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);

keyData_[0] = ir.Event.KeyEvent.wVirtualKeyCode;
keyData_[1] = ir.Event.KeyEvent.wVirtualScanCode;

break;
}

// Got 'special' key, so determine appropriate actions
switch (ir.Event.KeyEvent.dwControlKeyState)
{
case ENHANCED_KEY:
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
break;

case LEFT_ALT_PRESSED: case LEFT_CTRL_PRESSED:
case RIGHT_ALT_PRESSED: case RIGHT_CTRL_PRESSED:
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);
inputBuffer[0] = NUL;
break;

case SHIFT_PRESSED:
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);
break;
}

keyData_[0] = ir.Event.KeyEvent.wVirtualKeyCode;
keyData_[1] = ir.Event.KeyEvent.wVirtualScanCode;
}
else
{
// Not Key Event, so just extract and discard
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
}
}

} while (itemsRead == 0);

::FlushConsoleInputBuffer(hConsole);
::SetConsoleMode(hConsole, omode);

env->ReleaseIntArrayElements(keyData, keyData_, 0);

return inputBuffer[0];
}

Jul 17 '05 #18
Can anyone help me with this one? I am looking for a makefile to fill
in for the one mentioned in the previous posting for so I can compile
this all myself. I have the Borland v5.5 command line tools. I have no
clue where to go here. I tried skipping the 'makefile' idea and tried
using following syntax and it compiled but I don't think it compiled
the way it supposed to (windows freaked when I tried to use the DLL)

cpp32 -I\j2sdk1.4.2_03\include -I\j2sdk1.4.2_03\include\win
32 -I\Borland\BCC55\Include -oConsoleImp.dll ConsoleImp.cpp

So I am assuming I am missing a lot here. Any help would be
appreciated.

Russ

"Anthony Borla" <aj*****@bigpond.com> wrote in message news:<UO****************@news-server.bigpond.net.au>...
"Denz" <RU*****@RUBBISHhotmail.com> wrote in message
news:Hz****************@news-server.bigpond.net.au...

Something like this Console class would be
great. Ive just downloaded JCurses but so far looks
like overkill- when just a few methods like clear screen
and cursor position would go such a long way...


I quite like JCurses, *but* I fully agree with you - too much functionality.
Having thought about it a little more, all that is minimally required:

* Console information accessor [e.g. get max lines, columns, etc]
* A clear screen method
* Cursor position set / get methods
* A character read method i.e. accept a sing;le character
without needing to press ENTER [could be designed to
also accept 'special' keys like F1, though best, I think, to
keep it simple and ignore non-standard keys]

with other functionality such as:

* A string read method [e.g. up to X characters in
length without pressing ENTER; ESC could abort
input process; extend to not echo typed keys so
as to allow password-type input]
* Support character atributes like colour etc
* Cursor hide / unhide

would be purely optional.

It is easy enough to make a start with this by creating a Console class:

public class Console
{
public static native int getMaxRows();
public static native int getMaxCols();

public static native void cls();

public static native int getCurRow();
public static native int getCurCol();

public static native void setCurPos(int row, int col);

public static native char inputKey(int[] keyData);
}

Below you will find a JNI implementation for such a class. Since we're
dealing with JNI such code is strictly system-specific, so will work only on
the targeted platform, in this case, Win32 i.e. it should run ok on all
Win9x and up platforms.

The included files:

* A batch file called 'mkConsole.bat' used to document / generate
the steps in creating the native code

* The Java source files:

- Console.java, the Java interface to the JNI routines
- TestConsole.java, a minimal test harness for these routines

* C++ source files:

- Console.h
- ConsoleImp.cpp

Assuming you use a Win32 system [I hope so :)] you will need to provide:

* C++ Compiler capable of generating Win32 DLL's
* Suitable 'make' file for this compiler

The code was tested with Borland 5.5.1 and J2SDK 1.4.1, and appears to work
ok. The code is provided 'as is' for educational purposes. It goes without
saying that I accept no reponsibility for this code's use by others.

It may be worthwhile for anyone contemplating the use of this code to first:

* Read the JNI tutorial on the Sun site
* Consult Win32 documentation regarding 'console applications'
for some insight into the code

Sadly, the codes its provision alienates a substantial segment of the Java
community. It is hoped, though, that other may be motivated to implement
this, or similar functionality, for their own platform, and - hopefully -
post it. It would be interesting to see how these simple tasks are performed
on other platforms.

I hope this helps.

Anthony Borla

// FILE: mkConsole.bat ========================
@echo off

:: Compile JAVA Source containing Native Method calls
javac -deprecation TestConsole.java

:: Create .h header from Java Source
javah -jni TestConsole

:: Implement Native Method (C++) and create a .DLL
:: 1) Compile ConsoleImp.cpp
:: 2) Create ConsoleImp.dll
:: *** You need to implement this for your compiler ***
make -fConsoleImp.mak

:: Execute Java Program which calls Native Method
:: 1) Loads ConsoleImp.dll
:: 2) Calls 'Console' native method
java TestConsole
// FILE: Console.java ========================
public class Console
{
// Load DLL
static
{
try
{
System.loadLibrary("ConsoleImp");
}

catch (UnsatisfiedLinkError e)
{
System.out.println("Could not locate DLL");
System.exit(1);
}
}

public static native int getMaxRows();
public static native int getMaxCols();

public static native void cls();

public static native int getCurRow();
public static native int getCurCol();

public static native void setCurPos(int row, int col);

public static native char inputKey(int[] keyData);
}

// FILE: TestConsole.java =====================
public class TestConsole
{
public static void main(String[] args)
{
System.out.println("Start...");

Console.cls();

System.out.println(Console.getMaxRows());
System.out.println(Console.getMaxCols());

Console.setCurPos(10, 5);

System.out.println(Console.getCurRow());
System.out.println(Console.getCurCol());

char key;
int[] keyData = new int[]{0, 0};

System.out.print("Press a key: ");
key = Console.inputKey(keyData);

System.out.print(key + ", " + (int)key);

if (key == 0)
System.out.println("A special key was pressed");

System.out.println("Key Code: " + keyData[0]);
System.out.println("Key Scan: " + keyData[1]);

System.out.println("End...");
}
}
// FILE: Console.h ========================
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Console */

#ifndef _Included_Console
#define _Included_Console
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Console
* Method: getMaxRows
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getMaxRows
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getMaxCols
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getMaxCols
(JNIEnv *, jclass);

/*
* Class: Console
* Method: cls
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Console_cls
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getCurRow
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getCurRow
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getCurCol
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getCurCol
(JNIEnv *, jclass);

/*
* Class: Console
* Method: setCurPos
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_Console_setCurPos
(JNIEnv *, jclass, jint, jint);

/*
* Class: Console
* Method: inputKey
* Signature: ([I)C
*/
JNIEXPORT jchar JNICALL Java_Console_inputKey
(JNIEnv *, jclass, jintArray);

#ifdef __cplusplus
}
#endif
#endif

// FILE: ConsoleImp.cpp ======================
// *** Plaform-specific Header(s)
// *** Here is for Win32
#include <windows.h>
// ***

#include <jni.h>
#include "Console.h"

JNIEXPORT jint JNICALL Java_Console_getMaxRows(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwSize.Y;
}

JNIEXPORT jint JNICALL Java_Console_getMaxCols(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwSize.X;
}

JNIEXPORT void JNICALL Java_Console_cls(JNIEnv *, jclass)
{
COORD coordScreen = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO csbi;

DWORD cCharsWritten;
DWORD dwConSize;

HANDLE hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);

// Get the number of character cells in the current buffer
::GetConsoleScreenBufferInfo(hConsole, &csbi);

// Compute Console size
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

// Fill the entire screen with blanks
::FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);

// Get the current text attribute
::GetConsoleScreenBufferInfo(hConsole, &csbi);

// Set the buffer's attributes accordingly
::FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);

// Home the cursor at (0, 0)
::SetConsoleCursorPosition(hConsole, coordScreen);

return;
}

JNIEXPORT jint JNICALL Java_Console_getCurRow(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwCursorPosition.Y;
}

JNIEXPORT jint JNICALL Java_Console_getCurCol(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwCursorPosition.X;
}

JNIEXPORT void JNICALL Java_Console_setCurPos(JNIEnv* env, jclass obj, jint
row, jint col)
{
COORD coord; coord.X = col; coord.Y = row;

::SetConsoleCursorPosition(
::GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

JNIEXPORT jchar JNICALL Java_Console_inputKey(JNIEnv* env, jclass obj,
jintArray keyData)
{
const char NUL = '\0',
ESC = '\x1B';

const int TIMEOUT_MILLIS = 1000;

jsize len = env->GetArrayLength(keyData);
jint* keyData_ = env->GetIntArrayElements(keyData, 0);

DWORD omode, nmode; DWORD itemsRead = 0;
INPUT_RECORD ir;
char inputBuffer[4] = { NUL };

HANDLE hConsole = ::GetStdHandle(STD_INPUT_HANDLE);

::GetConsoleMode(hConsole, &omode);
nmode = omode & ~ENABLE_LINE_INPUT & ~ENABLE_ECHO_INPUT;
::SetConsoleMode(hConsole, nmode);

do
{
::FlushConsoleInputBuffer(hConsole);
::WaitForSingleObject(hConsole, TIMEOUT_MILLIS);
::PeekConsoleInput(hConsole, &ir, 1, &itemsRead);

if (itemsRead > 0)
{
if (ir.EventType == KEY_EVENT)
{
// Discard all non-keydown events
if (ir.Event.KeyEvent.bKeyDown == FALSE)
{
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
continue;
}

// Got ASCII key, so extract, flush, and exit
if (ir.Event.KeyEvent.uChar.AsciiChar != NUL)
{
if (ir.Event.KeyEvent.uChar.AsciiChar == ESC)
{
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
inputBuffer[0] = ESC;
}
else
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);

keyData_[0] = ir.Event.KeyEvent.wVirtualKeyCode;
keyData_[1] = ir.Event.KeyEvent.wVirtualScanCode;

break;
}

// Got 'special' key, so determine appropriate actions
switch (ir.Event.KeyEvent.dwControlKeyState)
{
case ENHANCED_KEY:
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
break;

case LEFT_ALT_PRESSED: case LEFT_CTRL_PRESSED:
case RIGHT_ALT_PRESSED: case RIGHT_CTRL_PRESSED:
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);
inputBuffer[0] = NUL;
break;

case SHIFT_PRESSED:
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);
break;
}

keyData_[0] = ir.Event.KeyEvent.wVirtualKeyCode;
keyData_[1] = ir.Event.KeyEvent.wVirtualScanCode;
}
else
{
// Not Key Event, so just extract and discard
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
}
}

} while (itemsRead == 0);

::FlushConsoleInputBuffer(hConsole);
::SetConsoleMode(hConsole, omode);

env->ReleaseIntArrayElements(keyData, keyData_, 0);

return inputBuffer[0];
}

Jul 17 '05 #19
I know from some experience with Linux, the native code you are making into
your library must be position independent.
So when you are making your shared library you need to compile it with the
correct switches.

On Linux you would do something of this fashion
http://www.javaworld.com/javaworld/j...javatip23.html
Look in the "Create a shared library" section of this article.

For Linux this is how it works
http://www.tldp.org/HOWTO/Program-Li...libraries.html

If the library is compiled without being position independent then when you
run it, strange things may happen or it may crap out completely.

Roman.

"Russell Allen" <wh*********@hotmail.com> wrote in message
news:2c************************@posting.google.com ...
Can anyone help me with this one? I am looking for a makefile to fill
in for the one mentioned in the previous posting for so I can compile
this all myself. I have the Borland v5.5 command line tools. I have no
clue where to go here. I tried skipping the 'makefile' idea and tried
using following syntax and it compiled but I don't think it compiled
the way it supposed to (windows freaked when I tried to use the DLL)

cpp32 -I\j2sdk1.4.2_03\include -I\j2sdk1.4.2_03\include\win
32 -I\Borland\BCC55\Include -oConsoleImp.dll ConsoleImp.cpp

So I am assuming I am missing a lot here. Any help would be
appreciated.

Russ

"Anthony Borla" <aj*****@bigpond.com> wrote in message

news:<UO****************@news-server.bigpond.net.au>...
"Denz" <RU*****@RUBBISHhotmail.com> wrote in message
news:Hz****************@news-server.bigpond.net.au...

Something like this Console class would be
great. Ive just downloaded JCurses but so far looks
like overkill- when just a few methods like clear screen
and cursor position would go such a long way...


I quite like JCurses, *but* I fully agree with you - too much functionality. Having thought about it a little more, all that is minimally required:

* Console information accessor [e.g. get max lines, columns, etc]
* A clear screen method
* Cursor position set / get methods
* A character read method i.e. accept a sing;le character
without needing to press ENTER [could be designed to
also accept 'special' keys like F1, though best, I think, to
keep it simple and ignore non-standard keys]

with other functionality such as:

* A string read method [e.g. up to X characters in
length without pressing ENTER; ESC could abort
input process; extend to not echo typed keys so
as to allow password-type input]
* Support character atributes like colour etc
* Cursor hide / unhide

would be purely optional.

It is easy enough to make a start with this by creating a Console class:

public class Console
{
public static native int getMaxRows();
public static native int getMaxCols();

public static native void cls();

public static native int getCurRow();
public static native int getCurCol();

public static native void setCurPos(int row, int col);

public static native char inputKey(int[] keyData);
}

Below you will find a JNI implementation for such a class. Since we're
dealing with JNI such code is strictly system-specific, so will work only on the targeted platform, in this case, Win32 i.e. it should run ok on all
Win9x and up platforms.

The included files:

* A batch file called 'mkConsole.bat' used to document / generate
the steps in creating the native code

* The Java source files:

- Console.java, the Java interface to the JNI routines
- TestConsole.java, a minimal test harness for these routines

* C++ source files:

- Console.h
- ConsoleImp.cpp

Assuming you use a Win32 system [I hope so :)] you will need to provide:

* C++ Compiler capable of generating Win32 DLL's
* Suitable 'make' file for this compiler

The code was tested with Borland 5.5.1 and J2SDK 1.4.1, and appears to work ok. The code is provided 'as is' for educational purposes. It goes without saying that I accept no reponsibility for this code's use by others.

It may be worthwhile for anyone contemplating the use of this code to first:
* Read the JNI tutorial on the Sun site
* Consult Win32 documentation regarding 'console applications'
for some insight into the code

Sadly, the codes its provision alienates a substantial segment of the Java community. It is hoped, though, that other may be motivated to implement
this, or similar functionality, for their own platform, and - hopefully - post it. It would be interesting to see how these simple tasks are performed on other platforms.

I hope this helps.

Anthony Borla

// FILE: mkConsole.bat ========================
@echo off

:: Compile JAVA Source containing Native Method calls
javac -deprecation TestConsole.java

:: Create .h header from Java Source
javah -jni TestConsole

:: Implement Native Method (C++) and create a .DLL
:: 1) Compile ConsoleImp.cpp
:: 2) Create ConsoleImp.dll
:: *** You need to implement this for your compiler ***
make -fConsoleImp.mak

:: Execute Java Program which calls Native Method
:: 1) Loads ConsoleImp.dll
:: 2) Calls 'Console' native method
java TestConsole
// FILE: Console.java ========================
public class Console
{
// Load DLL
static
{
try
{
System.loadLibrary("ConsoleImp");
}

catch (UnsatisfiedLinkError e)
{
System.out.println("Could not locate DLL");
System.exit(1);
}
}

public static native int getMaxRows();
public static native int getMaxCols();

public static native void cls();

public static native int getCurRow();
public static native int getCurCol();

public static native void setCurPos(int row, int col);

public static native char inputKey(int[] keyData);
}

// FILE: TestConsole.java =====================
public class TestConsole
{
public static void main(String[] args)
{
System.out.println("Start...");

Console.cls();

System.out.println(Console.getMaxRows());
System.out.println(Console.getMaxCols());

Console.setCurPos(10, 5);

System.out.println(Console.getCurRow());
System.out.println(Console.getCurCol());

char key;
int[] keyData = new int[]{0, 0};

System.out.print("Press a key: ");
key = Console.inputKey(keyData);

System.out.print(key + ", " + (int)key);

if (key == 0)
System.out.println("A special key was pressed");

System.out.println("Key Code: " + keyData[0]);
System.out.println("Key Scan: " + keyData[1]);

System.out.println("End...");
}
}
// FILE: Console.h ========================
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Console */

#ifndef _Included_Console
#define _Included_Console
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Console
* Method: getMaxRows
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getMaxRows
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getMaxCols
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getMaxCols
(JNIEnv *, jclass);

/*
* Class: Console
* Method: cls
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Console_cls
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getCurRow
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getCurRow
(JNIEnv *, jclass);

/*
* Class: Console
* Method: getCurCol
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Console_getCurCol
(JNIEnv *, jclass);

/*
* Class: Console
* Method: setCurPos
* Signature: (II)V
*/
JNIEXPORT void JNICALL Java_Console_setCurPos
(JNIEnv *, jclass, jint, jint);

/*
* Class: Console
* Method: inputKey
* Signature: ([I)C
*/
JNIEXPORT jchar JNICALL Java_Console_inputKey
(JNIEnv *, jclass, jintArray);

#ifdef __cplusplus
}
#endif
#endif

// FILE: ConsoleImp.cpp ======================
// *** Plaform-specific Header(s)
// *** Here is for Win32
#include <windows.h>
// ***

#include <jni.h>
#include "Console.h"

JNIEXPORT jint JNICALL Java_Console_getMaxRows(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwSize.Y;
}

JNIEXPORT jint JNICALL Java_Console_getMaxCols(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwSize.X;
}

JNIEXPORT void JNICALL Java_Console_cls(JNIEnv *, jclass)
{
COORD coordScreen = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO csbi;

DWORD cCharsWritten;
DWORD dwConSize;

HANDLE hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);

// Get the number of character cells in the current buffer
::GetConsoleScreenBufferInfo(hConsole, &csbi);

// Compute Console size
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

// Fill the entire screen with blanks
::FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);

// Get the current text attribute
::GetConsoleScreenBufferInfo(hConsole, &csbi);

// Set the buffer's attributes accordingly
::FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);

// Home the cursor at (0, 0)
::SetConsoleCursorPosition(hConsole, coordScreen);

return;
}

JNIEXPORT jint JNICALL Java_Console_getCurRow(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwCursorPosition.Y;
}

JNIEXPORT jint JNICALL Java_Console_getCurCol(JNIEnv *, jclass)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;

::GetConsoleScreenBufferInfo(
::GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

return csbi.dwCursorPosition.X;
}

JNIEXPORT void JNICALL Java_Console_setCurPos(JNIEnv* env, jclass obj, jint row, jint col)
{
COORD coord; coord.X = col; coord.Y = row;

::SetConsoleCursorPosition(
::GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

JNIEXPORT jchar JNICALL Java_Console_inputKey(JNIEnv* env, jclass obj,
jintArray keyData)
{
const char NUL = '\0',
ESC = '\x1B';

const int TIMEOUT_MILLIS = 1000;

jsize len = env->GetArrayLength(keyData);
jint* keyData_ = env->GetIntArrayElements(keyData, 0);

DWORD omode, nmode; DWORD itemsRead = 0;
INPUT_RECORD ir;
char inputBuffer[4] = { NUL };

HANDLE hConsole = ::GetStdHandle(STD_INPUT_HANDLE);

::GetConsoleMode(hConsole, &omode);
nmode = omode & ~ENABLE_LINE_INPUT & ~ENABLE_ECHO_INPUT;
::SetConsoleMode(hConsole, nmode);

do
{
::FlushConsoleInputBuffer(hConsole);
::WaitForSingleObject(hConsole, TIMEOUT_MILLIS);
::PeekConsoleInput(hConsole, &ir, 1, &itemsRead);

if (itemsRead > 0)
{
if (ir.EventType == KEY_EVENT)
{
// Discard all non-keydown events
if (ir.Event.KeyEvent.bKeyDown == FALSE)
{
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
continue;
}

// Got ASCII key, so extract, flush, and exit
if (ir.Event.KeyEvent.uChar.AsciiChar != NUL)
{
if (ir.Event.KeyEvent.uChar.AsciiChar == ESC)
{
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
inputBuffer[0] = ESC;
}
else
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);

keyData_[0] = ir.Event.KeyEvent.wVirtualKeyCode;
keyData_[1] = ir.Event.KeyEvent.wVirtualScanCode;

break;
}

// Got 'special' key, so determine appropriate actions
switch (ir.Event.KeyEvent.dwControlKeyState)
{
case ENHANCED_KEY:
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
break;

case LEFT_ALT_PRESSED: case LEFT_CTRL_PRESSED:
case RIGHT_ALT_PRESSED: case RIGHT_CTRL_PRESSED:
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);
inputBuffer[0] = NUL;
break;

case SHIFT_PRESSED:
::ReadFile(hConsole, inputBuffer, 1, &itemsRead, NULL);
break;
}

keyData_[0] = ir.Event.KeyEvent.wVirtualKeyCode;
keyData_[1] = ir.Event.KeyEvent.wVirtualScanCode;
}
else
{
// Not Key Event, so just extract and discard
::ReadConsoleInput(hConsole, &ir, 1, &itemsRead);
}
}

} while (itemsRead == 0);

::FlushConsoleInputBuffer(hConsole);
::SetConsoleMode(hConsole, omode);

env->ReleaseIntArrayElements(keyData, keyData_, 0);

return inputBuffer[0];
}

Jul 17 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: NeoPhreak | last post by:
How would you clearn the screen in a console program??? Thanks NeoPhreak >.<
0
by: Peter | last post by:
I am just trying to learn python, to use for some fairly basic command line utilities which will run in either Windows/DOS or Linux. I cannot find a platform neutral way of manipulating the screen....
20
by: ritchie | last post by:
Hi, I am trying to clear the screen in my program. I am loking for something that will work on all compilers, especially Borland & MS Visual Studio 6. On Visual studio I used 'system("cls");'...
4
by: John | last post by:
Hi I have the following in a Perl script. I just want to "clear screen" but it does not work. I don't think I've got my client and server sides confused. Any ideas? <SCRIPT...
8
by: REDBAIT | last post by:
Hi Guys, Am using Windows 2000 and Dev C++ to create C programs. I'm trying to use clear screen and other graphic functions such as positioning output on screen. Have seen some documents that...
0
by: sajithamol | last post by:
I have an Applet that has three buttons on it, one being a clear screen button. I have it setup right now so it draws a rectangle the size of the applet and fills it with the background color. But...
2
by: owl | last post by:
and here I thought I was going to finally be able to change the world AND contribute back to python with my amazing clear screen extension - but I can't get it to work. ;( Copying from...
9
by: pbd22 | last post by:
Hi. Does anybody know how to issue a clear screen command like DOS "cls" in a telnet session? Thanks!
5
by: Kid Programmer | last post by:
Hello guys. I was wondering how you can clear the screen in a java program. Here is the code for my program: import javax.swing.SwingUtilities; import javax.swing.JFrame; import...
1
by: sganeshsvk | last post by:
i want to clear the screen command prompt for mysql in windows XP... In linux we use (CTRL + L) for clear the screen in mysql prompt.... i already use this command ( \c ) then ( \! clear ) for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.