Pre-Defined Data types in JavaTopHello World!Using objectdraw

Using objectdraw

Java also supports an objectdraw library - in fact it predated the Grace objectdraw library by 15 years. The ideas are similar, but the syntax is different. This next program repeats the Scribbler program that we wrote early in the semester. Because I am preparing you more for more advanced courses, we are going to use a slightly different approach than regular CS51. They use something called "Applet"s, which can be embedded in web pages, while I will use applications. Thus if you compare these programs with those written for CS 51 you will see some minor differences involving the use of the main method (they don't use one, while we will). Our approach will make it easier for you to see the similarities with Grace programs.

Click on the following link to see a running version of the Scribble program and its text: scribbler demo

Before we get into details, let's talk about comments. Java has line-oriented comments starting with //, just like Grace. However, Java also has multi-line comments that begin with /* and end with */. Anything between that start and end symbol is ignored by the compiler. For example

/*  This
      is a
      multi-line
      comment
*/

Java also includes special comments that can be picked up automatically to generate documentation about a program (using a tool called JavaDoc). These follow the same rules as for multi-line comments but have a few extra embellishments that are used to help the tool. Here is an example:

   /**
    * save the current mouse position when mouse pressed as first coordinate of
    * the line
    * 
    * @param point -- location of mouse during press
    */
    public void onMousePress(Location point) {

They start with /**, successive lines start with a * until the last line, which ends with the usual */. Inside words that start with @ have special meaning. An example is @param above. The rest of that line gives a description of the parameter point to the method. Another common keywords for comments is @return, which is generally followed by a description of the value returned by a method.

Programs that use the objectdraw library need two special features:

  1. You must import the objectdraw library

  2. The main class must be declared as extends WindowController.

Skipping the comments at the top of the file, this is done by writing:

import objectdraw.*;

public class Scribble extends WindowController {

The import of objectdraw is similar to declaring the objectdraw dialect in Grace. (The * means import everything in that library.) The declaration that Scribble extends WindowController is similar to Grace's declaration that an object inherits graphicApplication.size....

The class Scribble has one instance variable, nextLineStrarts. The declaration is written as:

   private Location nextLineStarts;

The identifier nextLineStarts has type Location, which is similar to Grace's Point type. We must include a visibility modifier to all declarations in Java. For instance variables this is generally private, which means that the identifier is only visible inside the class (not in any of its subclasses, however, so it is more restrictive than our confidential.

In general instance variable declarations consist of a visibility modifier (generally private), followed by the type of the variable, followed by the name of the variable and then a ;. There is no keyword like var in Grace that informs the reader that it is a variable declaration. The declaration can also include code to initialize the variable, e.g.,

   private Location nextLineStarts = someLocn;

Notice that an = is used to assign values to variables rather than :=, as we are used to.

You should use the standard naming conventions for identifiers in Java. Classes and types all start with capital letters, like Location and Scribble. Variables and methods always start with lower case letters. We will see later that constants are written in all caps, with _ used to separate words in the name, e.g., PAUSE_TIME.

There are two methods in the class, onMousePress and onMouseDrag. They serve the same purpose as the corresponding methods in Grace. As with variables, there is no keyword to indicate that something is a method. Instead you recognize this by the form of the declaration.

   public void onMousePress(Location point) {
      nextLineStarts = point;
   }

As with instance variables, methods start with a visibility annotation. There are three options for methods:

The protected annotation is similar to Grace's confidential. The private annotation does not correspond to anything in Grace. Most methods are either public or private. Methods are declared protected when you anticipate the need to override them in subclasses.

Method onMousePress takes a single parameter point with type Location. Notice that type annotations always appear before the identifier in either a variable or parameter declaration. Similarly, the return type of a method always appears immediately before the method name. The type void in Java corresponds to Done in Grace. It is the return type of a method that does not return a value.

Method names in Java are simpler than in Grace in that they are not allowed to have multiple parts. Thus in Java, the complete name of the method occurs before a single list of all the parameters. Methods that have no parameters are required to be terminated by () in Java. Thus one would request the width of a rectangle by writing myRect.getWidth(), while in Grace we would have written the somewhat simpler myRect.width.

The same is true when we construct new objects from classes. Classes have special constructors that are separate from the class header. We will see these in more details soon, but for now you should know that new objects are created by applying the new operator to a class name and list of parameters. For example, our onMouseDrag method for this class is:

   public void onMouseDrag(Location point) {
      new Line(nextLineStarts, point, canvas);
      nextLineStarts = point;
   }

The first line of the method constructs a new object from class Line with the parameters nextLineStarts, point, and canvas. In Grace we would have constructed this by writing line.from(nextLineStarts) to (point) on (canvas). Notice the same information is provided to the class to initialize the new object, but in Grace, the different parts of the method name make it easier for the programmer to remember what parameter is required in that slot.

The second line of the method body updates the value of the instance variable nextLineStarts with the value of formal parameter point. Recall that this update uses = rather than :=.

Sadly (at least from my point of view), Java does not impose indenting restrictions on the programmer. A statement may be on one line or may drip over to as many lines as necessary without requiring continuation lines to be more indented than the first. So how does Java know when a statement is over? The key is the placement of a ; after statements. Thus you'll see a ; after both lines of onMouseDrag. However you do not terminate a statement with a ; if the semicolon would appear just before or after an opening { or just after a }. Thus the method header above is not terminated with a ;, nor is the last line, which ends with }. In practice this means that statement that include blocks never have the blocks terminated with a ;.

I should note that while Java doesn't care if you don't indent your program properly, the TA's and I do! Generally, we won't even look at a program that is not indented consistently, whether to help you or to grade it. I urge you also to take advantage of Eclipse's "Format" tool, which is available under the "Source" menu.

Main programs like the one above that use the objectdraw library generally have a begin method that is used to initialize variables, but this program is too simple to require one. We'll seen an example later.

The only thing we have not yet talked about is the main method at the bottom of the class:

   public static void main(String[] args) {
      new Scribble().startController(400,400);
   }

As with the hello world program, this is the method that starts the program. When you tell Java to execute your program as an application, it looks for a main method with exactly this signature and then begins executing it. In this case it creates an object from our Scribble class and then immediately sends it a startController method request. This is like a combination of our startGraphics method that we place at the end of the main program and the graphicApplication.size(400,400) that we inherit from in Grace. In the example above, it creates a n object with a 400 by 400 pixel window and the instance variables and methods specified in the class. In this case it will react to mouse presses and drags by drawing straight lines.


Pre-Defined Data types in JavaTopHello World!Using objectdraw