Introduction to Computer Graphics and Java Programming for Artists


School of the Museum of Fine Arts ::: Continuing Education

George Aroush, instructor


Lecture One -- Introduction; Using Eclipse; Compiling in Java; Simple Variables


 

Readings

  1. The handouts named:
  2. Sample Programs in Batch One:
  3. "Java How to Program" by Paul J. Deitel, and Harvey M. Deitel

 

Exercises

    Do before our next lecture

    1. Log into Microsoft Windows, start Eclipse, write a simple "Hello, Java" type of program, compile and run it. See the handout Compiling your First Java Program
    2. Write a program that prints your name vertically -- so that when you run the program your name appears like this on the screen:
      G
      e
      o
      r
      g
      e
    3. (optional) Write a program that uses System.out.print() statements and many characters on the PC keyboard to create a complex design or a drawing.
    4. Write a program that counts from 1 to 5. Use a variable and System.out.print() to print its value. Make the variable change its value between System.out.print()'s to get the count.
    5. Change the previous program so it counts down from 5 to 1.
    6. Write a program that uses a variable and prints out its value as it doubles several times.
    7. Change the previous program so that the variable starts with a fairly large value and is halved several times, printing each value.

Back to the top of this page. Back to main Index

 

Startup

Class Hours

Tue: 6:30 PM to 9:30 PM -- Lectures
Thu: 6:30 PM to 9:30 PM -- Workshops

Warning

This course is HARD! It is not targeted for someone who have not had previous computer experience. Don't take it just to fill a block in your schedule or because you want to learn something about computers, graphic programming or Java. Success in this course requires more motivation -- you must have strong reasons to want to learn a lot of very technical, abstract and unreasonably nit-picky material. If you don't have strong reasons, take Art-Making and Design for the Internet: Intermediate to Advanced instead or 3-D Computer Animation Still here? OK. You are obviously willing to put in a lot of time and hard work to learn how to take full control of a complex machine and program computer graphics and animation on the Web!

Brief Description

This course will combine lectures, demonstrations and workshop time to teach students the basics of Java programming language and computer graphics using Microsoft Windows. Starting with simple 2D shapes, students will learn to control randomness and repetition to create more and more complex compositions, animation and special effects. By the end of the course, students should be able to generate and transform 3D "wire-frame" and solid 3D objects and create simple animations that can be viewed using any Java enabled browser.

Class Attendance & Assignments

Students must attend every class for its entirety -- specially lectures. Lecture begins at 6:35 PM sharp. If a student falls behind, it is hard for her/him to catch up. Assignments will be given and must be completed. Doing the assignments is the only way to learn programming and to succeed in this class. It should be possible to do the assignments during workshop hours but you should set some time aside off class hours for some readings. Students are encouraged to do independent projects.

Reading

Purchase of a Java programming textbook (" Java How to Program" Paul J. Deitel, and Harvey M. Deitel) is required. 20 to 30 pages of reading will be assigned each week. A computer graphics textbook, ("Computer Graphics : Principles and Practice" James D. Foley (Editor), Hughs, John Hughes, van Dam, Feiner) is recommended. Handouts and sample programs (via URLs on the WWW) will be given as well. You should read everything starting with the handouts first.

How to Submit Exercises

Submit the source code (the ".java" file) containing the name of the program, the date, a program description and your full name in a /* comment */ . You can email me the source code or I can FTP (File Transfer Protocol) to my folder when you are done (you have to bring this to my attention in order for us to do so).  My email address is: george@aroush.net. You should create folders/directories and copy the given weeks exercise into its folder. I'll email and/or print your ".java" files and write reactions and suggestions and give them back to you in about a week as needed.

Grades and Credits

Grades will be either letter or pass/fail; your choice, (TUFTS students must get a letter grade). Grades are based on the exercises and a project, participation in class, extra work and originality. Programming ability and aesthetics will be given equal weight.

Success

Success in this course requires several old-fashioned virtues: self-discipline, orderliness, determination, and hard work. Curiosity, a sense of play and enthusiasm are equally essential. Get lots of sleep the nights before the class. You are here to learn, so get involve, ask questions and take advantage of it.


Back to the top of this page. Back to main Index.

 

What is a Computer Language?

Low-Level Languages

