Standard Draw 3D
Complete Reference Manual
Hayk Martirosyan
Introduction
Standard Draw 3D is a Java library with the express goal of making it simple to create three-dimensional models, simulations, and games. If you have not used StdDraw3D before, make sure to go through the basic tutorial before coming here.
This document is the official reference manual for Standard Draw 3D. Listed below is every method in the library, categorized by functionality. Most of the library is in the form of static methods, but there are also nested classes that are crucial to the way StdDraw3D works.
If you would prefer to see this document in PDF form, go here.
Many interesting example programs for StdDraw3D are provided in the archive demos.zip.
Each section of the table of contents is hyperlinked to the corresponding section of the manual.
Canvas Size
Scale
Perspective
Mesh Control
Info Display
Anti-Aliasing
Pen Color
Pen Radius
Pen Font
Solid Color
Image
Spherical Panorama
Sphere
Cube
Box
Cone
Cylinder
Ellipsoid
3D Text
Tubes
External Model
Showing
Clearing
Camera Position and Orientation
Camera Direction
Mode Descriptions
Setting the Camera Mode
Orbit Center
Points
Lines
Polygons
Triangles
Image File
3D Scene
Light Utilities
Ambient Light
Point Light
Directional Light
Fog
Sound Utilities
Ambient Sound
Point Sound
Mouse Buttons
Mouse Position
Key Pressed
Key Stack
Circle
Square
Rectangle
Ellipse
Pixel
Point
Line
Arc
Polygon
Text
Picture
Color
Vector Direction
Combine
Copy
Move Absolute
Move Relative
Set Position
Rotate Absolute
Rotate Relative
Set Orientation
Rotate about Axis
Look At
Set Direction
Pair
Rotate FPS
Addition
Subtraction
Multiplication
Magnitude
Direction
Distance
Dot Product
Cross Product
Angle
Projection
Reflection
To String
Draw
These static functions are called by prefixing the method name with “StdDraw3D.”. For example, to draw a sphere, call StdDraw3D.sphere(x, y, z, r). Nested classes of StdDraw3D are documented below this section.
Global settings of StdDraw3D are included here, and control various aspects of the drawing window and rendering process. Canvas size and scale should be defined at the start of each project.
Canvas Size
Sets the drawing window size to (w, h) pixels. Can also set the drawing window to fullscreen, which makes it as large as possible to fit the displaying monitor.
All Arguments
Type | Name | Description |
double | w | width |
double | h | height |
Methods
Return Type | Name | Arguments |
void | setCanvasSize | (w, h) |
void | fullscreen | () |
Scale
The “scale” of the drawing window is important to the way StdDraw3D works. In general, it is the coordinate range which you are likely to be drawing in (for all three dimensions). In your drawing window, is by default placed such that you can see the working volume specified by the setScale() function. Of course you can move the camera as desired, but navigation speed is also calibrated based on the scale, so it is useful to set a good scale. Calling setScale() without arguments reverts to the default scale of (0, 1).
Arguments
Type | Name | Description |
double | minimum | the minimum x,y,and z coordinate of your working volume |
double | maximum | the maximum x,y,and z coordinate of your working volume |
Methods
Return Type | Name | Arguments |
void | setScale | (minimum, maximum) |
void | setScale | () |
Perspective
The perspective of a 3D scene is the way it is projected onto a 2D computer monitor to create the appearance of three-dimensional depth. Objects that are closer appear larger in real life, and this is how they are drawn in StdDraw3D. This is called perspective projection, and the amount to which closer objects appear larger is proportional to the field of view. You can specify the field of view with the setPerspectiveProjection() function, or revert to the default of 0.9. A large field of view looks like a fisheye, while a small field of view is like looking through a telescope. Reasonable values of fov range from 0.5 to 3.0. Zero perspective can be set with setParallelProjection(), and is how all CAD programs display 3D objects.
Arguments
Type | Name | Description |
double | fov | field of view of perspective projection |
Methods
Return Type | Name | Arguments |
void | setPerspectiveProjection | (fov) |
void | setPerspectiveProjection | () |
void | setParallelProjection | () |
Mesh Control
Sets the number of trianglular divisions of curved shapes like spheres and cylinders. The default is 100 divisons. Decrease this number to increase performance. Can also get the current value.
Arguments
Type | Name | Description |
int | N | number of triangular divisions |
Methods
Return Type | Name | Arguments |
void | setNumDivisions | (N) |
int | getNumDivisions | () |
Info Display
Toggles the information display in the upper-left corner.
Arguments
Type | Name | Description |
boolean | enabled | state of the info display |
Methods
Return Type | Name | Arguments |
void | setInfoDisplay | (enabled) |
Anti-Aliasing
Toggles simple anti-aliasing. Anti-aliasing makes graphics look much smoother, but is very resource heavy. It is good for saving images that look professional. The default is off. Currently not supported for OS-X, and only works with some PCs. Can also get the current state.
Arguments
Type | Name | Description |
boolean | enabled | state of anti-aliasing |
Methods
Return Type | Name | Arguments |
void | setAntiAliasing | (enabled) |
boolean | getAntiAliasing | () |
These functions change the propreties of the drawing pen, which influences the properties of things you draw. For example, a drawn sphere will be the pen color, a drawn line will have the pen radius, and drawn text will have the pen font.
Pen Color
Sets the pen to the given color. Can specify either a color, a color and a transparency, RGB components, or nothing (reverts to the default pen color). Can also get the current pen color.
All Arguments
Type | Name | Description |
Color | col | desired color |
int | alpha | optional transparency of color (0-255) |
int | r | red component of an RGB color |
int | g | green component of an RGB color |
int | b | blue component of an RGB color |
Methods
Return Type | Name | Arguments |
void | setPenColor | (col) |
void | setPenColor | (col, alpha) |
void | setPenColor | (r,g,b) |
void | setPenColor | () |
Color | getPenColor | () |
Pen Radius
Sets the pen stroke to the given radius, or reverts to the default of 0.002 if no arguments are given. Can also get the current radius.
Arguments
Type | Name | Description |
double | r | desired pen radius |
Methods
Return Type | Name | Arguments |
void | setPenRadius | (r) |
void | setPenRadius | () |
float | getPenRadius | () |
Pen Font
Sets the pen to the given Font object, or reverts to the default if no arguments are given. Can also get the current font.
All Arguments
Type | Name | Description |
Font | f | desired pen font |
Methods
Return Type | Name | Arguments |
void | setFont | (f) |
void | setFont | () |
Font | getFont | () |
The background of the drawing window is by black by default, but can be easily changed as needed. There are three options for specifying a background: you can set a solid color, a background image, or an image wrapped around a 3D background sphere.
Solid Color
Sets the background to a solid color.
Arguments
Type | Name | Description |
Color | color | desired color |
Methods
Return Type | Name | Arguments |
void | setBackground | (color) |
Image
Sets the background to a specified image, which is scaled to fit the window.
Arguments
Type | Name | Description |
String | imageURL | filename or URL of desired background image |
Methods
Return Type | Name | Arguments |
void | setBackground | (imageURL) |
Spherical Panorama
Sets the background to a specified image, wrapped around as a spherical skybox.
Arguments
Type | Name | Description |
String | imageURL | filename or URL of desired background image |
Methods
Return Type | Name | Arguments |
void | setBackgroundSphere | (imageURL) |
There are six basic three-dimensional shapes in StdDraw3D: spheres, cubes, boxes, cones, cylinders, and ellipsoids. Each primitive shape has a set of five drawing functions, depending on whether you want to specify rotation, texture, or wireframe properties. Sphere and cube are specific cases of ellipsoid and box, but are included for convenience. Every drawing function returns a Shape object, which can be moved, rotated, and scaled dynamically using the methods of the Shape class.
Sphere
Draws a sphere at (x, y, z) with radius r. Can specify Euler rotation angles (xA, yA, zA). Can be solid, wireframe, or textured.
All Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
double | r | radius |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
String | imageURL | filename or URL of wraparound texture |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Solid
Return Type | Name | Arguments |
Shape | sphere | (x,y,z,r) |
Shape | sphere | (x,y,z,r,xA,yA,zA) |
Wireframe
Return Type | Name | Arguments |
Shape | wireSphere | (x,y,z,r) |
Shape | wireSphere | (x,y,z,r,xA,yA,zA) |
Textured
Return Type | Name | Arguments |
Shape | sphere | (x,y,z,r,xA,yA,zA,imageURL) |
Cube
Draws a cube at (x, y, z) with radius r. Can specify Euler rotation angles (xA, yA, zA). Can be solid, wireframe, or textured.
All Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
double | r | radius (half of side length) |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
String | imageURL | filename or URL of wraparound texture |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Solid
Return Type | Name | Arguments |
Shape | cube | (x,y,z,r) |
Shape | cube | (x,y,z,r,xA,yA,zA) |
Wireframe
Return Type | Name | Arguments |
Shape | wireCube | (x,y,z,r) |
Shape | wireCube | (x,y,z,r,xA,yA,zA) |
Textured
Return Type | Name | Arguments |
Shape | cube | (x,y,z,r,xA,yA,zA,imageURL) |
Box
Draws a box at (x, y, z) with dimensions (w, h, d). Can specify Euler rotation angles (xA, yA, zA). Can be solid, wireframe, or textured.
All Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
double | w | width |
double | h | height |
double | d | depth |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
String | imageURL | filename or URL of wraparound texture |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Solid
Return Type | Name | Arguments |
Shape | box | (x,y,z,w,h,d) |
Shape | box | (x,y,z,w,h,d,xA,yA,zA) |
Wireframe
Return Type | Name | Arguments |
Shape | wireBox | (x,y,z,w,h,d) |
Shape | wireBox | (x,y,z,w,h,d,xA,yA,zA) |
Textured
Return Type | Name | Arguments |
Shape | box | (x,y,z,w,h,d,xA,yA,zA,imageURL) |
Cone
Draws a cone at (x,y,z) with radius r and height h. Can specify Euler rotation angles (xA, yA, zA). Can be solid, wireframe, or textured.
All Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
double | r | radius |
double | h | height |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
String | imageURL | filename or URL of wraparound texture |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Solid
Return Type | Name | Arguments |
Shape | cone | (x,y,z,r,h) |
Shape | cone | (x,y,z,r,h,xA,yA,zA) |
Wireframe
Return Type | Name | Arguments |
Shape | wireCone | (x,y,z,r,h) |
Shape | wireCone | (x,y,z,r,h,xA,yA,zA) |
Textured
Return Type | Name | Arguments |
Shape | cone | (x,y,z,r,h,xA,yA,zA,imageURL) |
Cylinder
Draws a cylinder at (x,y,z) with radius r and height h. Can specify Euler rotation angles (xA, yA, zA). Can be solid, wireframe, or textured.
All Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
double | r | radius |
double | h | height |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
String | imageURL | filename or URL of wraparound texture |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Solid
Return Type | Name | Arguments |
Shape | cylinder | (x,y,z,r,h) |
Shape | cylinder | (x,y,z,r,h,xA,yA,zA) |
Wireframe
Return Type | Name | Arguments |
Shape | wireCylinder | (x,y,z,r,h) |
Shape | wireCylinder | (x,y,z,r,h,xA,yA,zA) |
Textured
Return Type | Name | Arguments |
Shape | cylinder | (x,y,z,r,h,xA,yA,zA,imageURL) |
Ellipsoid
Draws an ellipsoid at (x, y, z) with dimensions (w, h, d). Can specify Euler rotation angles (xA, yA, zA). Can be solid, wireframe, or textured.
All Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
double | w | width |
double | h | height |
double | d | depth |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
String | imageURL | filename or URL of wraparound texture |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Solid
Return Type | Name | Arguments |
Shape | ellipsoid | (x,y,z,w,h,d) |
Shape | ellipsoid | (x,y,z,w,h,d,xA,yA,zA) |
Wireframe
Return Type | Name | Arguments |
Shape | wireEllipsoid | (x,y,z,w,h,d) |
Shape | wireEllipsoid | (x,y,z,w,h,d,xA,yA,zA) |
Textured
Return Type | Name | Arguments |
Shape | ellipsoid | (x,y,z,w,h,d,xA,yA,zA,imageURL) |
These more complicated 3D objects are useful in many cases. Every drawing function returns a Shape object, which can be moved, rotated, and scaled dynamically using the methods of the Shape class.
3D Text
Draws 3D text of the given string at (x, y, z) with the pen font. Can specify Euler rotation angles (xA, yA, zA). Uses the pen font, color, and transparency.
Arguments
Type | Name | Description |
double | x | x-coordinate of the center |
double | y | y-coordinate of the center |
double | z | z-coordinate of the center |
String | text | the text to be shown |
double | xA | x component of XYZ Euler angle rotation |
double | yA | y component of XYZ Euler angle rotation |
double | zA | z component of XYZ Euler angle rotation |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Methods
Return Type | Name | Arguments |
Shape | text3D | (x,y,z,text) |
Shape | text3D | (x,y,z,text,xA,yA,zA) |
Tubes
Draws a solid 3D cylindrical tube or a set of tubes with the given start and end coordinates and radius. Drawing a set of tubes is not much more efficient than drawing individual tubes, but will compact code. Can specify a set of colors for the set of tubes.
All Arguments
Type | Name | Description |
double | x1 | x-coordinate of the start point |
double | y1 | y-coordinate of the start point |
double | z1 | z-coordinate of the start point |
double | x2 | x-coordinate of the end point |
double | y2 | y-coordinate of the end point |
double | z2 | z-coordinate of the end point |
double[] | x | set of x-coordinates of the tube vertices |
double[] | y | set of y-coordinates of the tube vertices |
double[] | z | set of z-coordinates of the tube vertices |
double | r | radius of the tube |
Color[] | colors | set of colors that correspond to the set of tubes |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Single Tube
Return Type | Name | Arguments |
Shape | tube | (x1,y1,z1,x2,y2,z2,r) |
Set of Tubes
Return Type | Name | Arguments |
Shape | tubes | (x,y,z,r) |
Shape | tubes | (x,y,z,r,colors) |
External Model
You can easily import .OBJ (preferred) or .PLY models into your scene as Shape objects. It takes some time to create a mesh for larger models with millions of triangles. Colors and textures of an OBJ file can be imported by including the .mtl material template library. Can also import the model as a Shape with a single color (the pen color), and change the color using the Shape class. Imported OBJ models can be automatically scaled to have an average radius of 1, or keep their original size (default).
Note: The Java loader used to import OBJ files is based on old OBJ standards and often doesn’t work properly for textures and newer commands in the OBJ file.
Arguments
Type | Name | Description |
String | filename | filename of the model to import |
boolean | resize | resizes the model if true |
Return Value
Returns the drawn Shape object, which can be further manipulated.
Methods
Return Type | Name | Arguments |
Shape | model | (filename) |
Shape | model | (filename, resize) |
Shape | coloredModel | (filename) |
Shape | coloredModel | (filename, resize) |
The basic tools needed to display and animate a 3D scene are these functions for clearing or showing objects in the drawing window. If you are drawing a single scene without moving objects, just call finished() once after you are done drawing. If you are animating, call show() to display each frame with the desired framerate and clear() to clear the drawing window when desired. You can also clear just the 3D scene or just the 2D overlay, to enable independence between the two.
Showing
Show functions render everything to the drawing window, then pause for the given number of milliseconds. If no argument is provided, renders to the screen but does not pause. Methods are also provided to show just the 3D scene or to show just the 2D overlay. finished() is equivalent to show(infinity).
Arguments
Type | Name | Description |
int | time | pauses for this many milliseconds after displaying |
Methods
Return Type | Name | Arguments |
void | finished | () |
void | show | (time) |
void | show | () |
void | show3D | (time) |
void | show3D | () |
void | showOverlay | (time) |
void | showOverlay | () |
Clearing
Clears the drawing window upon the next call of show. Methods are also provided to clear just the 3D scene or to clear just the 2D overlay.
Arguments
Type | Name | Description |
Color | color | if provided,sets as the background color after clearing |
Methods
Return Type | Name | Arguments |
void | clear | (color) |
void | clear | () |
void | clear3D | () |
void | clearOverlay | () |
The simplest way to programmatically place the camera where you want is to use these static camera methods. For more advanced camera control, learn how to use and manipulate the Camera object.
Camera Position and Orientation
Sets the camera position and orientation. Orientation is represented by XYZ Euler rotation angles. These numbers correspond to those shown in the info display on the upper-left corner of the drawing window. Can specify scalar or vector input. Can also get the current position and orientation.
All Arguments
Type | Name | Description |
double | x | x-coordinate of camera position |
double | y | y-coordinate of camera position |
double | z | z-coordinate of camera position |
Vector3D | position | position vector, represents {x,y,z} |
double | xAngle | x component of XYZ Euler angle rotation |
double | yAngle | y component of XYZ Euler angle rotation |
double | zAngle | z component of XYZ Euler angle rotation |
Vector3D | angles | orientation vector, represents {xAngle,yAngle,zAngle} |
Position
Return Type | Name | Arguments |
void | setCameraPosition | (x,y,z) |
void | setCameraPosition | (position) |
Vector3D | getCameraPosition | () |
Orientation
Return Type | Name | Arguments |
void | setCameraOrientation | (xAngle,yAngle,zAngle) |
void | setCameraOrientation | (angles) |
void | getCameraOrientation | () |
Both
Return Type | Name | Arguments |
void | setCamera | (x,y,z,xAngle,yAngle,zAngle) |
void | setCamera | (position,angles) |
Camera Direction
Instead of specifying orientation by Euler angles, it is easier in many cases to specify the vector direction in which the camera should point. For example, (0,1,0) would cause the camera to rotate such that it looks along the positive y-axis. Input is normalized. Can specify scalar or vector input. Can also get the current camera direction.
All Arguments
Type |