Reference for Processing version 1.5. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.

Name

### size()

Examples

    void setup() {
      size(320, 240);
      background(153);
    }

    void draw() {
      line(0, 0, width, height);
    }

    void setup() {
      size(320, 240, P3D);
      background(153);
    }

    void draw() {
      line(0, 0, 0, width, height, -200);
    }

    import processing.opengl.*;

    // Unless running in "Present" mode, this will not hide
    // the menu bar when using Mac OS X.
    void setup() {
      size(screen.width, screen.height, OPENGL);
      background(153);
    }

    void draw() {
      line(0, 0, 0, width, height, -200);
    }

Description

Defines the dimension of the display window in units of pixels. The size() function must be the first line in setup(). If size() is not called, the default size of the window is 100x100 pixels. The system variables width and height are set by the parameters passed to the size() function.
Do not use variables as the parameters to size() command, because it will cause problems when exporting your sketch. When variables are used, the dimensions of your sketch cannot be determined during export. Instead, employ numeric values in the size() statement, and then use the built-in width and height variables inside your program when you need the dimensions of the display window are needed.
The MODE parameters selects which rendering engine to use. For example, if you will be drawing 3D shapes for the web use P3D, if you want to export a program with OpenGL graphics acceleration use OPENGL. A brief description of the four primary renderers follows:
JAVA2D - The default renderer. This renderer supports two dimensional drawing and provides higher image quality in overall, but generally slower than P2D.
P2D (Processing 2D) - Fast 2D renderer, best used with pixel data, but not as accurate as the JAVA2D default.
P3D (Processing 3D) - Fast 3D renderer for the web. Sacrifices rendering quality for quick 3D drawing.
OPENGL - High speed 3D graphics renderer that makes use of OpenGL-compatible graphics hardware is available. Keep in mind that OpenGL is not magic pixie dust that makes any sketch faster (though it's close), so other rendering options may produce better results depending on the nature of your code. Also note that with OpenGL, all graphics are smoothed: the smooth() and noSmooth() commands are ignored.
PDF - The PDF renderer draws 2D graphics directly to an Acrobat PDF file. This produces excellent results when you need vector shapes for high resolution output or printing. You must first use Import Library → PDF to make use of the library. More information can be found in the PDF library reference. If you're manipulating pixels (using methods like get() or blend(), or manipulating the pixels[] array), P2D and P3D will usually be faster than the default (JAVA2D) setting, and often the OPENGL setting as well. Similarly, when handling lots of images, or doing video playback, P2D and P3D will tend to be faster.
The P2D, P3D, and OPENGL renderers do not support strokeCap() or strokeJoin(), which can lead to ugly results when using strokeWeight(). (Bug 955)
For the most elegant and accurate results when drawing in 2D, particularly when using smooth(), use the JAVA2D renderer setting. It may be slower than the others, but is the most complete, which is why it's the default. Advanced users will want to switch to other renderers as they learn the tradeoffs.
Rendering graphics requires tradeoffs between speed, accuracy, and general usefulness of the available features. None of the renderers are perfect, so we provide multiple options so that you can decide what tradeoffs make the most sense for your project. We'd prefer all of them to have perfect visual accuracy, high performance, and support a wide range of features, but that's simply not possible.
The maximum width and height is limited by your operating system, and is usually the width and height of your actual screen. On some machines it may simply be the number of pixels on your current screen, meaning that a screen that's 800x600 could support size(1600, 300), since it's the same number of pixels. This varies widely so you'll have to try different rendering modes and sizes until you get what you're looking for. If you need something larger, use createGraphics to create a non-visible drawing surface.
Again, the size() method must be the first line of the code (or first item inside setup). Any code that appears before the size() command may run more than once, which can lead to confusing results.

Syntax

    size(width, height)
    size(width, height, MODE)

Parameters

width | int: width of the display window in units of pixels
height | int: height of the display window in units of pixels
MODE | Either P2D, P3D, JAVA2D, or OPENGL

Returns | None
Usage | Web & Application
Related | createGraphics() screen

Updated on August 30, 2010 04:29:39pm EDT