Console Graphics

Discussion in 'Java' started by Robotnik, May 10, 2004.

  1. Robotnik

    Robotnik Guest

    Does the JAVA language have any classes built in for doing graphics on the
    console? Or if not graphics, at the very least changing foreground and/or
    background colors of
    characters....as well as some cursur positioning methods?

    Thanks,

    Nic
     
    Robotnik, May 10, 2004
    #1
    1. Advertising

  2. Robotnik wrote:

    > Does the JAVA language have any classes built in for doing graphics on the
    > console? Or if not graphics, at the very least changing foreground and/or
    > background colors of
    > characters....as well as some cursur positioning methods?
    >


    It's called Java, not JAVA :)

    Anyway, the JRE doesn't ship with any such classes.

    You'll need to look at the various curses bindings for Java:
    http://www.javaworld.com/javaworld/javaqa/2002-12/02-qa-1220-console.html

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, May 10, 2004
    #2
    1. Advertising

  3. Robotnik schrieb:
    > Does the JAVA language have any classes built in for doing graphics on the
    > console? Or if not graphics, at the very least changing foreground and/or
    > background colors of
    > characters....as well as some cursur positioning methods?


    Short answer: No, there aren't any classes/methods that would support
    directly what you want.

    Bye
    Michael
     
    Michael Rauscher, May 10, 2004
    #3
  4. Robotnik

    Roedy Green Guest

    On Mon, 10 May 2004 19:55:05 +0200, Michael Rauscher
    <> wrote or quoted :

    >
    >Short answer: No, there aren't any classes/methods that would support
    >directly what you want.


    if there were, you would need to get an ANSI.SYS driver installed on
    the console, which I don't think is easy to do anymore. Windows is
    phasing out the DOS box and drops capability. Then you feed it ansi
    escape sequences, just like the old DOS days.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 10, 2004
    #4
  5. Robotnik

    Robotnik Guest

    I could probably do that, because for this project I planned on running the
    JVM in a Windows 98 SE DOS mode. The only thing is I would probably be
    developing it on a windows 2000 sp4 machine.

    Installing an ANSI.SYS on that is simple as adding a line to AUTOEXEC.BAT,
    I'm pretty sure.

    I'll give the escape sequence stuff a try. I think I've done it once before
    in C... but I"m not for sure.

    Thanks again,

    Nic

    "Roedy Green" <> wrote in message
    news:...
    > On Mon, 10 May 2004 19:55:05 +0200, Michael Rauscher
    > <> wrote or quoted :
    >
    > >
    > >Short answer: No, there aren't any classes/methods that would support
    > >directly what you want.

    >
    > if there were, you would need to get an ANSI.SYS driver installed on
    > the console, which I don't think is easy to do anymore. Windows is
    > phasing out the DOS box and drops capability. Then you feed it ansi
    > escape sequences, just like the old DOS days.
    >
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Robotnik, May 11, 2004
    #5
  6. Robotnik

    Real Gagnon Guest

    > I could probably do that, because for this project I planned on
    > running the JVM in a Windows 98 SE DOS mode. The only thing is I
    > would probably be developing it on a windows 2000 sp4 machine.
    >
    > Installing an ANSI.SYS on that is simple as adding a line to
    > AUTOEXEC.BAT, I'm pretty sure.


    ANSI.SYS is ok on Win98 but won't work on NT (or better) installation.

    You may want to consider a JNI solution.

    ANSI.SYS
    http://www.rgagnon.com/javadetails/java-0047.html

    JNI Windows
    http://www.rgagnon.com/javadetails/java-0469.html

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB snippets ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
     
    Real Gagnon, May 11, 2004
    #6
  7. Robotnik

    Guest

    Peace be unto you.

    The following code runs nicely on my machine (Windows Me).
    Screenshot http://members.rogers.com/mromarkhan/jconio/shot.gif
    Dll http://members.rogers.com/mromarkhan/jconio/JConioLib.dll

    I compiled it using MingW which includes Conio.c and Conio.h.


    -- QuranBrowser.java
    <code>
    class QuranBrowser
    {
    public static void main(String[] args)
    {
    JConioLib conio = new JConioLib();
    conio.setConsoleTitle("Quran Browser"); // rename title from Java
    conio.textbackground(JConioLib.WHITE); // white background
    conio.textcolor(JConioLib.BLACK); // black text
    conio.clrscr(); // clear screen
    char c;
    int recordPointer = 0;
    String [] records = {
    "I seek Allah's protection from",
    "Satan the outcast",
    "In the name of Allah, the Beneficent, the Merciful",
    "Praise be to Allah, Lord of the Worlds",
    "The Beneficent, the Merciful",
    "Owner of the Day of Judgement",
    "You we worship, You we ask for help",
    "Show us the straight path",
    "The path of those whom you have favoured",
    "Not of those who earn you anger",
    "Nor of those who go astray",
    "Accept the prayer"
    };
    conio.clrscr();
    conio.gotoxy(1,14);
    System.out.println("Press q to quit");

    while(true)
    {
    conio.gotoxy(1,1);
    System.out.println("Press any key to continue");
    c = (char)conio.getch();
    recordPointer++;
    if(c == 'q' || c == 'x')
    {
    conio.textbackground(JConioLib.RED);
    conio.textcolor(JConioLib.WHITE);
    conio.gotoxy(1,14);
    System.out.println("Press any key to quit");
    conio.getch();
    break;
    }
    if(recordPointer >= records.length)
    {
    recordPointer = 0;
    }
    else if(recordPointer < 0)
    {
    recordPointer = records.length-1;
    }
    for(int i = 0;i<records.length;i++)
    {
    conio.gotoxy(1,i+2);
    if(i == recordPointer)
    {
    conio.textbackground(JConioLib.GREEN);
    conio.textcolor(JConioLib.WHITE);
    }
    else
    {
    conio.textbackground(JConioLib.WHITE);
    conio.textcolor(JConioLib.BLACK);
    }
    System.out.println(records);
    }

    }

    }
    }
    </code>
    <code>
    -- JConioLib.java
    public class JConioLib
    {
    public static final int BLACK = 0;
    public static final int BLUE = 1;
    public static final int GREEN = 2;
    public static final int CYAN = 3;
    public static final int RED = 4;
    public static final int MAGENTA = 5;
    public static final int BROWN = 6;
    public static final int LIGHTGRAY = 7;
    public static final int DARKGRAY = 8;
    public static final int LIGHTBLUE = 9;
    public static final int LIGHTGREEN = 10;
    public static final int LIGHTCYAN = 11;
    public static final int LIGHTRED = 12;
    public static final int LIGHTMAGENTA = 13;
    public static final int YELLOW = 14;
    public static final int WHITE = 15;
    //void _setcursortype (int type); 1 -100
    //textattr (int _attr)
    public native String conio_gettext(int left, int top, int right, int bottom);
    public native int wherex();
    public native int wherey();
    public native void delline ();
    public native void clreol ();
    public native void puttext (int left, int top, int right, int bottom, String s);
    public native int getche ();
    public native int kbhit ();
    public native int putch (int ch);
    public native int ungetch (int ch);
    public native int getch();
    public native void clrscr();
    public native void textcolor(int color);
    public native void textbackground (int color);
    public native void gotoxy(int x, int y);
    public native void setConsoleTitle(String title);
    static
    {
    System.loadLibrary("JConioLib");
    }
    }
    </code>

    -- JConioLib.c
    <code>
    #include <stdio.h>
    #include <stdlib.h>
    #include <jni.h>
    #include "JConioLib.h"
    #include "conio.h"
    #include <windows.h>

    //extern void clrscr(void);

    /*
    * Class: JConioLib
    * Method: getch
    * Signature: ()I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_getch
    (JNIEnv * env, jobject obj)
    {
    int c = getch();
    return (jint) c;
    }

    /*
    * Class: JConioLib
    * Method: clrscr
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_JConioLib_clrscr
    (JNIEnv * env, jobject obj)
    {
    clrscr();
    }

    /*
    * Class: JConioLib
    * Method: textcolor
    * Signature: (I)V
    */
    JNIEXPORT void JNICALL Java_JConioLib_textcolor
    (JNIEnv * env, jobject obj,jint color)
    {
    textcolor(color);
    }

    /*
    * Class: JConioLib
    * Method: textbackground
    * Signature: (I)V
    */
    JNIEXPORT void JNICALL Java_JConioLib_textbackground
    (JNIEnv * env, jobject obj,jint color)
    {
    textbackground(color);
    }

    /*
    * Class: JConioLib
    * Method: puttext
    * Signature: (IIIILjava/lang/String;)V
    *
    */
    JNIEXPORT void JNICALL Java_JConioLib_puttext
    (JNIEnv * env, jobject obj, jint left, jint top, jint right, jint bottom, jstring prompt)
    {
    char buf[128];
    const char *str = (*env)->GetStringUTFChars(env, prompt, 0);
    puttext(left,top,right,bottom,str);
    (*env)->ReleaseStringUTFChars(env, prompt, str);
    }

    /*
    * Class: JConioLib
    * Method: wherex
    * Signature: ()I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_wherex
    (JNIEnv * env, jobject obj)
    {
    return (jint) wherex();
    }

    /*
    * Class: JConioLib
    * Method: wherey
    * Signature: ()I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_wherey
    (JNIEnv * env, jobject obj)
    {
    return (jint) wherey();
    }

    /*
    * Class: JConioLib
    * Method: delline
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_JConioLib_delline
    (JNIEnv * env, jobject obj)
    {
    delline();
    }

    /*
    * Class: JConioLib
    * Method: clreol
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_JConioLib_clreol
    (JNIEnv * env, jobject obj)
    {
    clreol();
    }

    /*
    * Class: JConioLib
    * Method: getche
    * Signature: ()I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_getche
    (JNIEnv * env, jobject obj)
    {
    (jint) getche();
    }

    /*
    * Class: JConioLib
    * Method: kbhit
    * Signature: ()I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_kbhit
    (JNIEnv * env, jobject obj)
    {
    return (jint)kbhit();
    }

    /*
    * Class: JConioLib
    * Method: putch
    * Signature: (I)I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_putch
    (JNIEnv * env, jobject obj, jint ch)
    {
    return (jint) putch(ch);
    }

    /*
    * Class: JConioLib
    * Method: ungetch
    * Signature: (I)I
    */
    JNIEXPORT jint JNICALL Java_JConioLib_ungetch
    (JNIEnv * env, jobject obj, jint ch)
    {
    return (jint) ungetch(ch);
    }


    /*
    * Class: JConioLib
    * Method: gotoxy
    * Signature: (II)V
    */
    JNIEXPORT void JNICALL Java_JConioLib_gotoxy
    (JNIEnv * env, jobject obj, jint x, jint y)
    {
    gotoxy(x,y);
    }


    /*
    * Class: JConioLib
    * Method: conio_gettext
    * Signature: (IIII)Ljava/lang/String;
    */
    JNIEXPORT jstring JNICALL Java_JConioLib_conio_1gettext
    (JNIEnv * env, jobject obj, jint left, jint top, jint right, jint bottom)
    {
    char buf[1928];
    _conio_gettext(left,top,right,bottom,buf);
    return (*env)->NewStringUTF(env, buf);
    }

    /*
    * Class: JConioLib
    * Method: setConsoleTitle
    * Signature: (Ljava/lang/String;)V
    */
    JNIEXPORT void JNICALL Java_JConioLib_setConsoleTitle
    (JNIEnv * env, jobject obj, jstring title)
    {
    char buf[128];
    const char *str = (*env)->GetStringUTFChars(env, title, 0);
    SetConsoleTitle(str);
    (*env)->ReleaseStringUTFChars(env, title, str);
    }
    </code>


    -- To Build
    // Compile to object not executable
    gcc -c conio.c
    javac JConioLib.java
    // Generate header
    javah -jni JConioLib
    // compile to Object not executable notice forward slash
    gcc -g -O2 -c -IC:/MinGW/include -Ic:/j2sdk1.4.2_04/include -Ic:/j2sdk1.4.2_04/include/win32 -g JConioLib.c
    // create dll
    dllwrap --output-def JConioLib.def --add-stdcall-alias -o JConioLib.dll JConioLib.o conio.o
    //Compile and run test
    javac JConioTest.java
    java JConioTest


    Have a good day
     
    , May 11, 2004
    #7
  8. Robotnik

    void_void

    Joined:
    Dec 16, 2009
    Messages:
    1
    JNI Call get stuck

    I have getch() in my c code..

    when ever i am calling my function from java using JNI..
    it gets stuck there..
    suppose i want to bypass that getch functionality from java ..
    how can i do this?
    or any tweak are also welcomed..

    i can;t modify the c code..
    :damnmate:
     
    void_void, Dec 16, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward
    Replies:
    0
    Views:
    439
    Edward
    Aug 10, 2003
  2. Ben Finney
    Replies:
    2
    Views:
    997
    Egor Bolonev
    Jun 30, 2003
  3. Replies:
    1
    Views:
    719
    Frank Millman
    Feb 1, 2005
  4. smartbeginner
    Replies:
    7
    Views:
    418
  5. Replies:
    1
    Views:
    593
Loading...

Share This Page