Tuesday, August 16, 2011

roguelike tutorial 01: Java, Eclipse, AsciiPanel, application, applet

This tutorial will be written in Java since I'm familiar with it and it's a decent enough language, has many tools and libraries, a large and helpful community, and runs on Mac, Windows, linux, and web browsers. It's assumed you know at least the basics but I'll explain some of it as I go along. If you have a preferred IDE then go ahead and use that; otherwise, download the latest version of Eclipse since it's powerful and used by may developers, including me. The one other thing this tutorial uses is AsciiPanel (sourcejar), a side project I started to help display the old-school ascii graphics so common to roguelikes. Even if you want to support graphics, starting with ascii will let us get started quickly.

So let's get started. Download and start Eclipse (or whatever IDE you're most familiar with) and start up a new project. I'm calling mine "rltut". Download the AsciiPanel jar file and add that to your project.

We'll start with something very simple: just a window with some text on it.

package rltut;

import javax.swing.JFrame;
import asciiPanel.AsciiPanel;

public class ApplicationMain extends JFrame {
    private static final long serialVersionUID = 1060623638149583738L;

    private AsciiPanel terminal;

    public ApplicationMain(){
        super();
        terminal = new AsciiPanel();
        terminal.write("rl tutorial", 1, 1);
        add(terminal);
        pack();
    }

    public static void main(String[] args) {
        ApplicationMain app = new ApplicationMain();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setVisible(true);
    }
}

If you're using Eclipse, your project
should look something like this
A humble beginning, but it means that we have all our tools, libraries, and project settings wired up correctly, which can be one of the most frustrating parts. We'll need a few minor changes but the ApplicationMain class is going to stay simple, it's only responsibility is to create a window and delegate input and output to other things.

The serialVersionUID is suggested by Eclipse and helps to prevent show-stopping failures when serializing different versions of our class. We won't be doing that in this tutorial but it's almost always a good idea to take care of compiler and IDE warning as soon as possible; It will save much trouble down the line.

The ApplicationMain constructor has all the set up code. So far that's just creating an AsciiPanel to display some text and making sure the window is the correct size. The AsciiPanel defaults to 80 by 24 characters but you can specify a different size in it's constructor - go ahead and try it. Play around with the write method while you're at it.

The main method just creates an instance of our window and show's it, making sure that the application exits when the window is closed. Simple as can be.

For extra awesomeness you can make your roguelike run from the users browser as an applet. Just add a file like this to your project:

package rltut;

import java.applet.Applet;
import asciiPanel.AsciiPanel;

public class AppletMain extends Applet {
    private static final long serialVersionUID = 2560255315130084198L;

    private AsciiPanel terminal;

    public AppletMain(){
        super();
        terminal = new AsciiPanel();
        terminal.write("rl tutorial", 1, 1);
        add(terminal);
    }

    public void init(){
        super.init();
        this.setSize(terminal.getWidth() + 20, terminal.getHeight() + 20);
    }

    public void repaint(){
        super.repaint();
        terminal.repaint();
    }
}

It's a good start. You don't have much but anyone can play it since it runs on any modern computer either from the user's browser or downloaded and run from the user's machine.

download the code

24 comments:

  1. I seem to be stuck at the import statements. It tells me asciiPanel doesn't exist. Is it because I'm using NetBeans? Or have I put the file in the wrong place?

    ReplyDelete
    Replies
    1. lol, scratch that. forgot to add the classpath. :3 sometimes I think I'm stupider than even I think I am.

      Delete
    2. I am stuck in the same place

      Delete
    3. Is your classpath set up right? Does it include the AsciiPanel.jar?

      Delete
    4. Ok... I have forgotten how to add the classpath again... Help?

      Delete
    5. Again, nevermind. I really am stupider than I think I am.

      Delete
  2. I started the tutorial yesterday, and I was having trouble with an IllegalArgumentException. I think I tracked it to the import of the PNG file. Do I have to do anything special with it? Must it be in any special folder? I had it in the same folder as the ascipanel class. Didn't help to move it to the same folder as the main class either.

    ReplyDelete
    Replies
    1. To further clarify, it seems to be in the loadglyph function. Where the png file loads. Any ideas on what might be causing this? Thanks :D

      Delete
    2. I remember there were quite a few problems on linux. have you downloaded the latest jar file? Worst case scenario: have you downloaded the latest source files and compiled them to a jar file? I know getting java jars to work on all systems is a huge pain, but that's why I've been moving from Java to Flash and ActionScript. Also, that same error seems to come up if the source is hosted on a secured site (https). Maybe that's the problem?

      Delete
  3. Well, I fiddled around with it a bit, turns out it was probably the classpath problem like the others were having. It seems to work now. Thanks for the help though! Really liking this tutorial so far! Keep up the good work! :D

    ReplyDelete
  4. I have the little problem that when I try to use this char ║ it gives me this error.

    Exception in thread "main" java.lang.IllegalArgumentException: character ? must be within range [0,256].

    what's wrong? :)

    ReplyDelete
    Replies
    1. Since AsciiPanel emulates code page 437 (http://en.wikipedia.org/wiki/Code_page_437), you can only draw those characters. Is there a similar looking one that it does support?

      Delete
    2. Said character *is* on code page 437, on 186 (BA). However, in the Java char representation, it isn't, as one would expect, 186, but rather 9553. 186 is º instead, which should be 167, and so on. This is because Java uses the the Unicode code point for char, and does not follow CP 437.
      Essentially, AsciiPanel would need to do some lookup-work to determine whether a given character is on CP 437, and then convert it (an int-int map might be useful).

      Delete
  5. Thanks for your wonderful information which helped us to join java online training

    ReplyDelete
  6. locos ayuda en la la elaboracion de la tabla ascci en java u.u

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. So, I'm attempting to modify the size of the terminal to be 80x42 but this is proving stupidly difficult... any help?

    E-mail: T.Deakin@live.com.au

    ReplyDelete
    Replies
    1. Nevermind, I figured it out, thanks anyway!

      Great tutorial by the way, not following to the letter, more using it to aid me with the use of asciiPanel, but wonderful nonetheless!

      Delete
    2. I'm glad you figured it out and that's the best way to use this tutorial! Adjust what I've learned to your own goals.

      I hope you share what you come up with; either on your own blog, http://forums.roguetemple.com, or elsewhere.

      Delete
  9. I just required some information and was searching on Google for it. I visited each page that came on first page and didn’t got any relevant result then I thought to check out the second one and got your blog. This is what I wanted!

    ReplyDelete
  10. REllay interesting tutorials of JAVA! thanks for u r posts

    ReplyDelete
  11. Very Nice Thoughts. Thanks For Sharing with us. I got More information about Java from Besant Technologies. If anyone wants to get Java Training in Chennai visit Besant Technologies.

    ReplyDelete
  12. Hi,
    Thank u Very much For this Useful Information…


    Import Procedure Chennai

    ReplyDelete
  13. Can this be run on a browser as applet?

    ReplyDelete