Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: java clear screen function

  1. #1
    Join Date
    Jan 2004
    Posts
    2,011
    Rep Power
    0

    Default java clear screen function

    when writing console applications if there is no clear screen things do often tend to get messy, so i ask, in java how do you clear the screen, similar to in c++ clrscr();

    or include stdio.h and use system('CLS&#039 where the clear screen is wanted. what in goods name do i use to accomplish this in java, somebody help me please, this holding up my progress got a lot of other languages to learn ;D ;D ;D

  2. #2
    Join Date
    Jul 2002
    Posts
    818
    Rep Power
    0

    Default Re:java clear screen function

    I'm not gonna say it doesn't exit, I'll just say that I've never come accross one.

    You're better off Sytem.out.println'ing a few '\n's to the screen.

  3. #3
    Join Date
    Jun 2002
    Posts
    648
    Rep Power
    0

    Default Re:java clear screen function

    Don't know if something like this would help:

    public void clearScreen ()
    {
    Graphics dg = drawingArea.getGraphics();
    Rectangle r = drawingArea.bounds();
    dg.setColor (this.getBackground ());
    dg.fillRect (r.x, r.y, r.width, r.height);
    }

  4. #4
    Join Date
    Jan 2004
    Posts
    2,011
    Rep Power
    0

    Default Re:java clear screen function

    thanks for the help guyz, i think i gonna go wit cknight's suggestion

  5. #5
    Join Date
    Mar 2004
    Posts
    232
    Rep Power
    0

    Default Re:java clear screen function

    Screen clearing is OS and Hardware dependent depending on the platform call the native API function that is able to clear the screen. lookup Runtime.getRuntime().exec that sould be able to execute a command com command

  6. #6
    Join Date
    Jan 2004
    Posts
    2,011
    Rep Power
    0

    Default Re:java clear screen function

    i went wit cknights "\n" idea but its just gonna be a temporary fix, i wrote a lil function to print a million and 1 "\n"'s ;D. but you know what, "\n" is only effective after a word, it doesn't stand alone as opposed to c++ (the better language in my eyes). any way how do you package a java program when your finished writing it.

    oh by the way there must me some proper way to clear the screen cause i'm thinking that \n in that multitude is a waste of system memory, how does a clear screen function or system ('CLS&#039 in c++ work i don't think it actually prints alot of (endl; or \n)

  7. #7
    Join Date
    Jul 2002
    Posts
    818
    Rep Power
    0

    Default Re:java clear screen function

    [quote author=death_knight link=board=21;threadid=3420;start=0#msg31594 date=1080434164]
    i went wit cknights "\n" idea but its just gonna be a temporary fix, i wrote a lil function to print a million and 1 "\n"'s ;D. but you know what, "\n" is only effective after a word, it doesn't stand alone as opposed to c++ (the better language in my eyes). any way how do you package a java program when your finished writing it.

    oh by the way there must me some proper way to clear the screen cause i'm thinking that \n in that multitude is a waste of system memory, how does a clear screen function or system ('CLS&#039 in c++ work i don't think it actually prints alot of (endl; or \n)
    [/quote]

    I don't know the answer to that but here's a cool trick:

    -Get your hands on a Linux box and open a cosole.
    -Type some junk on the screen, anything to get it crowded.
    -type "clear [return]" (it should be obvious what that does)
    -Scroll up

  8. #8
    Join Date
    Mar 2003
    Posts
    1,700
    Rep Power
    0

    Default Re:java clear screen function

    CKnight's suggestion is the correct way to do it in Java my friend.

    The C/C++ clrscr() function resets the bit shift registers that outputs data to a console screen. That essentially empties the registers, which ultimately empties the screen. But clrscr() is very platform specific and different libraries are required to summon the function under different operating systems.

    Java cannot work like that because Java is engineered to be very platform independent. Therefore, extremely low level functions like writing to hardware registers or even buffers are virtually impossible under Java. You will have to write a platform specific program to do that kind of job and call it from Java.

    Remember, Java outputs your program to a set of bytes that can run on any platform. However, in order for those bytes to run, Java has to have its own GUI definition (as you may have noticed by now) it's own highly generic system calls (calls present in every operating system) and it's own tightly integrated OOP implementation. This makes Java completely independent of function calls present in any specific operating system, and hence ultimately platform independent.

    This is also what makes Java extremely slow. : The host operating system is burdened with running a whole new API (Application Programmer's Interface) atop of it's very own. So running Java programs is essentially the same thing as running two operating systems, one atop the other.

    Linux uses a slightly different version of the C core functions than Windows (Linux doesn't use C++), but predominantly supports the Java J2EE standard. As such, any little work arounds for such things as clearing the screen, are done using the Java workaround...

    ...which in this case, is printing a multiplicity of '\n's.

    If you are running under Windows, you can try summoning the DOS command 'cls' using one of Java's shell functions if you don't like the \n solution.

    Good luck.

  9. #9
    Join Date
    Jan 2004
    Posts
    2,011
    Rep Power
    0

    Default Re:java clear screen function

    thanks alot xeno that sure did clarify everything i asked. the super answer. ;D ;D. i guess i'll just work wit the \n's then. ohh but just for saying it sake, there is this tool called "jni" i think it is used to allow java to pull from c++ libraries ergo the clrscr() or system ('CLS&#039 might be usable but as you stated it wouldn't make any sense because the java is platform depended. thanks alot man.

  10. #10
    Join Date
    Mar 2003
    Posts
    1,700
    Rep Power
    0

    Default Re:java clear screen function

    WARNING: If you use that tool, your java program will no longer be platform independent. In fact, doing something like that can be downright dangerous if someone tries to run your program under a different operating system.

    The memory bit shift register references (usually 0x02FE upwards) are different under LINUX. So if someone tries to run your program under Linux (or any operating system other than Windows), only God knows what can happen! In the worst case, they might suffer hardware damage!

    Use that tool with care, and esure to notify anyone that uses the final program you made that it's specifically targetted for Windows. Cool?

    Glad I could help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •