# Graphical environment

An essential first step is to learn how to use a graphical environment to **draw particles on the screen**. 
The graphical environment may be thought of as a program that simulates the so-called **penwriter** ({numref}`penwriter`), which was used in the early days of computers to draw graphs or other outputs.  
Currently, this has a new, more complex use in 3D printers.

```{figure} images/penwriter.jpg
---
height: 300px
name: penwriter
---
The penwriter has a pen which can be moved by the computer in horizontal (`x`) and vertical (`y`)
directions. Different pens with different colours and sizes can be used to draw lines or dots on a sheet of paper placed in the device. <small>*[Chiffre01](https://commons.wikimedia.org/wiki/File:Plotter_printer_homemade_crop.jpg), [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0)*, Wikimedia Commons
</small>
```

## NaNoPy package

For this course, a graphical Python package called NaNoPy was designed. It is included in the `pf-env` environment (see env.yaml), so it is already installed for you together with its dependencies.

To use the graphical environment, you will need to import the NaNoPy package in your Python scripts:

```
from NaNoPy import Canvas, Color, Writer
```

## Using graphical environment

### Canvas: Graphical window
As you want to make biomolecular simulations on your computer, the penwriter paper is effectively replaced by a **graphical window** of your computer. You can think of a graphical window as a canvas for drawing. To define a window that is 640 pixels wide and 360 pixels high, and carries the title "title" in the top bar of the screen, we use the following Python code:

```
x_size = 640
y_size = 360
screen = Canvas("title", x_size, y_size)
```

With this code, the graphical window is stored in the variable named `screen`. 
As you can see in the code, when declaring a graphical window, the horizontal (`x`) and vertical (`y`) sizes of the window need to be defined. 
Optionally, two additional arguments can be added: `xpos` and `ypos`. 
These arguments are the coordinates of the upper left corner of the window on your monitor. 
E.g., the coordinates (`xpos = 0`, `ypos = 0`) will place your window in the top left corner of your monitor; as default, coordinates in which the graphical window is displayed is the middle of your screen.

### Pen: Writer

The writer, used for drawing, needs to be declared in a similar fashion to the graphical window. You can think of the writer as a **pen with which you draw on a canvas**. 
To define a variable named `pen`, with the canvas on which it draws being the graphical window 
`screen` as declared above, we write:

```
pen = Writer(screen)
```

The `pen` can be used to draw a pixel of a specific colour on the `screen`, and takes three arguments: 

```
pen.draw_pixel(x, y, c)
```

The first two arguments are the `x` and `y` coordinates of the pixel. Notice that the `(0, 0)` 
coordinate is the lower left corner of the screen. The third argument gives the colour (e.g., 
`Color.yellow`).

### Updating the screen

For the drawings made with the `Writer` to be visualised in the graphical window, it is 
necessary to **update the screen** and **keep the window open** when your program is finished. You can do this by using the following code:

```
screen.update()
screen.keep_window()
```

These lines of code, together with the above graphical window and writer, allow you to execute a program that draws on the screen of your computer.

### Example: Drawing a horizontal line

Task: write a program that draws a horizontal red line by using the writer’s `draw_pixel` function.

```
from NaNoPy import Canvas, Color, Writer

x_size = 640
y_size = 360
screen = Canvas("title", x_size, y_size)
pen = Writer(screen)

y = y_size / 2

for x in range(x_size):
    pen.draw_pixel(x, y, Color.red)

screen.update()
screen.keep_window()
```

```{admonition} AI note
:class: ai
As we just saw, in this chapter we are using a package called NaNoPy. Unlike numpy or pandas, this package is designed for this course and difficult to find online help or guidance. Similar to this package, there will be many other custom tools/packages that you will need to use in the future.  Therefore, we suggest you to be especially critical while you get support from LLMs.
```

```{exercise}
:class: dropdown
First you can test if your LLM is aware of the package we will use: Do this by asking the AI about the NaNoPy package along with information on the `Writer` and `Canvas` classes. Compare the definition and input/output arguments given by your LLM against the definition of these classes in NaNoPy. You can see the NaNoPy definition of the classes by selecting the class's name in the script, right-clicking and choosing "Go to definition".
```
