Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Clearing the Screen
Index -> Programming, Java -> Java Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ProgrammingFun




PostPosted: Wed Dec 15, 2010 8:56 pm   Post subject: Clearing the Screen

Is there any way of clearing the screen (non-GUI) in java?
I want to use a function similar to cls in Turing but have not been able to find one so far...

Thanks for the help.
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Wed Dec 15, 2010 9:44 pm   Post subject: Re: Clearing the Screen

What you are explaining is very vague in Java, if you are talking about console (like System.out.println(...)) and similar then it is not possible really without shell level commands, cause the actual command line interface is not part of Java, but part of the shell in the operating system.
DemonWasp




PostPosted: Wed Dec 15, 2010 10:35 pm   Post subject: RE:Clearing the Screen

Plus, it's kinda hard to define what cls should do if your output is redirected to a file.

If you actually want to develop that kind of interface, make a simple GUI with just a text box (not editable) that you use for your display / output, and one smaller one (editable) that they use for input.
ProgrammingFun




PostPosted: Thu Dec 16, 2010 6:47 am   Post subject: RE:Clearing the Screen

I haven't learned GUI...
and yes, I am talking about console output...
DemonWasp




PostPosted: Thu Dec 16, 2010 9:55 am   Post subject: RE:Clearing the Screen

There's no real way to cls your output. System.out just refers to a PrintWriter, which has no clear functionality -- and for good reason; as I mentioned above, there are lots of contexts where this makes no sense.

You can either write enough lines of output to drive the old output off the screen, or you can not use any kind of clear screen function, or you can use languages with better console support. I would recommend the first or second option.
Dan




PostPosted: Thu Dec 16, 2010 5:06 pm   Post subject: RE:Clearing the Screen

Well you can't clear the screen the same way you do in Turing and for good reason (as DemonWasp and others pointed out) you can use ASCII controller characters to manipulate the console a bit.

For example ASCII character 12 (some times denoted as "\f") is form feed and may have a similar effect to clearing the screen (depending on the terminal). I don't have Javac installed on this computer so i can't test but i blive in most terminals it would have the effect similar to outputting blank lines in till it is off the screen and then moving the cursor to the top of the screen.

Another reason not to do this is portability. The action of clearing the screen is not defined in the same in all terminals and operating systems. Well you may get the desired effect in a windows terminal you may just get a mess in a linux terminal.


Edit: I tried typing the form feed char into the Windows 7 terminal and a Linux terminal (running the bash shell) via putty. The result was their was no effect on Windows and the Linux term cleared it's self. This highlights the issues with portability.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
SS1389




PostPosted: Fri Dec 17, 2010 12:32 am   Post subject: Re: Clearing the Screen

Is this for some culminating? wink wink Laughing
ProgrammingFun




PostPosted: Fri Dec 17, 2010 12:23 pm   Post subject: RE:Clearing the Screen

@SS1389: Yes...it is for the Orthello game Wink
@Everyone else: Thanks for the help. I am developing my program on Win7 and it will be marked on WinXP...so I guess I won't try to do anything fancy with the clearing of the screen.
Sponsor
Sponsor
Sponsor
sponsor
SS1389




PostPosted: Fri Dec 17, 2010 2:47 pm   Post subject: Re: Clearing the Screen

othello
SS1389




PostPosted: Fri Dec 24, 2010 12:16 pm   Post subject: Re: Clearing the Screen

Yeah, I wanted to clear the screen too, but moving it off the run i/o panel is much easier. That way, it seems like a new update to the game board.
mirhagk




PostPosted: Sun Dec 26, 2010 12:30 am   Post subject: RE:Clearing the Screen

couldn't you just \b... I'm not a java programmer but output the backspace char would do the same in most OS i think.
ProgrammingFun




PostPosted: Mon Dec 27, 2010 3:56 pm   Post subject: Re: RE:Clearing the Screen

mirhagk @ Sun Dec 26, 2010 12:30 am wrote:
couldn't you just \b... I'm not a java programmer but output the backspace char would do the same in most OS i think.

When I try to output \b, I always get
Result:
  08
as the answer. I used the following as a test case:
Java:

import java.util.*;

public class Test
{
        public static void main (String[] args)
        {
        Scanner sc = new Scanner (System.in);
        System.out.println ("Enter this");
        String fs = sc.next();
       
        System.out.println ("\b");
        }
}
SS1389




PostPosted: Mon Dec 27, 2010 4:09 pm   Post subject: Re: Clearing the Screen

That's because it's the unicode for backspace (\b).
ProgrammingFun




PostPosted: Mon Dec 27, 2010 5:06 pm   Post subject: Re: Clearing the Screen

SS1389 @ Mon Dec 27, 2010 4:09 pm wrote:
That's because it's the unicode for backspace (\b).
I realized that Razz ...
btiffin




PostPosted: Tue Dec 28, 2010 3:32 am   Post subject: Re: Clearing the Screen

It may not be completely cross platform, but, being old, I have yet to find a console that doesn't support at least some of the ANSI control sequences (outside the glaring Windows/NT console percockle).

Try
code:

final static String CSI = "\0x1B[";
System.out.print(CSI + "2J");
System.out.flush();


Basically escape (ASCII 27) + open square + 2J

The escape + open bracket is usually documented as CSI, Control Sequence Introducer. 2J clears the screen.

And following Dan's advice, ANSI CSI isn't supported on all platforms. So if you plan on your code being used by the world, ignore this.

Cheers
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 37 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: