Java tips

Note: More detailed instructions are on the java directions page. This is mainly extras and code snippets.


The book referenced in class was Java in a Nutshell, available at Quantum Bookstores for about $15.

The best reference online is Sun's java page. This is also how where you can download the development environment used in recitation for free. Just look for the pages about the JDK (java development kit).

Athena resources

On Athena, you must "add java" before running jdk, but then things should work as they did in class.

It was reported that there are other tools for doing java on Athena, thought I have not tried them out myself. The incantation on Athena is:

add java
add studio
add workshop

Code snippets

Here were some sample commands and code snippets that might be useful. All of these have been verified to compile:

g.setColor(new Color(0.5f, 0.5f, 0.5f));

g.drawLine(0, 0, w, h);


int myArray[] = new int[10];
int i;

for(i=0;i<10;i++) {
  myArray[i] = i;

if(t < 0.5) {
else {

int j=0;
while(j<10) {
  j = j+2;

float f = (t+0.1f);
do {
  f = f*2;
} while (f < 1.0);

float q = t;
q = (float)Math.sin(q);
q = (q+1.0f)/2.0f;
g.setColor(new Color(q,q,q));

mas110runner info

Remember that you only have to download and modify the following method in your code:

public void draw(Graphics g, float t) {

Just remove the few lines that are there and add your own commands. If you make new variables before this draw line, they will keep their value throughout the program, variables made after this draw line will be created new each time.

Along with any variables you make, you also have access to the following three variables:

int w = the width of your canvas
int h = the height of your canvas
float favoriteFrame = your favorite value of t, in case we take a snapshot.

Note that you should not reset w and h, but you should reset favoriteFrame if you desire. That is:

Dont do this: w = 200

But feel free to do this:

favoriteFrame = 0.65f

Resetting the t variable

The t variable will not reset, so it is up to you to make sure it stays within the bounds of the assignment. But since java has a floating point mod operator, this is easy. To have the time variable t start over after s seconds, just put the following line of code as the first line of your draw() method:

t = t % s;

So for part three, which asks for you to animate over 1.5 seconds and repeat the animation continuously, you could just add the following line to the beginning of your draw method:

t = t % 1.5f;

Of course, if you want to be more fancy (and add a delay at the end or whatever), that is completely up to you.