The earliest computers were programmed in Machine Language (ML) -- switches were set on and off in patterns the machine could understand as "recipes" for what it should do. If "1" means ON and "0" means OFF then 11110010 10010110 might mean: "Read the contents of memory location 150." It was hard to program these machines, and early programmers had reputations usually given to high priests. Later, Assembly Language was born. This was a program that could read quasi-English text and convert it into Machine Language. If an Assembler were given a statement like:

SUB     BX, $1H
MOV     AX, $150

It could translate it into 11110010 10010110. Assembly Language still required extremely detailed and primitive specifications for what the machine was to do; programming in Assembly Language has been compared to building a universe an atom at a time.

High Level Languages

The first high level languages were FORTRAN and COBOL. Here is a sample FORTRAN:

DO i = 1, npts
    y(i) = .3 * i
    x(i) = (y(i) * y(i)) / 2.
END DO

and a sample C code:

int i;
for (i = 0; i < 10; i++)
{
    printf("Counting: %d", i);
}

FORTRAN, COBOL, BASIC, C, C++ and many other high-level languages were much more English-like. A statement in FORTRAN might say to repeat some action a certain number of times. This would be sent to a Compiler. A compiler is a program that would read this English like language and other statements and convert them directly into Machine Language. Typically, one statement in a high-level language will generate three to six machine language commands.

The Java Programming Language

Java as Sun puts it:

"Is a simple, robust, object-oriented, platform-independent multi-threaded, dynamic general-purpose programming environment. It's best for creating applets and applications for the Internet, intranets and any other complex, distributed network."

Java originated as part of a research project to develop advanced software for a wide variety of networked devices and embedded systems. The goal was to develop a small, reliable, portable, distributed, real-time operating environment. When the project started, C++ was the language of choice. But over time the difficulties encountered with C++ grew to the point where the problems could best be addressed by creating an entirely new language environment. Design and architecture decisions drew from a variety of languages such as Eiffel, SmallTalk, Objective C, Cedar/Mesa and C++. The result is a language environment that has proven ideal for developing secure, distributed, network-based end-user applications in environments ranging from networked-embedded devices to the World-Wide Web and the desktop.

Developing your computer graphics and animation using Java results in software that is portable across multiple machine architectures, operating systems, and graphical user interfaces. With Java, your job as a computer artists or programmer is much easier -- you focus your full attention on the end goal of developing a computer art or software rather than the underlying operating-system or hardware.

Programs

A computer program is a sequence of statements that the computer executes in order. Some statements can cause parts of circumstances and skip to some other part of the program. Computers often seem to be intelligent, but truly, they are not. Rather, they are like very powerful, totally obedient, somewhat brain-damaged servants -- the kind you tell to go 100 yards in a particular direction and he'll walk over a cliff! When you program a computer, you are forced at times to think like this brain-damaged dolt -- absolutely literally and logically -- to avoid catastrophe. This is a small price to pay for the benefits to be gained.

Computers and Arts

The payoff is multi-fold. As an artist programming a computer you are essentially designing powerful artistic tools -- this tradition of designing your own tools goes back through Leonardo to the original cave painters in Lascaux. You will be able to make exactly what you need. Computer programming has its own aesthetic. Some of the processes used in computer bear an uncanny resemblance to processes used in Modern Art -- repetition in Minimalism, randomness in Dada and Surrealism, to name two. Working with these processes will broaden your own aesthetic. There is a fairly rapid cycle of struggle and reward: you write a program, it doesn't quite work, you get involved in solving the problem, you get it to work, you fine-tune it, and it's done! This is very satisfying. If you enjoy solving puzzles, you will enjoy programming. The computer also collaborates with the artist creatively; the program doesn't always do exactly what was envisioned -- sometimes the unforeseen result is better or suggests a previously unsuspected direction to work in. As any other medium, the artist and the computer engage in a dialogue. The result can be synergistic: the whole is greater than the sum of its parts.


Back to the top of this page. Back to main Index.

 

Working with Eclipse

Running Eclipse

After logging into Microsoft Windows, run Eclipse by clicking on it from Start -> Programs -> Eclipse menu. This will startup the Eclipse Platform. The Eclipse Platform has three key units/programs that are key to all development environments; those are: a Java editor, a Java compiler and a Java debugger. It is in this environment where we will develop our Java programs. The editor and the compiler are what we will use all the time. The editor is very similar to any other Windows based text editor with additional features for developers' need. If you are familiar with Microsoft Word, than the Eclipse editor should be very familiar for your. If not, than it is strongly recommended that you spend some time to get yourself familiar with the Eclipse Java source code editor.


