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

78 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
  14. Do most browsers have Java built in and ready to display applets?

    ReplyDelete
  15. How can we work with a sprite sheet or just colors instead of the AsciiPanel ?

    ReplyDelete

  16. Great information here. Thank you for sharing. If you are ever in a need of colorado
    24 hour
    Services we are Aurora Locksmith Services. You can find us at auroralocksmithco.com.com or call us at: (720) 220-4851.

    ReplyDelete
  17. It is a well known fact that Java as a programming language set off a new paradigm in the software industry. Suddenly, every software programmer worth his salt was amidst software jargons like 'Platform-Independence', 'Cross-Platform-Deployment' and 'The Java Virtual Machine'.

    java

    ReplyDelete
  18. Java India is an India based emerging company provides complete Java web solutions. We offer Java website development, Java application development and lot more services across the globe.

    ReplyDelete
  19. AsciiPanel.write(String text, int x, int y) gives an error when the length of the string + x >= 80, but it should only give an error when it is > 80. If it = 80, then that just means it reaches the edge of the console but not over it.

    ReplyDelete
  20. Hey there. I'm a java novice programmer. I get no errors, but when I try to run I get the following error on the Shell window:
    Error: Could not find or load main class rltut.ApplicationMain

    Any help?

    ReplyDelete
    Replies
    1. I'm having the exact same issue. Compiling and running from the command line.

      Delete
  21. java Tutorial for Beginners - A simple and short Core JAVA Tutorial and complete reference manual for all built-in java functions.

    ReplyDelete
  22. Our Java Application Development solution service is there to help the clients to receive better and more accomplished java solutions upwards right from the grassroots level. Our experienced team of professional programmers has ensured that our client's receive excellent java applications solutions. Our accomplished team has forever been highly knowledgeable with highest skill-sets to create products and software application with Java Technology Development while mastering its nuances and its very own specialties.

    ReplyDelete
  23. I followed your tutorial few years ago, but real life took over and had to abandon my project. I'm thinking to do it again, but use it as the basis for my own project. Would you mind if I do that? I will give you credit.

    ReplyDelete
  24. Very useful post. It is very simple and informative information. Keep sharing. Java training institutes in pune

    ReplyDelete
  25. Thanks for sharing this and its very useful post, it was so interesting to read & appreciate your work for blog post which is very useful.

    ReplyDelete
  26. Thanks for sharing this and its very useful to the back end developers. The explanation given is really comprehensive and informative .

    ReplyDelete
  27. java tutorial blog easy to learn

    http://bestjavatutorialblog.blogspot.in/

    ReplyDelete
  28. While using arrays, we create objects for arrays where class is non-existent. Whenever JVM encounters [] It understands that it must create an object. Thus, array object can be created without using the new operator. Find more Tips and JAVA Homework Help in Array.

    ReplyDelete
  29. These tutorials are very helpful and easy to understand. Keep posting..!!

    ReplyDelete
  30. Very useful list, it was helpful for me, Thanks for sharing.
    Mobile App Development Company Indore

    ReplyDelete
  31. Do you mind if i use this jar that you made for commercial purposes

    ReplyDelete

  32. I have seen lot blogs and Information on othersites But in this Java Blog Information is very useful thanks for sharing it........

    ReplyDelete
  33. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
    "

    ReplyDelete
  34. the blog is aboutAutomated data lineage documentation using #Java it is useful for students and Java Developers for more updates on Java follow the link
    java Online Training Hyderabad

    ReplyDelete
  35. Linux Online training in India – Webtrackker Technology is providing the linux online training with 100% placement support. If you are looking for the BEST LINUX & UNIX Training Institute In india or linux online training from india, live project based LINUX & UNIX online training then you can contact to us.

    Python online training in India, RPA Online training in India, Salesforce online training in india, AWS online training in india, Cloud Computing Online Training in India, SAS Online Training in india, Hadoop online training in INDIA, Oracle DBA online training in India, SAP online Training In india, Linux Online training in India








    ReplyDelete
  36. Australia Best Tutor is one of the best Online Assignment Help providers at an affordable price. Here All Learners or Students are getting best quality assignment help with reference and styles formatting.

    Visit us for more Information

    Australia Best Tutor
    Sydney, NSW, Australia
    Call @ +61-730-407-305
    Live Chat @ https://www.australiabesttutor.com




    Our Services

    Online assignment help
    my assignment help Student
    Assignment help Student
    help with assignment Student
    Students instant assignment help

    ReplyDelete
  37. Appreciate Your Work, Very informative Blog on Android Development, I would like to share some information on Android Development training which will help for the blog here.

    ReplyDelete
  38. CIITN is the pioneer of education providing the best PHP training in Noida as per the current industry requirement that enables candidates to land on their dream jobs in companies worldwide. CIITN Provides best PHP training course in Noida. CIITN is a renowned training company providing the best training service and also being the best PHP training institute in Noida rendering practical knowledge through training on projects and a dedicated placement assistance for all. The course curriculum for PHP training course is designed to provide in-depth knowledge that covers all the modules for the training ranging from basic to advanced level. At CIITN PHP training in Noida is supervised and managed by industrial experts having more than 10 years of experience in handling PHP projects. CIITN training comprises of both classroom as well as practical sessions to deliver an ideal environment for students that will enable them to handle difficult and complex situation when they would step into the reality of IT sector.CIITN is an excellent PHP training center in Noida with superior integrated infrastructure and newly designed labs for students to practice and pursue training for multiple courses at Noida. CIITN institute in Noida train thousands of students around the globe every year for the PHP training at an affordable price which is customised as per each candidate’s requirement of modules and content.


    PHP training in Noida

    ReplyDelete
  39. CIITN is the Best Php training institute in Noida and delhi Ncr. You will get Live Project Training on PHP by our PHP expert who have 5+ year industrial experience.Focus on practical and live project training. In our PHP training, we you will learn core PHP, advance PHP, HTML, CSS, JavaScript, jQuery, Bootstrap, Cake PHP and Wordpress.CIITN provides 100% job assistance in PHP training. CIITN is well known PHP coaching center because our 100% PHP students are placed now.


    Ciitnoida provides Core and java training institute in noida. We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-oriented, class-based build of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13 years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best Java training in Noida.


    java training institute in noida
    php training in noida
    linux training in noida
    linux institute in noida
    java course in noida

    ReplyDelete
  40. CIIT Noida provides Best MCA Courses in Noida based on the current IT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad. CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.

    At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to students that transform an average student into a corporate professional whom companies prefer to hire.

    Best MCA College in Noida

    ReplyDelete
  41. HADOOP TRAINING INSTITUTE IN NOIDA

    CIITN provides Big data hadoop training in Noida in Noida as per the current industry standards. Our training programs will enable professionals to secure placements in MNCs. CIITN is one of the most recommended Hadoop Training Institute in Noida that offers hands on practical knowledge/practical implementation on live projects and will ensure the job with the help of advance level Hadoop Training Courses. At CIITN Hadoop Training in Noida is conducted by specialist working certified corporate professionals having 8+ years of experience in implementing real-time Hadoop projects.CIITN is the best Hadoop training center in Noida with a very high level infrastructure and laboratory facility. The most attractive thing is that candidates can opt multiple IT training course at Noida location. We feel proud by announce that CIITN prepares thousands of candidates for Hadoop training at sensible fees structure which is sufficient for best Hadoop training in Noida to attend the Hadoop classes.Hadoop training course includes “Knowledge by Experiments” strategy to get Hadoop training and performing real-time practices and real-time modulation. This extra ordinary practices with live environment experience in Hadoop Training certifies that you are ready to apply your Hadoop knowledge in big corporations after the Hadoop training in Noida completed.


    Big data hadoop training in Noida

    B-12, Sector - 2, Noida, U.P
    State - Uttar Pradesh U.P
    Country - India
    Pin Code - 201301

    Phone - +917290926565
    Mon - Sun: 10am - 6pm

    ReplyDelete
  42. Best Linux Training Institute in Noida


    CIITNOIDA provides Best Linux Training in Noida
    as per the current industry standards. Our training programs will enable professionals to secure placements in MNCs. CIITNOIDA is one of the most recommended Linux Training Institute in Noida that offers hands on practical knowledge / practical implementation on live projects and will ensure the job with the help of advanced level Linux Training Courses. At CIITNOIDA Linux Training in Noida is conducted by specialist working certified corporate professionals having 8+ years of experience in implementing real-time Linux projects.
    CIITNOIDA is the well-known Linux training institute with high tech infrastructure and lab facilities. We also provide online access of servers so that candidates will implement the projects at their home easily. CIITNOIDA in Noida mentored more than 3000+ candidates with Linux Certification Training in Noida at very reasonable fee. The course curriculum is customized as per the requirement of candidates/corporates.
    In addition to this, our classrooms are built-in with projectors that facilitate our students to understand the topic in a simple manner.
    CIITNOIDA is one of the best Linux Training Institutes in Noida with 100% placement support. We are following the below “P3-Model (Placement Preparation Process)” to ensure the placement of our candidates.

    Linux Training Institute in Noida

    ReplyDelete
  43. Best Shell Scripting Training in Noida

    The training imparted by CIITN makes one develop his job accessibility. UNIX SHELL SCRIPTING training is imparted in such a method that the students become technically sound and that enhances their capability to work with this knowledge as technocrats. UNIX SHELL SCRIPTING is a script printed for the shell, or command line predictor of any operating scheme. The shell is often taken as aneasy domain-specific program language. Characteristic operations done by shell scripts include file management, program implementation and printing text. CIITN gives guidance by essentially making the student work with UNIX SHELL SCRIPTING. The teaching procedure is set in such a way that the learner gets a real feel of the work. The knowledge is trained in a step by step procedure so that the students can get that into their head and put into practice it when needed. They are permitted to create and manage tables, do scripting and actual feel the pulse. The trainers at CIITN is the most outstanding. They have the first hand information of the procedure and really communicate that to their students. The students feel confident that they are at CIITN for the education.
    CIITNOIDA offers shell scripting training with choice of multiple training locations across noida. Our unix shell scripting training centers are equipped with lab facilities and excellent infrastructure. We also provide unix shell scripting certification training path for our students in noida. Through our associated shell scripting training centers, we have trained more than 129 shell scripting students and provided 80 percent placement.

    Unix Shell Scripting Training in Noida



    ReplyDelete
  44. Java Training in Noida
    CIITN provides Best java training in noida based on current industry standards that helps attendees to secure placements in their dream jobs at MNCs.The curriculum of our Java training institute in Noida is designed in a way to make sure that our students are not just able to understand the important concepts of the programming language but are also able to apply the knowledge in a practical way.

    if you are looking for the best oracle sql certification center in Noida, CIIT is worth to consider. CIIT is a oracle training institute offering best sql course, oracle training, sql certification and oracle dba training at affordable price. Best Oracle training in Noida.

    Java Training in Noida

    best java training in noida

    ReplyDelete
  45. Best Summer Internship In Noida

    These technologies prepare individuals for fields like software programming, technical support, graphic design, software testing, business analytics. Embedded Systems, Industrial Automation Training and more. Candidates go through a series of comprehensive practical sessions where they work on live problems and implement solutions on real-time basis.
    It has a dedicated placement cell which provides 100% placement assistance to students. The benefits that a student gets out of summer training are innumerable. CIITN,Best Summer training Center for B.Tech/CS/CSE/IT/ BCA/MCA/ B.E /M.tech / B.sc/ M.sc/ Engineering Student has already accomplished itself successfully in the field of Training and Development after setting milestones and bringing smiles to the faces of more than 1 Lakh students. CIITN was incorporated in the year 2002 and over the years CIITN has grown remarkably into the greatest training giant of Northern India.
    There is no looking back to technology! Those passionate for it need not worry about the professional prospects it offers. Courses and trainings in the field of Computer Science and Applications open a wide array of choices for individuals. All you need to do it prepare yourself right!

    Summer Internship Course In Noida

    Summer Internship Training In Noida

    ReplyDelete
  46. I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    best online training for salesforce

    ReplyDelete
  47. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up selenium Online Training Bangalore

    ReplyDelete




  48. Hi Your Blog is very nice!!

    Get All Top Interview Questions and answers PHP, Magento, laravel,Java, Dot Net, Database, Sql, Mysql, Oracle, Angularjs, Vue Js, Express js, React Js,
    Hadoop, Apache spark, Apache Scala, Tensorflow.

    Mysql Interview Questions for Experienced

    php interview questions for experienced

    php interview questions for freshers

    python interview questions for freshers

    tally interview questions and answers

    ReplyDelete
  49. The Best article, Bala Guntipalli This concept is a good way to enhance the knowledge.

    ReplyDelete
  50. Nice article.. This concept will help for everyone. Nice explaination about Java, Eclipse, AsciiPanel, application, applet. In this tutorial so much information. I would like to share about some online training.

    ReplyDelete
  51. Nice blogs about Mastering Python With Mobile Testing at The
    Mastering Python With Mobile Testing training in bangalore

    ReplyDelete
  52. The article is too good and it will be helpful for java developers.Thanks for sharing such useful information.
    Java training in Chennai

    ReplyDelete
  53. In Chennai we at Aorta Digital Sevices publish more relevant SEO materials for young job seekers.
    Digital Marketing Training Institute in Chennai | SEO Training in Chennai

    ReplyDelete
  54. Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.

    AutoCAD training in Noida

    AutoCAD training institute in Noida


    Best AutoCAD training institute in Noida

    ReplyDelete