9

OK, this is going to sound like a crazy idea - but I'm interested in emulating a 1980s style roguelike game text interface in pure Java, i.e. using Swing or similar.

Here's roughly what it needs to do:

  • Provide a fixed size grid of fixed size characters as the "screen" (e.g. 100*75)
  • Use an appropriate monospaced font, ideally with lots of interesting symbols
  • Allow setting of foreground and background character colours for each character position individually
  • Allow printing of strings or individual characters at any place in the screen (which should overwrite whatever is already in the screen buffer in those locations)

Anyone know of a good existing solution that would enable this? Or am I stuck with hacking one together from scratch?

p.s. the reason I want pure Java is so that it can run in a sandboxed applet. So JNI solutions like jcurses are sadly ruled out.....

mikera
  • 105,238
  • 25
  • 256
  • 415
  • possible duplicate of [Java/swing: console component?](http://stackoverflow.com/questions/2806012/java-swing-console-component) – Art Licis Feb 02 '11 at 21:00
  • 1
    I remember when I first played Angband, it was so long ago people weren't even looking at me as if I was mad. :) – biziclop Feb 02 '11 at 21:07

6 Answers6

9

Not crazy at all, this is the approach I implemented in Legerdemain:http://roguelikefiction.com

I used a two dimensional array of characters (char[][]) with a corresponding array of java.awt.Color[][] objects to track the colors. You can shove these arrays into a class that inherits from JPanel (which in turn is part of a JFrame) and do all of the drawing in the panel's paintComponent() callback.

Nothing wrong with the Curses/JNI approach either, although you get all sorts of great Unicode glyphs if you go the Swing route. Legerdemain uses five or six hundred of them.

Nathan Jerpe
  • 106
  • 2
  • 1
    Thanks, I ended up implementing something very similar to this - available here for anyone interested: http://code.google.com/p/mikeralib/source/browse/trunk/Mikera/src/main/java/mikera/ui/JConsole.java – mikera Feb 10 '11 at 23:12
  • p.s. which Unicode fonts did you find best for roguelike glyphs? – mikera Feb 10 '11 at 23:12
  • 1
    @mikera Courier New and Lucida Sans Typewriter. They are both fixed width, guaranteed to be available by the Java runtime, and contain lots of glyphs. – Nathan Jerpe Feb 17 '11 at 12:32
  • @mikera: nice! (You may want to add this as an answer, so it would be easier findable.) – Paŭlo Ebermann Feb 20 '11 at 01:42
  • @Paulo: See my answer below, also see code at :http://code.google.com/p/mikeralib/source/browse/trunk/Mikera/src/main/java/mikera/ui/JConsole.java – mikera Feb 20 '11 at 11:23
  • @NathanJerpe I am building a text-based game and think Legerdemain looks great. Would you still recommend going the Swing route? (I have spent months building the backend, but zero time on the interface so far, and can't decide on an approach.) – john_science Nov 15 '12 at 23:03
4

For projects of this sort, I found it essential to rigorously separate the game model and view. This simple example suggest the overall architecture, and this more complex game expands on the notion. The benefit is that the view can evolve separately from the game itself, which doesn't care what the listening view(s) look like.

For symbols, Unicode glyphs may be an appealing option, as suggested in this example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

Provide a fixed size grid of fixed size characters as the "screen" (e.g. 100*75)

string[] screen = new string[75], then just fill each one with 100 spaces :).

Use an appropriate monospaced font, ideally with lots of interesting symbols

See this links for a few good ones: http://cg.scs.carleton.ca/~luc/mono.html

Allow setting of foreground and background character colours for each character position individually

You could have those text effects by using a control that allows rendering of HTML like the JEditorPane. That way you could just define special keywords as "special keyword". (Ok that is a bit deprecated but should work fine for your case. It would be easiest if you store your 'game state' as just a normal string (array), but have the html rendered just before you output it.

Allow printing of strings or individual characters at any place in the screen (which should overwrite whatever is already in the screen buffer in those locations)

If you followed my advice in the previous question than you have your gamestate as a normal string array, then you just find the string for your line, find use string.substring(length) + "A" + string.substring(startindex: length + 2, string.length - (length + 2)); to construct your new gamestate.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Roy T.
  • 9,429
  • 2
  • 48
  • 70
2

I once (years ago) started to code something like this (a terminal implementation in Swing). I got to the point that I could display text with ANSI escape sequences for cursor movement and colors in it, but didn't implement any input. If you are interested, I'll dig it out.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
2

Since you are talking rogue and character based interfaces in order to make the trip to the past complete, why don't you google for a Curses C implementation and do the View with JNI. Your Model and Controller are regular Java. There is a Curses implementation for almost every platform.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
  • Yeah, that was my first thought but I'd like it to be able run it in an unprivileged sandbox.... hence JNI won't work :-( – mikera Feb 02 '11 at 23:36
1

I ended up implementing a simple Swing console, inspired by many of the answers here (thanks everyone!)

For those who are interested, it's available here:

Swing based Java text console

mikera
  • 105,238
  • 25
  • 256
  • 415
  • 1
    Update: now hosted on GitHub as a separate library : https://github.com/mikera/swing-console – mikera Oct 25 '13 at 07:50