9.2. 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 (Fig. 9.1), 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.

../../_images/penwriter.jpg

Fig. 9.1 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. Chiffre01, CC BY-SA 4.0, Wikimedia Commons #

9.2.1. 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

9.2.2. Using graphical environment#

9.2.2.1. 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.

9.2.2.2. 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).

9.2.2.3. 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.

9.2.2.4. 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()

AI note

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.