Off-Screen Drawing and Buffers
To a computer, an image is a set of numbers, with each number specifying the color of one pixel.
A Question Before Reading Further
Earlier work with the Graphics class showed how to draw on a graphical user interface component that is visible on the screen. That raises two natural questions. What if you want to build a drawing somewhere the user cannot see it, inside the computer's memory? And what if the picture you need already exists in a file that you want to load and show? Both questions depend on one fact about how computers handle pictures.
Pause and ask yourself what a computer could possibly store to represent a photograph or a drawing. It cannot keep a picture the way a painter keeps a canvas. Whatever it stores must be something a machine can hold in memory and copy around. Keep that question in mind, because the answer shapes everything that follows.
An Image as Numbers
To a computer, an image is a set of numbers, with each number specifying the color of one pixel. A pixel is one small point of the picture, and the image is the collection of those per-pixel descriptions.
Notice the careful wording in that definition. The numbers specify color, and each pixel gets its own number. The source material does not say how many numbers a single color needs or what scale they use, so this article does not assume any particular encoding. What matters at this stage is the relationship: one pixel, one color described by numbers, and an image equals the full set of those descriptions.
One Number per Pixel
The easiest way to see the idea is a tiny strip of six pixels. The codes 0, 1, and 2 below stand for three different colors. These codes are invented for this illustration to make the idea visible. They are not a real standard, and nothing in a real system needs to use exactly these values.
Read the strip from left to right. Each box is a pixel, and the number under it is the only thing the computer needs in order to know that pixel's color. Two neighboring pixels with the same number have the same color, so pixels 0 and 1 above are the same color. Changing a pixel's color means changing its number, and nothing else about the image has to be rewritten.
The Frame Buffer and the Video Card
The numbers that show the picture on the screen are stored in a part of memory called the frame buffer. The video card reads the frame buffer many times each second and uses those numbers to color the screen. The frame buffer is therefore the single source of truth for the picture the user sees.
The screen does not hold a picture the way a canvas does. What you see is a fresh coloring of the screen, produced from the current contents of the frame buffer each time the video card redraws.
Changing the Screen by Writing Numbers
When a program needs to alter what is shown, it does not reach out and repaint the glass directly. It writes new numbers into the frame buffer. The change becomes visible at the next redraw, a fraction of a second later, when the video card reads the updated numbers and colors the pixels again.
In the diagram, only pixel 2 differs after the write. Its number changed from 0 to 1. Pixel 3 keeps its number and therefore its color. The change to the picture is exactly the change to the numbers, and the viewer sees the new color only after the next redraw has read the frame buffer.
Tracing a Single Pixel Change
Changing one pixel in the six-pixel strip
The strip currently holds the codes 1, 1, 0, 2, 2, 0. A program needs pixel 2 to show color 1. What must change, and where does the change become visible?
- 1Find the pixel
Pixel 2 is the third box from the left, since counting starts at 0. Its current number is 0.
- 2Write the new number
The program writes the number 1 into the position that holds pixel 2. No other number is touched, because each pixel's color is stored independently.
- 3Wait for the redraw
The video card has not yet been told about the change. At its next redraw it reads the frame buffer, finds 1 at pixel 2, and colors that pixel accordingly.
The strip becomes 1, 1, 1, 2, 2, 0. Only pixel 2 changed, and the viewer sees the new color at the next redraw rather than at the instant of the write.
Drawing Off-Screen
A frame buffer is only one place an image can live. Because an image is just a set of numbers, its data does not have to sit in the frame buffer at all. The same numbers can be kept elsewhere in the computer's memory, where a program can prepare a drawing without showing it. This is off-screen drawing: the numbers exist and can be manipulated, but nothing is displayed until they are moved to the frame buffer or otherwise shown.
Images on Disk and From the Network
The numbers can also be stored in a file on the computer's hard disk. Like any other data file, an image file can be downloaded over the Internet. A file is a durable copy: it survives after the program ends and can be sent from one machine to another. When you need the picture again, the file's contents can be loaded back into memory and then displayed.
Java provides standard classes and subroutines for this work. They can copy image data from one part of memory to another, and they can read data from an image file and use it to display the image on the screen. The class names are not part of the source material here, so the diagram below speaks only in general steps.
Read the diagram as a set of routes rather than one fixed sequence. The numbers may come from a file, a download, or ordinary memory, and some may already be in the frame buffer. Whatever route they take, the video card's job at the end is the same: read the frame buffer and color the pixels. Only the frame buffer feeds the screen directly, which is why an image stored elsewhere must eventually be copied into the frame buffer before the user can see it.
Predicting When a Change Appears
What do you think happens?
A program writes new numbers into the frame buffer. Does the user see the change at the instant of the write?
Reveal answer
No, the change appears at the next redraw, a fraction of a second later
The program only writes numbers. The video card reads the frame buffer again at its next redraw and colors the screen from the updated numbers, so the visible change lags the write.
Using the six-pixel strip 1, 1, 0, 2, 2, 0, a program writes the number 2 into pixel 0 and the number 0 into pixel 5. Write the new strip. Then explain in one sentence why the viewer sees neither change at the moment of writing.
Hints
- Count positions from 0, starting at the left.
- Only the numbers at the changed positions differ; every other number stays the same.
- Think about when the video card reads the frame buffer.
Common Misunderstandings About Screen Changes
Common mistake Believing that writing to the frame buffer changes the screen immediately
Why it failsThe change becomes visible at the next redraw. The video card reads the frame buffer many times each second, so the write and the visible change are separated in time.
Do this insteadThink of writing numbers as preparing the next picture; the screen shows it when the video card next redraws.
Common mistake Assuming an image must be in the frame buffer in order to be stored or prepared
Why it failsBecause image data is just numbers, it can live elsewhere in memory, in an image file on disk, or in a downloaded file.
Do this insteadKeep image data wherever it is convenient, and copy it into the frame buffer only when it needs to be shown.
Common mistake Thinking a drawing kept in memory is lost or invisible to the program
Why it failsOff-screen drawing means the picture is held as numbers that the program can read and change. It is only not yet displayed.
Do this insteadOff-screen data can be modified and then moved to the frame buffer whenever the program is ready to show it.
Common mistake Assuming a file on disk is itself what the screen shows
Why it failsA file is a durable copy of the numbers. The video card reads only the frame buffer, so the file must first be loaded and then copied there.
Do this insteadFollow the route: file, then memory, then frame buffer, then screen.
Key Takeaways in Brief
- 1To a computer, an image is a set of numbers, with each number specifying the color of one pixel.
- 2The frame buffer holds the numbers that produce the visible picture, and the video card reads it many times each second to color the screen.
- 3Changing the screen means writing new numbers into the frame buffer, and the change appears at the next redraw.
- 4Image data can live in memory, in a file on disk, or in a downloaded file, and it must be copied into the frame buffer before the user can see it.
At a glance
- To a computer, an image is a set of numbers, with each number specifying the color of one pixel.
- The frame buffer holds the numbers that produce the visible picture, and the video card reads it many times each second to color the screen.
- Changing the screen means writing new numbers into the frame buffer, and the change appears at the next redraw, not at the instant of writing.
- Image data can be kept in memory, in a disk file, or downloaded, and it reaches the screen only after being copied into the frame buffer.