Back to the top of this page. Back to main Index.

 

Compiling Your First Java Program

Creating the Source File

After running Eclipse (see Working with Eclipse) and the editing window appears, you will need to set it up for building a Java program. To do this, got to the File menu and select File -> New -> Project.... A dialog box will appear with the title New Project. On the left pan select Java by clicking on it once. On the right pan select Java Project then click on the Next button. A new dialog box appears with the title New. In the section for Project name type a name for your Java project; for this exercise type: Week One.

Once Eclipse is done creating the project, go back to the File menu and select File -> New -> Class. A dialog box will appear with the title New. In the Name section type a name for your Java class; for this exercise type HelloJava then click on the Finish button. This will be the name of the Java program that you will execute. The name cannot start with a number and it cannot contain spaces (and few other reserved characters.)

Once Eclipse is done processing your request, it will create a simple empty Java code for you to edit where you will add your own code. Assuming that you called your class HelloJava, the Eclipse generated code will look like so:

/**
 * @author
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class HelloJava
{

}

Edit this code so that it looks like so:

/**
 *  Program:    [Program title]
 *  Purpose:    [Program description (brief)]
 *  Date:       [Original creation date]
 *  Change Log: [Indicate changes if this is an ongoing project]
 *
 *  @author [Your Name]
 *
 *  Full description of Program:
 *      The MyProjectName class is the simplest Java program that you can 
 *      ever have that does nothing!
 */
public class First
{
    public static void main(String [] args) 
    {
        System.out.print("Hello, Java!");   /* traditional first statement */
    }
}

Once you are done typing your code you must save it in order to compile and run it. From the File menu select Save HelloJava.java.

Building/Compiling the Java Source Code

Before you can run your program, it must be compiled. This is done from the menu Project -> Rebuild All. If there are any errors in your code, they will be listed in the Task window as well as underlined in a red color. You must fix those before you can run your program.

Running the Program

To run your program, select Run -> Run As -> 1 Java Application. The output of your program will be displayed in the Console window.


Back to the top of this page. Back to main Index.

 

Explanations of the First Batch of Java Programs

Explanation of First.java

/** */

The symbol /** is the start of a documentation comment - a block of text that will be ignored by the compiler but used by JavaDoc. It is here to make the program text more readable by humans. The comment continues until the opposite symbol */ appears.

If you don't want your comment to be processed by JavaDoc, then use /*.

/**
 *  Program:    [Program title]
 *  Purpose:    [Program description (brief)]
 *  Date:       [Original creation date]
 *  Change Log: [Indicate changes if this is an ongoing project]
 *
 *  @author [Your Name]
 *
 *  Full description of Program:
 *      The MyProjectName class is the simplest Java program that you can 
 *      ever have that does nothing!
 */

The above lines are a typical comment at the start of a program. As you can see they provide some key information about the program without having to look at the code.

public class First

Java is an Object Oriented Programming language (OOP).  Here class is Java's way of defining an object. When we create a class, we must give it a name. In this case we called our class First.

public static void main(String [] args)

Every Java program has at least one method. This is the start of the program, main(), which is the start of every Java program. The words public, static, void and String [] args are hard to explain at this time. For now just know that you must have them as you see them, later we will see their meaning. The parenthesis ( ) following main have to follow every method; sometimes they enclose values the function will use (more about this later.)

{

This left curly brace makes the start of the method main(). Pairs of curly braces are generally used to mark blocks, loops, and other statements in Java

System.out.print("Hello, Java!");

This statement prints "Hello, Java!" on the screen. print() is a built-in Java method belonging to the System.out object that comes standard with the Java language. It is followed by parentheses which surround the information it is to print. In this case, it will print a string of series of typed characters, which must be enclosed within quotation marks. Like all statements in Java, this program line must end with a semicolon ;

/* traditional first statement */

This comment explains the meaning or intention of the program line, for the reader. You should probably include a comment every three or four program lines, or depending on the complicity of the line/statement.

}

This right curly brace marks the end of the method main().

Summary of what is needed for a Java Program

All Java programs must have the followings:

  1. At least one class.
  2. At least one method named main().
  3. A left curly brace { to indicate the start of the method.
  4. One or more program statements, each of which ends with a semicolon;
  5. A right curly brace } to show the end of the method.

Explanation of Second.java

