public class MyFile { public static void drawBox() { Draw.window(600, 400); Draw.setTitle("My Drawing #1"); Draw.setColor(255, 255, 0); Draw.setFill(true); Draw.rectangle(100, 100, 400, 200); Draw.setColor(0, 0, 0); Draw.text(300, 200, "Hello"); } }Rules:
file | public class { method-declaration ... } |
method-declaration | public static return-type method-name() { statement ... } |
statement |
file-name.method-name();
System.out.println(expression);
if (boolean-expression) { statements } else { statements } return expression; while (boolean-expression) { statements } variable = expression; type variable; |
int answer; answer = Integer.parseInt("42");Methods: int parseInt(String textWithNumber)
int yourNum; int myNum; System.out.println("You pick a number"); yourNum = Integer.parseInt(Util.input()); myNum = Util.random(1, 10); System.out.println("I picked the number " + myNum);Methods: String input()
Draw.window(600, 400); //600 pixels wide, 400 pixels tall Draw.setTitle("My Drawing #1"); Draw.setColor(255, 255, 0); //yellow Draw.setFill(true); Draw.rectangle(100, 100, 400, 200); //top left at (100, 100) Draw.setColor(0, 0, 0); //black Draw.text(300, 200, "Hello"); //centered at (300, 200)Methods: void window(int width, int height)
int x; int y; Grid.window("mypicture.jpg"); x = 0; while (x < Grid.getWidth()) { y = 0; while (y < Grid.getHeight()) { Grid.setColor(x, y, Grid.getRed(x, y), 0, 0); y = y + 1; } x = x + 1; }Methods: void window(int width, int height)
public static void turnAround() { Robot.turnLeft(); Robot.turnLeft(); }Methods: void move()