public class GCanvas extends JComponent implements GCanvasInterface, GContainer, Iterable<GObject>
GCanvas
class is a lightweight component that also
serves as a container for graphical objects. As such, this class
provides the link between graphical objects and the window system.
Conceptually, the GCanvas
provides a background canvas
to which other graphical objects can be added.JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
BACK_TO_FRONT, FRONT_TO_BACK
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
GCanvas()
Creates a new
GCanvas that contains no objects. |
Modifier and Type | Method and Description |
---|---|
Component |
add(Component comp)
Adds the component to this canvas without changing its location.
|
void |
add(Component comp,
double x,
double y)
Adds the component to this canvas and sets its location
to the point (
x , y ). |
void |
add(Component comp,
GPoint pt)
Adds the component to this canvas and sets its location to the specified point.
|
void |
add(GObject gobj)
Adds the graphical object to this canvas.
|
void |
add(GObject gobj,
double x,
double y)
Adds the graphical object to this canvas and sets its location
to the point (
x , y ). |
void |
add(GObject gobj,
GPoint pt)
Adds the graphical object to this canvas and sets its location to the specified point.
|
void |
clear()
Removes all graphical objects from this canvas.
|
protected void |
conditionalRepaint()
Repaints the canvas if auto-repaint is in effect.
|
void |
diff(File file)
Compares the pixels of this canvas to the image stored in the given file.
|
void |
diff(File file,
boolean ignoreWindowSize)
Compares the pixels of this canvas to the image stored in the given file.
|
void |
diff(String file)
Compares the pixels of this canvas to the image stored in the given file.
|
void |
diff(String file,
boolean ignoreWindowSize)
Compares the pixels of this canvas to the image stored in the given file.
|
protected void |
dispatchMouseEvent(MouseEvent e)
Dispatches this mouse event to the uppermost graphical object for which
the active point is within the object bounds.
|
void |
draw(Shape paramShape)
Draws an outlined version of the given shape.
|
void |
drawArc(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4,
double paramDouble5,
double paramDouble6)
Draws an outlined arc with the given coordinates.
|
void |
drawLine(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4)
Draws a line with the given endpoint coordinates.
|
void |
drawOval(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4)
Draws an outlined oval with the given coordinates.
|
GPoint |
drawPolarLine(double x0,
double y0,
double r,
double theta)
Draws a line with the given polar coordinates, returning its starting endpoint.
|
GPoint |
drawPolarLine(GPoint p0,
double r,
double theta)
Draws a line with the given polar coordinates, returning its starting endpoint.
|
void |
drawRect(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4)
Draws an outlined rectangle with the given coordinates.
|
void |
fill(Shape paramShape)
Draws a filled version of the given shape.
|
void |
fillArc(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4,
double paramDouble5,
double paramDouble6)
Draws a filled arc with the given coordinates.
|
void |
fillOval(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4)
Draws a filled oval with the given coordinates.
|
void |
fillRect(double paramDouble1,
double paramDouble2,
double paramDouble3,
double paramDouble4)
Draws a filled rectangle with the given coordinates.
|
boolean |
getAutoRepaintFlag()
Returns the current setting of the auto-repaint flag as described in
setAutoRepaintFlag . |
<T extends GObject> |
getElement(int index)
Returns the graphical object at the specified index, numbering from back
to front in the the z dimension.
|
<T extends GObject> |
getElementAt(double... coords)
This version of getElementAt accepts a variable number of coordinate
pairs (in x1, y1, x2, y2, x3, y3, ...
|
<T extends GObject> |
getElementAt(double x,
double y)
Returns the topmost graphical object that contains the point
(
x , y ), or null if no such
object exists. |
<T extends GObject> |
getElementAt(GPoint pt)
Returns the topmost graphical object that contains the specified point,
or
null if no such object exists. |
int |
getElementCount()
Returns the number of graphical objects stored in this
GCanvas . |
boolean |
getNativeArcFlag()
Returns the current setting of the native-arc flag as described in
setNativeArcFlag . |
Graphics2D |
getOSG()
Returns the graphical pen for drawing on this canvas.
|
String |
getPixelsAsString()
Returns a Base64-encoded representation of the pixel data of this image.
|
int |
getRGB(int x,
int y)
Returns the RGB color stored at the given (x, y) pixel, or 0 if that pixel falls outside the bounds of this canvas.
|
boolean |
hasElementAt(double... coords)
Returns true if a graphical object exists that touches any of the given
(x, y) pixel positions, or false if no such object exists.
|
boolean |
hasElementAt(double x,
double y)
Returns true if a graphical object exists that touches the given
(x, y) pixel position, or false if no such object exists.
|
boolean |
inBounds(int x,
int y)
Whether the given (x, y) point falls within the bounds of this canvas, from (0, 0) ..
|
boolean |
isAntiAliasing()
Sets whether this canvas uses anti-aliasing, which is smoothing and blending of neighboring pixels.
|
Iterator<GObject> |
iterator()
Returns an
Iterator that cycles through the elements within
this container in the default direction, which is from back to front. |
Iterator<GObject> |
iterator(int direction)
Returns an
Iterator that cycles through the elements
within this container in the specified direction, which must be one
of the constants GContainer.FRONT_TO_BACK
or GContainer.BACK_TO_FRONT . |
void |
paintComponent(Graphics g)
Paints the canvas.
|
void |
remove(Component comp)
Removes the component from the canvas.
|
void |
remove(double x,
double y)
Removes the top-most graphical object at the given (x, y) position from this container.
|
void |
remove(GObject gobj)
Removes a graphical object from this
GCanvas . |
void |
remove(GPoint pt)
Removes the top-most graphical object at the given (x, y) position from this container.
|
void |
removeAll()
Removes all graphical objects from this
GCanvas . |
void |
removeAll(double... coords)
Removes all graphical objects at the given (x, y) positions from this container.
|
void |
removeAll(double x,
double y)
Removes all graphical objects at the given (x, y) position from this container.
|
void |
removeAll(GPoint pt)
Removes all graphical objects at the given (x, y) position from this container.
|
void |
save(File file)
Writes the contents of the canvas to the given file.
|
void |
save(String filename)
Writes the contents of the canvas to the given file.
|
protected void |
sendBackward(GObject gobj)
Implements the
sendBackward function from the GContainer
interface. |
protected void |
sendForward(GObject gobj)
Implements the
sendForward function from the GContainer
interface. |
protected void |
sendToBack(GObject gobj)
Implements the
sendToBack function from the GContainer
interface. |
protected void |
sendToFront(GObject gobj)
Implements the
sendToFront function from the GContainer
interface. |
void |
setAntiAliasing(boolean antialias)
Sets whether this canvas uses anti-aliasing, which is smoothing and blending of neighboring pixels.
|
void |
setAutoRepaintFlag(boolean state)
Changes the setting of the auto-repaint flag.
|
void |
setBackground(Color color) |
void |
setBorder(Color color)
Sets this canvas to use a border of the given color, 1px thick.
|
void |
setBorder(Color color,
int thickness)
Sets this canvas to use a border of the given color and number of pixels thick.
|
void |
setColor(Color color) |
void |
setColor(int paramInt) |
void |
setForeground(Color color) |
void |
setForeground(int paramInt) |
void |
setNativeArcFlag(boolean state)
Sets whether the redering code for
GArc and GOval should use
Java arcs. |
void |
setOpaque(boolean flag)
Sets a flag indicating whether this canvas is opaque, which means that it
obscures anything behind it.
|
void |
setPixelsFromString(String base64) |
void |
setRGB(int x,
int y,
int rgb) |
void |
setRGB(int x,
int y,
int rgb,
boolean repaint) |
void |
setShowPixelGrid(boolean show)
Sets whether to display information on this canvas about the current pixel where
the mouse pointer is resting.
|
void |
setShowPixelInfo(boolean show)
Sets whether to display information on this canvas about the current pixel where
the mouse pointer is resting.
|
void |
showDiffDialog()
Pops up a dialog box to compare the contents of this canvas to an expected image file.
|
void |
showDiffDialog(File directory)
Pops up a dialog box to compare the contents of this canvas to an expected image file.
|
void |
showDiffDialog(String directory)
Pops up a dialog box to compare the contents of this canvas to an expected image file.
|
File |
showSaveDialog()
Pops up a dialog box to save the contents of this canvas to a file.
|
File |
showSaveDialog(File directory)
Pops up a dialog box to save the contents of this canvas to a file.
|
File |
showSaveDialog(String directory)
Pops up a dialog box to save the contents of this canvas to a file.
|
BufferedImage |
toImage()
Returns the pixel contents of this canvas as a BufferedImage.
|
BufferedImage |
toImage(int width,
int height)
Returns the pixel contents of this canvas as a BufferedImage of the given size.
|
protected void |
updateEnabledList()
Reconstructs the enabledList list in the correct order.
|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getBackground, getFont, getHeight, getSize, getWidth, repaint, setFont, setSize
forEach, spliterator
public GCanvas()
GCanvas
that contains no objects.public Component add(Component comp)
Component
to match the method in
the Container
class, but the result is typically
ignored.public final void add(Component comp, double x, double y)
x
, y
).add
in interface GCanvasInterface
comp
- The component to addx
- The new x-coordinate for the objecty
- The new y-coordinate for the objectpublic final void add(Component comp, GPoint pt)
add
in interface GCanvasInterface
comp
- The component to addpt
- A GPoint
object giving the coordinates of the pointpublic void add(GObject gobj)
add
in interface GCanvasInterface
add
in interface GContainer
gobj
- The graphical object to addpublic final void add(GObject gobj, double x, double y)
x
, y
).add
in interface GCanvasInterface
add
in interface GContainer
gobj
- The graphical object to addx
- The new x-coordinate for the objecty
- The new y-coordinate for the objectpublic final void add(GObject gobj, GPoint pt)
add
in interface GCanvasInterface
add
in interface GContainer
gobj
- The graphical object to addpt
- A GPoint
object giving the coordinates of the pointpublic void clear()
clear
in interface GCanvasInterface
protected void conditionalRepaint()
repaint
method in
GObject
and is not accessible outside the package.public void diff(File file)
public void diff(File file, boolean ignoreWindowSize)
public void diff(String file)
public void diff(String file, boolean ignoreWindowSize)
protected void dispatchMouseEvent(MouseEvent e)
e
- The event that triggered this responsepublic void draw(Shape paramShape)
public void drawArc(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, double paramDouble5, double paramDouble6)
public void drawLine(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
public void drawOval(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
public GPoint drawPolarLine(double x0, double y0, double r, double theta)
public GPoint drawPolarLine(GPoint p0, double r, double theta)
public void drawRect(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
public void fill(Shape paramShape)
public void fillArc(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, double paramDouble5, double paramDouble6)
public void fillOval(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
public void fillRect(double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4)
public boolean getAutoRepaintFlag()
setAutoRepaintFlag
.true
if auto-repaint mode is enabled, and false
otherwisepublic <T extends GObject> T getElement(int index)
getElement
in interface GCanvasInterface
getElement
in interface GContainer
index
- The index of the component to returnpublic <T extends GObject> T getElementAt(double x, double y)
x
, y
), or null
if no such
object exists.getElementAt
in interface GCanvasInterface
getElementAt
in interface GContainer
x
- The x-coordinate of the point being testedy
- The y-coordinate of the point being testednull
if no such object existspublic <T extends GObject> T getElementAt(double... coords)
getElementAt
in interface GCanvasInterface
public final <T extends GObject> T getElementAt(GPoint pt)
null
if no such object exists.getElementAt
in interface GCanvasInterface
getElementAt
in interface GContainer
pt
- The coordinates being testednull
if no such object existspublic int getElementCount()
GCanvas
.getElementCount
in interface GCanvasInterface
getElementCount
in interface GContainer
GCanvas
public boolean getNativeArcFlag()
setNativeArcFlag
.true
if native arcs are enabled, and false
otherwisepublic Graphics2D getOSG()
public String getPixelsAsString()
public int getRGB(int x, int y)
public boolean hasElementAt(double x, double y)
hasElementAt
in interface GCanvasInterface
public boolean hasElementAt(double... coords)
hasElementAt
in interface GCanvasInterface
public boolean inBounds(int x, int y)
public boolean isAntiAliasing()
isAntiAliasing
in interface GCanvasInterface
public Iterator<GObject> iterator()
Iterator
that cycles through the elements within
this container in the default direction, which is from back to front.
You can also run the iterator in the opposite direction by using the
iterator
(
direction)
form of this method.iterator
in interface GCanvasInterface
iterator
in interface Iterable<GObject>
Iterator
ranging over the elements of the
container from back to frontpublic Iterator<GObject> iterator(int direction)
Iterator
that cycles through the elements
within this container in the specified direction, which must be one
of the constants GContainer.FRONT_TO_BACK
or GContainer.BACK_TO_FRONT
.
for (Iterator<GObject> i = gc.iterator(direction); i.hasNext(); )
iterator
in interface GCanvasInterface
Iterator
ranging over the elements of the
container in the specified directionpublic void paintComponent(Graphics g)
paintComponent
in class JComponent
g
- The graphics context into which the canvas is paintedpublic void remove(Component comp)
public void remove(double x, double y)
remove
in interface GCanvasInterface
public void remove(GObject gobj)
GCanvas
.remove
in interface GCanvasInterface
remove
in interface GContainer
gobj
- The graphical object to removepublic void remove(GPoint pt)
remove
in interface GCanvasInterface
public void removeAll()
GCanvas
.removeAll
in interface GCanvasInterface
removeAll
in interface GContainer
removeAll
in class Container
public void removeAll(double x, double y)
removeAll
in interface GCanvasInterface
public void removeAll(double... coords)
removeAll
in interface GCanvasInterface
public void removeAll(GPoint pt)
removeAll
in interface GCanvasInterface
public void save(File file)
public void save(String filename)
protected void sendBackward(GObject gobj)
sendBackward
function from the GContainer
interface. Clients should not be calling this method, but the semantics of
interfaces forces it to be exported.protected void sendForward(GObject gobj)
sendForward
function from the GContainer
interface. Clients should not be calling this method, but the semantics of
interfaces forces it to be exported.protected void sendToBack(GObject gobj)
sendToBack
function from the GContainer
interface. Clients should not be calling this method, but the semantics of
interfaces forces it to be exported.protected void sendToFront(GObject gobj)
sendToFront
function from the GContainer
interface. Clients should not be calling this method, but the semantics of
interfaces forces it to be exported.public void setAntiAliasing(boolean antialias)
setAntiAliasing
in interface GCanvasInterface
public void setAutoRepaintFlag(boolean state)
setAutoRepaintFlag(false)
, but you must then make explicit
calls to repaint()
whenever you want to update the display.
The advantage of this model is that you can then make many different changes
and have them all appear at once with a single repaint
call.state
- true
to enable auto-repaint mode, and false
to disable itpublic void setBackground(Color color)
setBackground
in interface GCanvasInterface
setBackground
in class JComponent
public void setBorder(Color color)
public void setBorder(Color color, int thickness)
public void setColor(Color color)
public void setColor(int paramInt)
public void setForeground(Color color)
setForeground
in class JComponent
public void setForeground(int paramInt)
public void setNativeArcFlag(boolean state)
GArc
and GOval
should use
Java arcs. By default, arcs and ovals are rendered using polygons and polylines
to ensure that the same set of pixels is covered by the fill and frame.
If this value is set to true
, the renderers will use the native
arc code, which is more efficient but less accurate.state
- true
to enable native arcs, false
to use polygonspublic void setOpaque(boolean flag)
false
makes
the GCanvas
transparent, so that any other lightweight components
behind it show through.setOpaque
in class JComponent
flag
- true
to make this canvas opaque, and false
to make it transparentpublic void setPixelsFromString(String base64)
public void setRGB(int x, int y, int rgb)
public void setRGB(int x, int y, int rgb, boolean repaint)
public void setShowPixelGrid(boolean show)
public void setShowPixelInfo(boolean show)
setShowPixelInfo
in interface GCanvasInterface
public void showDiffDialog()
public void showDiffDialog(File directory)
public void showDiffDialog(String directory)
public File showSaveDialog()
public File showSaveDialog(File directory)
public File showSaveDialog(String directory)
public BufferedImage toImage()
toImage
in interface GCanvasInterface
public BufferedImage toImage(int width, int height)
protected void updateEnabledList()