This program is the same as First.java with the addition of the new-line character or \n (look at the full text of the program.) When the program First.java was run, it printed "Hello, Java!" all right, but if we add another print() statement to print something else in the next line, it will get printed next to the old line. For example if we have the following:

public class Second
{
    public static void main(String [] args)
    {
        System.out.print("Hello, Java!");
        System.out.print("This is my first Java program");
    }
}

you will end up with:

Hello, Java!This is my first Java program

It's better to get Java to go on to the next line, so we need to print a so-called control character called new-line. This is a carriage return (a return of the cursor to the left of the screen, like a typewriter carriage return) and a line feed a movement of the cursor to the next line. So retype the above program to look like this:

class Second
{
    public static void main(String [] args)
    {
        System.out.print("Hello, Java!\n");
        System.out.print("This is my first Java program\n");
    }
}

The output of the last print() will look like:

Hello, Java!
This is my first Java program

Explanation of Variable.java

This program demonstrates the use of variables in a Java programming. A variable is a small area of memory that is given a name. Numbers can be written to or stored in this area of memory and read back or retrieved from it. The value in the variable can be changed, which is why it's called a variable. There are several kinds of variables, each of which is a different size: char, int, float, and double are the most common. We will start with int variables, which can store integers or whole numbers between -2,147,483,648 to 2,147,483,647 inclusive (more about data type in upcoming lectures.) The Java compiler needs to know which variables we are going to use so it can grab appropriate space from computer memory to store them. In Variable.java this is done right after the { at the start of main():

