> 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/example-code.md).

# Example Code

## Example 1: A Tree!

Here is a simple example drawing a tree with a rectangle and three triangles. The background is set to sky blue with a hex color value, and the triangles are rotated 30 degrees.

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

        EasyDraw.setCanvasColor("#87CEEB");

        EasyDraw.Rectangle trunk = new EasyDraw.Rectangle(250, 300, 100, 310);
        trunk.setColor(160, 82, 45);

        EasyDraw.RegPolygon top = new EasyDraw.RegPolygon(3, 300, 175, 125);
        top.setColor(0, 255, 0);
        top.rotate(30);

        EasyDraw.RegPolygon middle = new EasyDraw.RegPolygon(3, 300, 275, 175);
        middle.setColor(0, 255, 0);
        middle.rotate(30);

        EasyDraw.RegPolygon bottom = new EasyDraw.RegPolygon(3, 300, 400, 250);
        bottom.setColor(0, 255, 0);
        bottom.rotate(30);

        EasyDraw.end();
    }
}
```

![](https://651142499-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRAMSGwqRIdQ2ADsTkz%2F-MRHN5hgZYKKGE0yrPL_%2F-MRHX6zyKRTdaLV-G_Wr%2Ftree.png?alt=media\&token=84101837-80fa-4dde-8c7c-a082ce459dd1)

## Example 2: Property Change and Event Handling

Here is an example of drawing a square and an octagon, then animating them when clicked.

When the square is clicked, it changes color to a new random value and rotates by 10 degrees.

When the octagon is clicked, it changes color to a new random value and rotates by 10 degrees.

```java
import java.util.Random;

public class Main {
    static String[] colors = {"blue", "red", "yellow", "green", "orange", "purple"};
    static Random random = new Random();

    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        EasyDraw.Rectangle square = new EasyDraw.Rectangle(100, 100, 200, 200);
        square.eventSetup("<Button-1>", event -> {
            square.setColor(colors[random.nextInt(colors.length)]);
            square.rotate(10);
        });

        EasyDraw.RegPolygon octagon = new EasyDraw.RegPolygon(8, 400, 400, 100);
        octagon.eventSetup("<Button-1>", event -> {
            octagon.setColor(colors[random.nextInt(colors.length)]);
            octagon.rotate(10);
        });

        EasyDraw.end();
    }
}
```

## Example 3: Moving with the Arrow Keys

The arrow keys move the blue circle. Clicking the circle adds 1 to the score.

```java
public class Main {
    static int score = 0;

    public static void main(String[] args) {
        EasyDraw.loadCanvas();

        EasyDraw.Circle player = new EasyDraw.Circle(300, 300, 25);
        player.setColor("blue");

        EasyDraw.Text scoreText = new EasyDraw.Text(300, 30, "Score: 0");
        scoreText.setSize(24);
        scoreText.setBold(true);

        EasyDraw.canvasEventSetup("<Up>", event -> player.setCenter(player.getCenterX(), player.getCenterY() - 10));
        EasyDraw.canvasEventSetup("<Down>", event -> player.setCenter(player.getCenterX(), player.getCenterY() + 10));
        EasyDraw.canvasEventSetup("<Left>", event -> player.setCenter(player.getCenterX() - 10, player.getCenterY()));
        EasyDraw.canvasEventSetup("<Right>", event -> player.setCenter(player.getCenterX() + 10, player.getCenterY()));

        player.eventSetup("<Button-1>", event -> {
            score = score + 1;
            scoreText.setText("Score: " + score);
        });

        EasyDraw.end();
    }
}
```

{% hint style="info" %}
`score` is a `static` variable of the class, not a local variable in `main`. A lambda can only change variables like this one. See [Event Handling](https://easy-draw.joemazzone.net/java/module-reference/event-handling.md).
{% endhint %}
