Java howto



Index



How to make your basic console-based app


Here's the basic frame for a console-based Java application:

class yourApp {
  public static void main (String args[]) { // Or 'String[] args'
    System.out.println("A line of text outputted to stdout");
  }
}

Use this template each time you make a new console-based app

How to get input in a console-based app


To get a line from standard input(stdin):

class yourApp {
  public static void main (String args[]) {
    String st="";         // Initialize a String object to an empty string
    int i;                // Chararacters inputted from stdin go here after being tested or appended to st
    while (true) {        // Loop forever or until a 'break' statement
      i=System.in.read(); // Get a char from stdin
      if (i==10) break;   // If it's a newline(ASCII 10) then exit
      st += (char)i;      // Cast i(an int) to a char and append it to our string
    }
  }
}

How to make your basic applet


This is a basic template for an applet:

import java.applet.Applet;
/* The extends keyword causes our class to 'extend' the Applet
   class we imported. In other words, we 'inherit' the Applet
   class' methods and public variables. What is inheritance?
   When a class inherits another class' methods and public vari-
   ables, they become available to the class that extended it */
class yourApplet extends Applet {
  public void init () {             // Do initialization stuff
  }
  public void paint (Graphics g) {  // Do stuff during a paint event
  }
  public void update (Graphics g) { // Do stuff when the screen is updated
  }
}

Those are all of the basic methods. You can leave out the paint
and update methods, but it's generally a good idea to keep the
init one. You can also have methods to handle events as well.
Currently, these methods are KeyDown, KeyUp, KeyPress, MouseUp,
MouseDown, MouseMove, MouseDrag, MouseEnter, MouseLeave, and
action. MouseEnter is called when the cursor just enters your
applet's space on the screen and MouseLeave is called when the
cursor leaves it. action is called when any event occurs
it unlike the others, which take an 'Event' and some 'int'
objects, action takes an 'Event' and an 'Object'. The others
have suggestive names. The Key events take an Event and an int(
the key). The Mouse events(except MouseLeave and MouseEnter)
take two int's(the x and y coords of the cursor). There are
some special codes for extended keys for the Key events. Here's
a table of the names and what they represent:
Constant What it represents
Event.HOME The Home key
Event.END The End key
Event.PGUP, PGDN The Page Up and Page Down keys
Event.UP, DOWN, LEFT, RIGHT The Up, Down, Left, and Right arrow keys(i.e. Event.LEFT)
F1..12 The keys F1 through F12(i.e. Event.F5)

Note: You can also add a run() method, which is designed spe-
cifically for threads. I'll explain that later.

How to get input in an applet



This is a template you should use for a basic prompt. You can
spruce it up adding other buttons and control as you wish:
import java.awt.*;
import java.applet.Applet;
public class yourApplet extends java.applet.Applet {
  TextField inputField; // Our input box
  Button bt1;           // The OK button
  String st="";         // Holds the inputted text
  public void init () {
    // The parameter is how many characters wide it is
    TextField inputField=new TextField(15);
    Button bt1=new Button("&OK"); // Or whatever caption you want
    add(inputField); add(bt1);    // Add the controls
    /* Set the layout to a flow layout(I don't really know if
       this is necessary */
    setLayout(new FlowLayout());
  }
  // The action() method is called for any event
  public void action (Event e, Object what) {
    if (e.target==bt1) {          // Our OK button received an event
      // Call upon the powers of OOP to get the text of our input field
      st=inputField.getText();
    }
  } 
}


Threads



If you've never been introduced to threads, threads can be
thought of as kabob(you know, veggies and meat on a stick).
You have some meat and veggies all skewered on a stick. You
Can pull one piece of meat off one stick and a veggie of the
other stick. Threading works similarly. You have tiny chunks
of code broken down into their simplest parts, and the pro-
gram that's running the threads runs those tiny chunks, and
sets up the thread to execute the next chunk. Then it goes
to the next thread and keeps going until it gets to the last
thread, where it goes back to the first thread ad infinitum.
When a thread reaches its last chunk, the program will skip
over it. When it gets to the point where it skips all the
threads, the program ends.
Now, you're ready to see how to implement threads:
class yourApp extends Thread {
  public static void main (String args[]) {
    // Make a new thread object from our class
    Thread thread1=new Thread(new yourApp());
    // Start the thread
    thread1.start();
  }
  public void run () {
    // Some code here
  }
}

Now you may be wondering, how do I define more than one
thread in one class? To do this, simple define a class inside
the main one and make it extend the Thread class, just like
its parent.
Whenever using threads, you must put the threaded code to
run a publicly declared void(doesn't return a value) method
named run().
Note: When making a threaded applet, it's probably a good idea
to put implements Runnable after the extends keywords and all
the class names following it.