Java Tips for Beginners (5): arrays, input&output

Arrays

// declare an array:

int[] numbers = {1,2,3,4,5}

// or

int[] numbers = new int[5];

for (int i = 0; i < 5; i++){

numbers[i] = i+1;

}

// shorthand way to iterate over the contents

for (int n : numbers){

sum = sum + n;

}

 

Input and Output

API for StdDraw

api.jpg

Color

Color c = new Color(red,green,blue)

white: (255,255,255)

black: (0,0,0)

built-in colors: Color.GREEN, etc.

 

Coordinate system: default  drawing canvas

(0,1)                          (1,1)

 

(0,0)                          (1,0)

 

location of mouse & if mouse is pressed:

double x = StdDraw.mouseX();

double y = StdDraw.mouseY();

boolean isPressed = StdDraw.mousePressed();

 

if a key is typed & what is the key typed:

boolean isTyped = StdDraw.hasNextKeyTyped();

char got = StdDraw.nextKeyTyped();

 

report every 100 ms:

StdDraw.pause(100);

 

Typical animation loop:

while (true) {

  • StdDraw.clear();
  • Draw stuff
  • StdDraw.show(50);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s