public static void main(String [] args)
{
    int     v;

First, the programmer must declare which variables will be used. int is the data type -- an integer or whole number. v is the identifier -- a label for the area in memory where the integer value is stored. In this sample program, the compiler is told to find space for an integer variable and give it the name v. v is the identifier for the variable. Java identifiers must begin with a letter, and can be made of letters, numbers and a few punctuation marks:

_

is common. DO NOT use the following in your variable name as they are not allowed:

.   ,   :   ;   "   '   =   +   (   )   *   
&^   %   $   #   {   }   [   ]   @   !   -

Java pays attention to upper and lower case. FOO, foo, Foo, fOO and foO are all different identifiers. Java has a few reserved key words which can not be used for identifiers. The reserved words are:

abstract        default         implements      static  
void            boolean         do              import  
super           byte            double          instanceof  
switch          case            extends         int 
synchronized    catch           final           interface   
throw           char            finally         long
throws          class           float           native
transient       const           goto            package 
try

DO NOT use them for identifiers.

v = 13;     /* Give 'v' a value of 13, in other word make v equals 13 */

Here, = is used to store the number 13 in the variable v. The next statement asks Java to show us the value of v.

System.out.print("v = " + v + "\n");

The + in the print() statement tells Java to take the value of v and convert it into a string representation and then add it to the end of the string "v = " (what ever is on the left of +. In effect the above statement gets converted into the following:

  1. v gets converted to its string representation of "13"
  2. The converted string "13" gets appended to the end of the string: "v = " this in effect gives us a new string: "v = 13".
  3. The string "\n" gets appended to the string: "v = 13" which was generated above, to give us our final string: "v = 13\n".
  4. Finally, this newly constructed string: "v = 13\n" gets passed to the method print() for display.

If more than one value needs to be printed, all the identifiers are listed, separated by + in the order that we want them to appear, outside the quotes:

System.out.print("u = " + u + "v = " + v + "w = " + w + "\n");

Explanation of VarsAndArith.java

This program uses three variables u, v and w, and also shows how to do simple arithmetic. Java uses + and - for addition and subtraction, * for multiplication and / for division.

int     u, v, w;    /* declare 3 integer variables */

If we have more than one integer variable, we can declare them after a single int statement, separating the identifiers with commas:

u = 8;      /* give 'u' a value of 8 */
v = 13;     /* give 'v' a value of 13 */
w = u + v;  /* take the value of 'u', which is 8, add */
            /* the value of 'v', which is 13, and give */
            /* the result, 21, as a value to 'w' */

Here, we see addition in action. Java takes whatever appears to the right of =, performs all the calculations, and puts the resulting value into the variable to the left of the equal symbol (=):

w = w + v;  /* Take the current value of 'w' 21, add the */
            /* value of 'v', 31, and use the result 34 as */
            /* a new value for 'w' */

Here we see something a little stranger; the same identifier appears on both sides of the =. The old or current value of w is used in the calculation, and the result is stored back in w, replacing the old value.

u = v * w;  /* Same as before, but multiplication is done */
v = v / 17; /* Division */
w = w - v;  /* Subtraction */

When all four arithmetic operators are mixed together, for example:

w = w + u * v;

Java proceeds from left to right, and does the multiplication and divisions first, then goes back and does additions and subtraction. So the statement above adds u * v to w If one wanted Java to add w + u first and then multiply the result by v, parentheses must be used:

w = (w + u) * v;

Java will perform the calculations inside parenthesis first.


Back to the top of this page. Back to main Index.

 

Sample Programs -- Batch One

First.java

/**
 *  Program:    First.java
 *  Purpose:    Sample program for first effort
 *  Date:       1/1/1998
 *  Change Log: None
 *
 *  @author George Aroush
 *
 *  Full description of Program:
 *      This Java program will display the text "Hello, Java!".
 */

public class First
{
    public static void main(String [] args) 
    {
        System.out.print("Hello, Java!");
    }
}

Second.java

/*
 *  Program:    Second.java
 *  Purpose:    Sample program for 'improved' first effort
 *  Date:       1/1/1998
 *	Change Log:	None
 *
 *  @author George Aroush
 *
 *	Full description of Program:
 *      Improved First.java program -- prints statement on screen, with 'new line'
 */

public class Second
{
    public static void main(String [] args)
    {
        System.out.print("Hello, Java!\n");
        System.out.print("This is my first Java program\n");
    }
}

Variable.java

/*
 *  Program:    Variable.java
 *  Purpose:    Sample program for first effort
 *  Date:       1/1/1998
 *	Change Log:	None
 *
 *  @author George Aroush
 *
 *	Full description of Program:
 *      This Java program will display the text "Hello, Java!".
 */
public class Variable
{
    public static void main(String [] args)
    {
        int     v;  /* 
                     * First, the programmer must 'declare' which variables will be 
                     * used.  'int' is the data type -- an integer of whole number 
                     * between -2,147,483,648 and 2,147,483,647 'v' is the 
                     * identifier -- a label for the area in memory where the 
                     * integer value is stored
                     */	

        v = 13;     /* Give 'v' a value of 13, in other word make v equals 13 */

        System.out.print("v = " + v + "\n");

        v = 21;                 /* 
                                 * Give 'v' the value of 13; over writing the old 
                                 * value -- change the value of 'v' 
                                 */

        System.out.print("Now, v = " + v + "\n");   /* Re-print the value of 'v' */
    }
}

VarsAndArith.java

/*
 *  Program:    VarsAndArith.java
 *  Purpose:    More on variables and arithmetic
 *  Date:       1/1/1998
 *  Change Log: None
 *
 *  @author George Aroush
 *
 *  Full description of Program:
 *      Further demonstration of integer variables, and use of arithmetic
 */

public class VarsAndArith
{
    public static void main(String [] args) 
    {
        int     u, v, w;    /* declare 3 integer variables */


        u = 8;              /* give 'u' a value of 8 */
        v = 13;             /* give 'v' a value of 13 */

        w = u + v;          /*
                             * take the value of 'u', which is 8, add the value of 
                             * 'v', which is 13, and give the result, 21, as a 
                             * value to 'w'
                             */

            /* display the values of u, v & w */
        System.out.print("u = " + u + "v = " + v + "and w = " + w +  "\n");

        w = w + v;          /* 
                             * take the current value of 'w' (21), add the value 
                             * of 'v' (13), and use the result (34) as new value 
                             * for 'w'
                             */

        System.out.print("Now, w = " + w + "\n");

        u = v * w;          /*
                             * '*' is used for  multiplication in most programming 
                             * languages.  Multiply the values of 'v' and 'w' and 
                             * give the product to 'u' 13 * 34 = 442
                             */

        System.out.print("Next, u = " + u + "\n");

        v = u / 17;         /* 
                             * '/' is used for division.  Divide the value of 'u' 
                             * by 17, giving the result to 'v' 
                             */

        System.out.print("Then, v = " + v + "\n");

        w = w - v;          /*
                             * same old '-' Subtract the value of 'v' form the 
                             * present value of 'w' and give the result as a 
                             * new value to 'w'
                             */

        System.out.print("Finally, w = " + w + "\n");
    }
}

Back to the top of this page. Back to main Index.