> For the complete documentation index, see [llms.txt](https://easy-draw.joemazzone.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easy-draw.joemazzone.net/java-curriculum/unit-1-using-easy-draw-objects.md).

# Unit 1: Using Easy Draw Objects

In this unit students learn to **create objects** and **call methods** by drawing with Easy Draw. Every shape is an object, which makes classes, constructors, references, and methods visible on the screen.

{% hint style="info" %}
**Teacher notes**

* **Time:** 11 class periods: 6 lessons (1 class each) and 5 classes for the unit project
* **AP Computer Science A:** Primitive Types and Using Objects (creating objects, calling void and non-void methods, the `Math` class).
* **Setup:** each student needs `EasyDraw.java` in the same folder as their program. See [Java Setup and Install](/java/setup-and-install.md).
  {% endhint %}

## Learning Objectives

Students will be able to:

1. Explain the difference between a class and an object.
2. Create objects with `new` and a constructor, passing arguments in the right order.
3. Store a reference to an object in a variable of the class's type.
4. Call `void` methods (setters) and non-void methods (getters) on an object and use the returned value.
5. Use `int` and `double` variables and arithmetic expressions to place shapes.
6. Use `Math.random()` and `Math.sqrt()` in a drawing.

## Lesson 1.1: Classes and Objects

A **class** is a blueprint. An **object** is one thing built from that blueprint. `EasyDraw.Circle` is a class. Each circle on the canvas is an object.

```java
public class Main {
    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        EasyDraw.Circle sun = new EasyDraw.Circle(300, 300, 100);
        EasyDraw.Circle moon = new EasyDraw.Circle(500, 100, 40);

        EasyDraw.end();
    }
}
```

Break the line `EasyDraw.Circle sun = new EasyDraw.Circle(300, 300, 100);` into parts:

* `EasyDraw.Circle` - the type of the variable (the class).
* `sun` - the variable that stores a **reference** to the object.
* `new` - creates a new object.
* `EasyDraw.Circle(300, 300, 100)` - calls the **constructor** with arguments: center x, center y, and radius.

**Discussion:** We made two objects from one class. What is the same about them? What is different?

## Lesson 1.2: The Coordinate Plane and Constructors

The canvas is 600 x 600 pixels. (0, 0) is the **top-left** corner and y grows **down**. Turn on the **Grid** button on the Easy Draw page and hover over the canvas to see coordinates.

Each constructor expects its arguments in a certain **order**:

| Constructor                                                 | Arguments in order              |
| ----------------------------------------------------------- | ------------------------------- |
| `new EasyDraw.Rectangle(x, y, width, height)`               | top-left corner, then size      |
| `new EasyDraw.Circle(centerX, centerY, radius)`             | center, then radius             |
| `new EasyDraw.Oval(centerX, centerY, width, height)`        | center, then size               |
| `new EasyDraw.RegPolygon(nsides, centerX, centerY, radius)` | number of sides, center, radius |
| `new EasyDraw.Polygon(x1, y1, x2, y2, x3, y3, ...)`         | each point                      |
| `new EasyDraw.Line(x1, y1, x2, y2)`                         | start point, end point          |
| `new EasyDraw.Text(centerX, centerY, text)`                 | center, then the text           |

**Practice:** Swap the order of two arguments in a constructor call. What happens? What happens if you leave one out? (The compiler error is the lesson!)

## Lesson 1.3: Calling Methods

**Methods** are what an object can do. Setters are `void` methods: they change the object and do not return anything.

```java
public class Main {
    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        EasyDraw.Rectangle box = new EasyDraw.Rectangle(200, 200, 200, 100);
        box.setColor("orange");
        box.setBorderColor("black");
        box.setBorderWidth(5);
        box.rotate(30);

        EasyDraw.end();
    }
}
```

Getters are **non-void** methods: they **return** a value we can store or use.

```java
public class Main {
    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        EasyDraw.Circle ball = new EasyDraw.Circle(300, 300, 50);

        double r = ball.getRadius();
        System.out.println("The radius is " + r);

        ball.setRadius(ball.getRadius() * 2);
        System.out.println("Now the radius is " + ball.getRadius());
        System.out.println(ball);

        EasyDraw.end();
    }
}
```

**Discussion:** `ball.setRadius(ball.getRadius() * 2);` uses a non-void method call as the argument of a void method call. Which method runs first?

**Practice:** Draw a square, then use its getters to place a circle exactly on its top-right corner.

## Lesson 1.4: Variables and Expressions

Variables and arithmetic let shapes be placed **relative** to each other. Change `x`, `y`, or `size` and the whole snowman moves and resizes together.

```java
public class Main {
    public static void main(String[] args) {
        EasyDraw.loadCanvas("skyblue");

        double x = 300;
        double y = 450;
        double size = 80;

        EasyDraw.Circle bottom = new EasyDraw.Circle(x, y, size);
        EasyDraw.Circle middle = new EasyDraw.Circle(x, y - size * 1.6, size * 0.75);
        EasyDraw.Circle head = new EasyDraw.Circle(x, y - size * 2.8, size * 0.5);

        bottom.setColor("white");
        middle.setColor("white");
        head.setColor("white");

        EasyDraw.end();
    }
}
```

**Discussion:** What happens with `int size = 80;` and `size / 3`? Compare it with `double size = 80;`. (Integer division!)

## Lesson 1.5: Colors and Strings

Colors can be a `String` (a name or a hex value) or three `int` values for red, green, and blue.

```java
shape.setColor("tomato");
shape.setColor("#FF6347");
shape.setColor(255, 99, 71);
```

Use the **Color Picker** button on the Easy Draw page to find RGB and hex values.

Text objects hold a `String`, and `+` builds new strings:

```java
String name = "Ada";
int age = 15;
EasyDraw.Text label = new EasyDraw.Text(300, 50, name + " is " + age + " years old");
label.setSize(28);
```

## Lesson 1.6: The Math Class

`Math.random()` returns a `double` from 0.0 up to (not including) 1.0. Multiply and cast it to get a random whole number.

```java
public class Main {
    public static void main(String[] args) {
        EasyDraw.loadCanvas("black");

        int x = (int) (Math.random() * 600);
        int y = (int) (Math.random() * 600);
        int red = (int) (Math.random() * 256);

        EasyDraw.Circle star = new EasyDraw.Circle(x, y, 20);
        star.setColor(red, 255, 0);

        double distance = Math.sqrt(Math.pow(x - 300, 2) + Math.pow(y - 300, 2));
        EasyDraw.Text info = new EasyDraw.Text(300, 580, "Distance from center: " + (int) distance);
        info.setColor("white");

        EasyDraw.end();
    }
}
```

**Practice:** Run the program several times. Then draw a line from the center of the canvas to the star.

## Unit Project: Object Portrait

Students draw a portrait (of themselves, a character, an animal, or a robot) built only from Easy Draw objects.

**Requirements:**

* At least 12 objects using at least 5 different classes (`Rectangle`, `Circle`, `Oval`, `RegPolygon`, `Polygon`, `Line`, `Arc`, `Text`).
* Every shape is placed using `x`, `y`, and `size` variables, so changing those three values moves and resizes the whole portrait.
* At least 3 different setters and at least 2 getters are used.
* At least one color is RGB and one is hex.
* Uses `Math.random()` for at least one detail (like a random shirt color).
* A `Text` object shows a title built with string concatenation.

| Category          | 4 - Exceeds                                      | 3 - Meets                       | 2 - Approaching               | 1 - Beginning     |
| ----------------- | ------------------------------------------------ | ------------------------------- | ----------------------------- | ----------------- |
| Objects           | 15+ objects, 6+ classes                          | 12+ objects, 5+ classes         | Fewer objects or classes      | Very few objects  |
| Methods           | Setters and getters used together in expressions | 3+ setters and 2+ getters       | Only setters                  | No methods called |
| Relative position | Whole portrait scales correctly with `size`      | Portrait moves with `x` and `y` | Some shapes use fixed numbers | All fixed numbers |
| Code quality      | Clear names, comments, and organization          | Comments explain each part      | Few comments                  | Hard to read      |
| Design            | Original, detailed, and polished                 | Complete and recognizable       | Unfinished                    | Hard to recognize |
