Concepts / Colors and Pixel Control in Java Graphics

Colors and Pixel Control in Java Graphics

A pixel is one of the small squares arranged in rows and columns that make up a screen, and the computer controls each pixel's color to produce drawings.

  • Programming

Why Every Picture Is Built From Pixels

Every picture you have ever seen on a screen, from a photograph to a button in a window, is ultimately made from one very small kind of unit. Before you can draw a line, fill a circle, or place a rectangle inside a window using Java, you need a clear picture of what that unit is and how the computer keeps track of where each unit sits. This article builds that picture in three parts: what a pixel is, how its position is named with coordinates, and how shapes are described in terms of those positions.

A very common first assumption is that a pixel is a fixed, universal piece of the screen, the same physical size no matter which monitor you are using. It is easy to see why. You are told that the screen is made of small squares, and it seems natural that every square is identical in size everywhere. The source describes a more careful picture, and this article will correct the assumption as we go.

What do you think happens?

You are given a rectangular drawing area on the screen and asked to find the pixel that gets coordinates (0,0). Which corner of the area is it?

  • The upper left corner

  • The lower left corner

  • The center of the area

  • The upper right corner

Reveal answer
Answer:

The upper left corner

The origin, where both counts begin, is the upper left corner of the rectangular drawing area, and its coordinates are (0,0). Keep this in mind, because it determines the direction in which every other coordinate increases.

What a Pixel Is and What It Controls

Definition

A pixel is one of the small squares arranged in rows and columns that make up a screen. The computer controls each pixel's color to produce drawings.

Think of the screen as a grid. Each cell of the grid is one pixel, and each cell can show its own color. A drawing is nothing more than a particular arrangement of colors across that grid. A straight line, a filled circle, and a photograph all reduce to the same underlying operation: deciding what color each pixel should be.

This explains why drawing in Java depends on pixels. Drawing happens by changing the colors of individual pixels. The computer controls the color of each one, so to draw anything you must be able to name a particular pixel. Without a way to name pixels, the computer has no way to say which square should change. That naming system is the coordinate system described next.

Tracing a Pixel Address Through the Grid

Each pixel has a pair of integer coordinates, usually called x and y, which specify its horizontal and vertical position within the drawing area. Two rules fix how the numbering works. The x coordinate increases from left to right. The y coordinate increases from top to bottom. The origin, the point where both counts begin, is the upper left corner of the drawing area, written (0,0).

The vertical direction is the detail that most often surprises learners. In school graphs, the y value usually grows upward. In a pixel drawing area, y grows downward. The pixel at (0,1) therefore sits directly below the origin, not above it. Keep the picture of a grid that starts at the top left and extends right and down, and every coordinate will make sense.

(0,0)origin(0,1)(0,2)(1,0)(1,1)(1,2)(2,0)(2,1)(2,2)(3,0)(3,1)(3,2)target pixel(4,0)(4,1)(4,2)
If the origin (0,0) is at the upper left corner, which pixel is at x=3, y=2, and why does y increase downward?
Worked example

Finding the pixel at x=3, y=2

Starting from the origin at the upper left corner, locate the pixel with coordinates x=3 and y=2.

  1. 1
    Start at the origin

    The origin (0,0) is the upper left corner of the drawing area. Both counts begin there.

  2. 2
    Move horizontally by x

    The x coordinate increases from left to right, so x=3 means move 3 pixels to the right of the origin's column.

  3. 3
    Move vertically by y

    The y coordinate increases from top to bottom, so y=2 means move 2 pixels down from the origin's row.

  4. 4
    Identify the pixel

    The pixel at the intersection of column 3 and row 2 is the one named (3,2). It is highlighted in the grid above.

Result

The pixel at x=3, y=2 is three columns from the left edge and two rows down from the top edge.

Physical Pixels Versus Logical Pixels

The first correction to the common assumption is to separate two ideas that the word pixel can cover. A physical pixel is one of the small squares that actually make up the screen. A logical pixel is a unit of measure that is somewhere close to 1/100 inch. Both meanings appear in discussions of screens, and mixing them up is a frequent source of confusion.

On an ordinary screen, the source notes that there are usually about 100 pixels per inch. At that density the two units line up closely, so a logical pixel corresponds almost exactly to one physical square. Many screens now pack far more physical pixels into each inch, however. On these high-resolution screens, one physical pixel becomes much smaller than 1/100 inch.

Small square on thescreenUnit of measurePacked tightly onhigh-resolutionscreensClose to 1/100 inchRoughly constant forprogrammer
When one logical pixel maps to several physical pixels, what changes on a high-DPI screen, and what stays roughly constant for the programmer?

The diagram above contrasts the two meanings. On a high-resolution screen, a drawing measured in logical pixels does not map to a single physical square. The physical squares are packed more tightly, so each logical unit covers several of them. What stays roughly constant for the programmer is the unit of measure itself, close to 1/100 inch. This is why the difference matters: a shape specified in logical units keeps its approximate size across screens, even though the number of physical squares behind it changes.

