Clear the console, set color and cursor position (JNI)Tag(s): JNI


[Windows only]
First you need this Java stub (JavaHowTo.java) to provide an interface to the JNI DLL (jni3.dll).
class JavaHowTo {

  public static final short FOREGROUND_BLACK = 0x0;
  public static final short FOREGROUND_BLUE  = 0x1;
  public static final short FOREGROUND_GREEN = 0x2;
  public static final short FOREGROUND_RED   = 0x4;
  public static final short FOREGROUND_WHITE = 0x7;
  public static final short FOREGROUND_INTENSITY = 0x8;

  public static final short BACKGROUND_BLUE  = 0x10;
  public static final short BACKGROUND_GREEN = 0x20;
  public static final short BACKGROUND_RED   = 0x40;
  public static final short BACKGROUND_INTENSITY = 0x80;
  // and so on...the definition for the other colors is
  // left as an exercise :-)


  public native void cls();
  public native void setCursorPosition( short x, short y);
  public native void keepColors();
  public native void restoreColors();
  public native void setColor( short foreground, short background);
  static {
    System.loadLibrary("jni3");
  }
}
Compile and generate an header with javah JavaHowto, the result is a file called JavaHowTo.h.

Next we built a DLL, I'm using VisualStudio v6. Don't forget to include the folders %JAVAHOME%\include and %JAVAHOME%\include\win32 to have access to the JNI header files.

// jni3.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <stdlib.h>
#include "JavaHowTo.h"


int originalColors;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}


JNIEXPORT void JNICALL Java_JavaHowTo_cls
  (JNIEnv *env, jobject obj) {
    
    HANDLE hConsole;
    unsigned long * hWrittenChars = 0;
    CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo;
    COORD Home;
    static unsigned char EMPTY = 32;

    Home.X = 0;
    Home.Y = 0;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &strConsoleInfo);
    FillConsoleOutputCharacter(hConsole, EMPTY, 
          strConsoleInfo.dwSize.X * strConsoleInfo.dwSize.X, Home, 
          hWrittenChars);
    SetConsoleCursorPosition(hConsole, Home);
    // system("cls");  will do the same as the above!
}

JNIEXPORT void JNICALL Java_JavaHowTo_setCursorPosition
(JNIEnv *env, jobject obj, jshort x, jshort y) {

    HANDLE hConsole;
    COORD coordScreen;  

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    coordScreen.X = x;
    coordScreen.Y = y;
    SetConsoleCursorPosition( hConsole, coordScreen );
 
}

JNIEXPORT void JNICALL Java_JavaHowTo_setColor
(JNIEnv *env, jobject obj, jshort foreground, jshort background) {
    HANDLE hConsole;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, foreground + background);
}

JNIEXPORT void JNICALL Java_JavaHowTo_keepColors
(JNIEnv *env, jobject obj) {
    HANDLE hConsole;
    CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo);
    originalColors = ConsoleInfo.wAttributes;
}

JNIEXPORT void JNICALL Java_JavaHowTo_restoreColors
(JNIEnv *env, jobject obj) {
    HANDLE hConsole;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, originalColors);
}

Build the DLL (target Release), the result is jni3.dll.

Here an example how to use the DLL.

public class JNIJavaHowTo {
  public static void main(String[] args) {
    JavaHowTo jht = new JavaHowTo();
    
    // clear the screen
    jht.cls();
    
    // set the cursor at line 10 column 20
    jht.setCursorPosition((short)20,(short)10);
    System.out.print("Real's HowTo");
    
    // set the cursor at line 15 column 20
    jht.setCursorPosition((short)20,(short)15);
    
    // keep the current colors
    jht.keepColors();
    
    // set the color as WHITE on BLUE
    jht.setColor(jht.FOREGROUND_WHITE,jht.BACKGROUND_BLUE);
    
    System.out.print("http://www.rgagnon.com");
    
    // restore the orginal colors
    jht.restoreColors();
    
    // set the cursor at line 20 column 0
    jht.setCursorPosition((short)0,(short)20);
    }
}
You can download a zip with everything here.
blog comments powered by Disqus