Docs - james-bern/CS136 GitHub Wiki

🐮 Cow.java

Cow.java is a simple Java dialect and game library designed to prepare you to program in C

🆎 primitive types

  • Use boolean for truth values, double for real numbers, int for (signed) integers, and char for characters
    • NOTE: do NOT use byte, short, long, or float

🖨️ print

  • PRINT(thing); is like System.out.println(thing); but can actually prints arrays, prints char's like 'A', and prints the 0 char like '\0'

📏 math

  • Number MIN(Number a, Number b); returns the lesser of the two arguments (can be int or double)
  • Number MAX(Number a, Number b); returns the greater of the two arguments (can be int or double)
  • Number ABS(Number a); returns $|a|$ (can be int or double)
  • double SQRT(double a); returns $\sqrt{a}$
  • NOTE: to square a number use a * a
  • int MODULO(int a, int b); is like a % b but works for negative numbers (like Python's % 🐍)
  • int ROUND(double a); rounds $a$ to the nearest integer
  • boolean ARE_EQUAL(double a, double b); checks whether $a \approx b$ (allows for "floating point error"); returns $(|a - b| < \varepsilon)$ for a small $\varepsilon$

🐄 app

  • void beginFrame(); can be used to create a game/app with the classic setup-loop structure

    class ExampleApp extends Cow {
        public static void main(String[] arguments) {
    
            // setup
            double time = 0.0;
    
            while (beginFrame()) {
    
                // loop
                time += 0.033;
                drawCircle(mouseX, mouseY, 32.0, colorRainbowSwirl(time));
    
            }
        }   
    }
    

🎹 keyboard

  • boolean keyPressed(char key); returns whether key was pressed on your keyboard during the previous frame
    • keyPressed('A') returns whether the key labeled A was just pressed on your keyboard
      • NOTE: do NOT use keyPressed('a') (there is no key labeled a on your keyboard)
      • NOTE: for uppercase and lowercase use (keyHeld(SHIFT) && keyPressed('A')) and ((!keyHeld(SHIFT)) && keyPressed('A'))
  • boolean keyHeld(char key); returns whether key is currently held on your keyboard
  • boolean keyReleased(char key);
  • boolean keyToggled(char key); is useful for creating "toggle switches"
    • NOTE: keyToggled('A') will be false if A has never been pressed, true if it has been pressed once, `false if it has been pressed twice, ...

🔑 keys

For most keys, you can use a char, such as 'A' for "the A key" and ' ' for "the space bar." For other special keys, you can use these constants.

  • char ENTER, TAB;
  • char BACKSPACE, DELETE;
  • char CONTROL, COMMAND, ALT, SHIFT;
  • char LEFT_ARROW, RIGHT_ARROW, UP_ARROW, DOWN_ARROW;

🐭 mouse

  • double mouseX; is the x-coordinate of the current mouse position
    • NOTE: $x$ increases as we move from left to right
  • double mouseY; is the y-coordinate of the current mouse position
    • NOTE: $y$ increases as we move from bottom to top
  • boolean mousePressed; is whether your left mouse button was pressed during the previous frame
  • boolean mouseHeld; is whether your left mouse button is currently held
  • boolean mouseReleased;

✏️ draw

Simple drawing functions.

  • void drawLine(double x1, double y1, double x2, double y2, Color color, double thickness); draws a line connecting $(x_1, y_1)$ and $(x_2, y_2)$; thickness is in something like pixels
  • void drawCircle(double x, double y, double radius, Color color); draws a circle with center $(x, y)$ and radius $r$
  • void drawRectangle(double x1, double y1, double x2, double y2, Color color); draws a rectangle with corners $(x_1, y_1)$ and $(x_2, y_2)$
  • NOTE: The style arguments color and thickness are optional (they default to BLACK and a reasonable thickness).

🎨 color

My simple color class. We will just use the constants it provides in our calls to the drawing functions.

  • Color BLACK, BLUE, BROWN, CYAN, GREEN, MAGENTA, ORANGE, PURPLE, RED, YELLOW, WHITE;
  • Color colorRainbowSwirl(double time); is a cyclic rainbow color map.

🖼️ canvas

  • canvasConfig(double left, double bottom, double right, double top, Color color, int maxDimensionInPixels); can be used to set up the canvas coordinate system and size
    • NOTE: the left-bottom corner of the canvas is $(x_\text{left}, y_\text{bottom})$ in "world units"
    • NOTE: the right-top corner of the canvas is $(x_\text{right}, y_\text{top})$ in "world units"
    • NOTE: the canvas is cleared to color each beginFrame()
    • NOTE: the longer side of the canvas is maxDimensionInPixels pixels long on your screen
    • NOTE: the color and maxDimensionInPixels arguments are optional (they default to WHITE and 512)
    • NOTE: calling this function at all is optional (the other arguments default to -256, 256, -256, 256)
      • NOTE: but if you're going to call this function, put it just above while (beginFrame()) { ... }