Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Package | com.apple.cocoa.application |
Companion guide | Basic Drawing |
The NSGraphicsContext class is the programmatic interface to objects that represent graphics contexts. A context can be thought of as a destination to which drawing and graphics state operations are sent for execution. Each graphics context contains its own graphics environment and state.
The NSGraphicsContext class is an abstract superclass for destination-specific graphics contexts. You obtain instances of concrete subclasses with the static methods currentContext
, graphicsContextWithAttributes
, , , and graphicsContextWithWindow
.
At any time there is the notion of the current context. The current context for the current thread may be set using setCurrentContext
.
Graphics contexts are maintained on a stack. You push a graphics context onto the stack by sending it a saveGraphicsState
message, and pop it off the stack by sending it a restoreGraphicsState
message. By sending restoreGraphicsState
to an NSGraphicsContext object you remove it from the stack, and the next graphics context on the stack becomes the current graphics context.
imageInterpolation
setImageInterpolation
shouldAntialias
setShouldAntialias
patternPhase
setPatternPhase
NSGraphicsContext is an abstract class, so you should not create your own instances.
public NSGraphicsContext
()
Use one of the static methods to obtain an instance of the appropriate concrete subclass.
Returns the current graphics context of the current thread.
public static NSGraphicsContext currentContext
()
Returns a concrete subclass of NSGraphicsContext.
Convenience method equivalent to sending isDrawingToScreen
to the result of currentContext
.
public static boolean currentContextDrawingToScreen
()
Instantiates a concrete subclass of NSGraphicsContext using the information in attributes.
public static NSGraphicsContext graphicsContextWithAttributes
(NSDictionary attributes)
Use this method to create a graphics context for a window or bitmap destination. If you want to create a graphics context for a PDF or PostScript destination, do not use this method; instead, use the NSPrintOperation class to set up the printing environment needed to generate that type of information.
See the "Constants" section for the dictionary keys available for attributes.
Creates and returns a new graphics context for drawing into aWindow.
public static NSGraphicsContext graphicsContextWithWindow
(NSWindow aWindow)
Pops a graphics context from the per-thread stack, makes it current, and sends the context a restoreGraphicsState
message.
public static void restoreGraphicsContext
()
Sends the current graphics context a saveGraphicsState
message and pushes the context onto the per-thread stack
public static void saveGraphicsContext
()
.
Sets the current graphics context of the current thread to context.
public static void setCurrentContext
(NSGraphicsContext context)
context must be a concrete subclass of NSGraphicsContext.
Makes the graphics context of graphicState current, and resets graphics state.
public static void setGraphicsState
(int graphicsState)
The graphicState must be created in the calling thread.
Returns the receiver’s attributes, if any.
public NSDictionary attributes
()
Screen-based graphics contexts do not store attributes, even if you create them using graphicsContextWithAttributes
.
Not implemented.
public void flush
()
Use flushGraphics
.
Forces any buffered operations or data to be sent to the receiver’s destination.
public void flushGraphics
()
Graphics contexts use buffers to queue pending operations but for efficiency reasons may not always empty those buffers immediately. This method forces the buffers to be emptied.
Returns the receiver’s interpolation (image smoothing).
public int imageInterpolation
()
Returns true
if the drawing destination is the screen or a bitmap, false
otherwise.
public boolean isDrawingToScreen
()
You can interpret a return value of false
to mean that the drawing destination is a printer, although it may also be a PDF or EPS file. The NSColor method set
, for example, invokes isDrawingToScreen
to determine whether it can apply an alpha value, which is not supported by printing contexts.
If this method returns false
, you can call attributes
to see if additional information is available about the drawing destination.
Returns the amount to offset the pattern color when filling the receiver.
public NSPoint patternPhase
()
The pattern phase is a translation (width, height) applied before a pattern is drawn in the current context and is part of the saved graphics state of the context. The default pattern phase is (0,0). Setting the pattern phase has the effect of temporarily changing the pattern matrix of any pattern you decide to draw. For example, setting the pattern phase to (2,3) has the effect of moving the start of pattern cell tiling to the point (2,3) in default user space.
Removes the receiver’s graphics state from the top of the graphics state stack and makes the next graphics state the current graphics state.
public void restoreGraphicsState
()
This method must have been preceded with a saveGraphicsState
message to add the graphics state to the stack. Invocations of saveGraphicsState
and restoreGraphicsState
methods may be nested.
Restoring the graphics state restores such attributes as the current drawing style, transformation matrix, color, and font of the original graphics state.
Saves the current graphics state and creates a new graphics state on the top of the stack.
public void saveGraphicsState
()
The new graphics state is a copy of the previous state that can be modified to handle new drawing operations.
Saving the graphics state saves such attributes as the current drawing style, transformation matrix, color, and font. To set drawing style attributes, use the methods of NSBezierPath. Other attributes are accessed through appropriate objects such as NSAffineTransform, NSColor, and NSFont.
Sets the receiver’s interpolation (image smoothing) to interpolation.
public void setImageInterpolation
(int interpolation)
Note that this value is not part of the graphics state, so it cannot be reset using restoreGraphicsState
.
Sets the amount to offset the pattern color when filling the receiver.
public void setPatternPhase
(NSPoint phase)
Use it when you need to line up the pattern color with another pattern, such as the pattern in a superview.
The pattern phase is a translation (width, height) applied before a pattern is drawn in the current context and is part of the saved graphics state of the context. The default pattern phase is (0,0). Setting the pattern phase has the effect of temporarily changing the pattern matrix of any pattern you decide to draw. For example, setting the pattern phase to (2,3) has the effect of moving the start of pattern cell tiling to the point (2,3) in default user space.
Sets whether the receiver should use antialiasing, depending on the Boolean value antialias.
public void setShouldAntialias
(boolean antialias)
This value is part of the graphics state and is restored by restoreGraphicsState
.
Returns whether the receiver uses antialiasing.
public boolean shouldAntialias
()
Not implemented.
public void synchronize
()
The following constants are defined by NSGraphicsContext and are dictionary keys used by graphicsContextWithAttributes
and attributes
:
The following constants are possible values for the GraphicsContextRepresentationFormatAttributeName
key in a graphic context’s attribute dictionary:
Constant | Description |
---|---|
GraphicsContextPDFFormat | Destination file format is PDF. |
GraphicsContextPSFormat | Destination file format is PostScript. |
The following interpolations are defined by NSGraphicsContext and used by imageInterpolation
and setImageInterpolation
:
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)