Describing a Rectangle With a Corner and Size

Once every pixel has a name, shapes can be described in pixel terms. The source uses the rectangle as its main example. A rectangle is specified by three things: the x and y coordinates of its upper left corner, and its width and height, all measured in pixels. The corner says where the rectangle begins, and the width and height say how far it extends.

(0,0)(0,1)(0,2)(0,3)(1,0)(1,1)corner(1,2)filled(1,3)(2,0)(2,1)filled(2,2)filled(2,3)(3,0)(3,1)filled(3,2)filled(3,3)(4,0)(4,1)(4,2)(4,3)(5,0)(5,1)(5,2)(5,3)
Given x=1, y=1, width=3, and height=2, which pixels are filled, and where does the rectangle end on the right and bottom edges?
Worked example

Finding the far edges of a rectangle

A rectangle has upper left corner x=1, y=1, width 3, and height 2, all in pixels. Which pixels does it cover, and where do its right and bottom edges fall?

  1. 1
    Read the corner

    The upper left corner is at x=1, y=1. The rectangle begins at column 1 and row 1.

  2. 2
    Apply the width

    Width 3 covers three columns starting at x=1: columns 1, 2, and 3. The rightmost filled column is x=3.

  3. 3
    Apply the height

    Height 2 covers two rows starting at y=1: rows 1 and 2. The bottom filled row is y=2.

  4. 4
    List the filled pixels

    The filled pixels are (1,1), (2,1), (3,1), (1,2), (2,2), and (3,2). Six pixels in total, arranged as a 3 by 2 block.

Result

The rectangle fills columns 1 through 3 and rows 1 through 2, so its right edge is at x=3 and its bottom edge is at y=2.

Mistakes With Coordinates and Units

Most errors in this topic come from three habits: reading y as if it grows upward, treating the origin as the center of the screen, and assuming one pixel is always one fixed physical size. The items below show each habit and the correction.

  • Common mistake

    Assuming y increases upward, as on a school graph

    What it looks like

    Reading (0,5) as a point near the bottom of the drawing area and (0,1) as near the top.

    Why it fails

    The y coordinate increases from top to bottom, so larger y values are lower on the screen.

    Do this instead

    Picture the origin at the top left and move down as y grows.

  • Common mistake

    Placing the origin at the center of the drawing area

    What it looks like

    Expecting (0,0) to be in the middle of the window.

    Why it fails

    The origin is the upper left corner of the drawing area, and that is where both counts begin.

    Do this instead

    Always locate (0,0) first at the upper left corner, then count right for x and down for y.

  • Common mistake

    Treating a logical pixel as exactly one physical square

    What it looks like

    Assuming a shape measured in logical pixels covers exactly one physical square on every screen.

    Why it fails

    A logical pixel is a unit of measure close to 1/100 inch, and on high-resolution screens one logical unit can cover several physical squares.

    Do this instead

    Treat logical pixels as a stable unit of measure, and recognize that the physical square count behind them depends on the screen.

  • Common mistake

    Forgetting that width and height extend from the corner

    What it looks like

    Believing a rectangle at corner (1,1) with width 3 ends at x=3 when it actually fills columns 1 through 3 and ends at x=3 only by counting from the corner.

    Why it fails

    The corner says where the rectangle begins, and the width and height say how far it extends, so the far edges depend on both values together.

    Do this instead

    Compute the far edge as corner plus size, checking each count against the first filled pixel.

Checking Your Pixel Reasoning

MEDIUM

A rectangle has upper left corner at x=2, y=1, width 4, and height 3, all in pixels. Which column is the rightmost filled column, and which row is the bottom filled row? Then name the pixel that sits at the upper left corner of the rectangle.

Hints
  • Width 4 starting at column 2 covers columns 2, 3, 4, and 5.
  • Height 3 starting at row 1 covers rows 1, 2, and 3.
  • The upper left corner of the rectangle is the first filled pixel, so its coordinates are simply the corner values.
EASY

Without looking back, state where the origin is, which direction y increases, and why a logical pixel is not the same as a physical pixel on a high-resolution screen. Then check your answer against the sections above.

Hints
  • The origin is the upper left corner of the drawing area.
  • Think about the downward direction of y, which is opposite to the upward direction on school graphs.
  • A logical pixel is a unit of measure near 1/100 inch, while a physical pixel is one of the squares on the screen.

Key Takeaways

  • A pixel is one of the small squares arranged in rows and columns that make up a screen, and the computer controls each pixel's color to produce drawings.
  • Each pixel is named by integer x and y coordinates, with x increasing from left to right, y increasing from top to bottom, and the origin (0,0) at the upper left corner of the drawing area.
  • A physical pixel is a square on the screen, while a logical pixel is a unit of measure close to 1/100 inch; on high-resolution screens one logical pixel can correspond to several physical pixels.
  • A rectangle is specified by the x and y coordinates of its upper left corner together with its width and height, all measured in pixels.