Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Server Console Cross-post  (Read 1005 times)
0 Members and 1 Guest are viewing this topic.
Offline Tjstretchalot

Junior Member


Medals: 2
Projects: 1



« Posted 2012-10-02 21:59:41 »


Sort of a cross-post from this, except oriented towards a server. Code has the same base, maybe more up to date.

Hi, lets not reinvent the wheel here when it comes to making a simple gui for Servers, especially when it is independent from the client (Not uncommon).  It would be simplest if one could just tell the computer "use swing instead of console", and then "if any of these are said, do xyz, otherwise do abc".  This is just that, except with two different levels.  The lower level acts as close to System.out and a Scanner wrapping as is possible, and is shown in this pastebin.

The relevant code of the sort of things you could do with this look like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
/**
   * For testing purposes, shows an example usage of this program
   */

  public static void main(String[] args)
  {
    ConsoleGUI.toGUI();
    ConsoleGUI.setSaveText(true);
    TAScanner scanner = ConsoleGUI.getScanner();
    TAPrintStream out = ConsoleGUI.getStream();
    out.println("Type a string");
    String str = scanner.nextLine();
    out.println("Type an int");
    int i = scanner.nextInt();
    out.println("You typed "+str+" and "+i);
    out.println("Say something starting with y");
    boolean b = scanner.nextBoolean();
    out.println(b);
    out.println("Say something starting with n");
    b = scanner.nextBoolean("n", true);
    out.println(b);
    out.println("Say banana");
    b = scanner.nextBoolean("banana", false);
    out.println(b);
   
    out.println();
    RuntimeException exc = new RuntimeException();
    handleException("Test", exc, saveRun);
    //for(int i = 0; i < 50; i++) { out.println("Purposeful Spam"); }
 }


This has methods from ints to doubles, although throws exceptions if you ask for an int and get "asdf".  Intuitive and consistent, makes a console-based ASCII game easily converted into an executable jar, but besides that doesn't seem like anything special.  The sub-class however, will make the 'gui' part of your code be able to cleanly and nicely fit inside one class or several anonymous classes.  That way, you can do more fun stuff, like that network code you "forgot" to do. Pastebin

This would let you do something like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public static void main(String[] args) {
  CommandListener listener = new CommandListener() {
    @Override
    public void onCommand(String nm, String[] params) {
      if(nm.equalsIgnoreCase("eatanapple") {
        String aniName = params[0];
        ConsoleGUI.getStream().println(aniName + " ate an apple!");
      }else if(..) {
        ...
      }
    }
  };
  ServerConsole.getGUI().registerCommand("eatanapple",
                                         "Makes an animal eat an apple. Example: eatanapple Bob", listener);
  ServerConsole.updateHelp(); // Called when you finish registering commands.
}

A few 'special' commands that it will have by default include:
default - If an unknown command is entered, same as help if not overriden
help - prints the list of commands. updateHelp() MUST be called after all registerCommands(..) to be up-to-date
memory - prints memory usage.

This is what typing memory might look like:
1  
2  
3  
4  
5  
> memory
Allocated Memory / Max Memory: 259,522,560 (247.5 megabytes)
Total Memory: 16,318,464 (15.56 megabytes)
Free Memory: 11,736,760 (11.19 megabytes)
Used Memory: 4,581,704 (4.37 megabytes)
Offline ReBirth
« Reply #1 - Posted 2012-10-03 14:26:20 »

Thanks for sharing the code!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!