public class Viewer2D extends JComponent implements AppearanceChangeListener
This is a basic viewer for the 2D-scenes in this package. A scene node is displayed in the following order: shape, outline, points, annotation, children.
There are two ways to add geometry to the viewer: Geometry that is
attached to the viewer window itself (like coordinate systems) can be
added to the SceneComponent
returned by
getBackdrop()
. The backdrop is drawn before root and does not
participate in picking or encompass. All other geometry should be added to
the SceneComponent
returned by getRoot()
.
The viewer's affine transformation (mapping user space to screen space) is
always a diagonal matrix followed by a translation. This means that the
visible part in user space is always a rectangle aligned with the axes,
called the viewport. If one wants to preserve the aspect ratio of the viewer,
encompass(Rectangle2D.Double r)
provides a convenient
method to make the rectangle r
in user space just fit into the
viewing window (centered both horizontally and vertically). If the property
keepingAspectRatio
is false
,
encompass(r)
will make the viewport in fact equal to
r
. keepingAspectRatio
also determines whether the
standard scale tool scales both dimensions uniformly or separately.
To implement geometry that dynamically depends on the viewport (like
cordinate axes) objects can register with the viewer to be notified about
the occurance of a ViewportChangeEvent
.
Dragging with the mouse in the viewer window results in drag events being
sent to the frontmost (i.e last drawn) drag-enabled scene node that was under
the mouse cursor when mouse button was pressed. These events contain
information about which part
of the node is dragged (the shape, the outline or one of the points) and (as
a general modifier) whether the shift key is down or not. One can add
behaviour to a scene node either by subclassing and overriding the methods
of the DragListener
interface or (using a particular
implementation of SceneComponent
like SceneComponent
that supports this) adding a DragListener
to the scene node's
list of drag listeners.
If no scene node is under the mouse cursor when mouse button is pressed, the drag event is sent to the viewer's primary tool (or the secondary tool if the shift key is down). The primary tool and the secondary tool can be freely set. By default, the primary tool translates the viewer's viewport and the secondary tool scales the viewport. By holding the control key down, one can enforce activation of the viewer tools even when some drag-enabled scene node was under the mouse cursor when mouse button was pressed.
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
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
Viewer2D() |
Viewer2D(Rectangle2D.Double r)
encompassPolicy determines when the viewer will adjust its
viewport to encompass the whole scene. |
Modifier and Type | Method and Description |
---|---|
void |
addDragListener(DragListener l)
Adds
l to the list of listeners that are notified of
drag events on the viewer background. |
void |
addViewportChangeListener(ViewportChangeListener l)
Adds
l to the list of listeners that are notified when the
viewport changes. |
void |
appearanceChange(AppearanceChangeEvent e)
Take notice that the appearance of some scene node has changed (meaning
that a repaint is needed).
|
void |
encompass(Rectangle2D.Double r) |
void |
encompass(Rectangle2D.Double r,
double marginX,
double marginY)
Use a translation and a scaling (preserving the aspect ratio) to make
the rectangle
r in user space just fit into the viewing
window (centered both horizontally and vertically), respecting the value
of the property encompassMargin . |
void |
exportPDF() |
boolean |
getAntialias()
Returns the boolean that determines whether antialiasing is turned on.
|
SceneComponent |
getBackdrop()
Returns the root node of the part of the scene that contains geometry
attached to the viewer window itself (like coordinate systems).
|
Rectangle2D.Double |
getBounds2D()
Return the rectangle in user space enclosing the whole scene.
|
int |
getEncompassMarginX()
Returns the integer that determines how many pixels on the left and
right window boundary are left as as a margin when a rectangle (or
the whole scene) is encompassed.
|
int |
getEncompassMarginY()
Returns the integer that determines how many pixels on the top and
bottom window boundary are left as as a margin when a rectangle (or
the whole scene) is encompassed.
|
Action |
getExportPDFAction() |
CoordinateGrid |
getGrid() |
Rectangle2D.Double |
getInitialViewPort() |
double |
getLastPopupLocationX() |
double |
getLastPopupLocationY() |
JPopupMenu |
getMenu() |
DragListener |
getMenuTool()
Returns the DragListener that pops up the context menu.
|
boolean |
getMenuToolEnabled()
Returns the boolean that determines whether left mouse drag on the viewer
background is hooked to scaling of the viewport.
|
SceneComponent |
getRoot()
Returns the root of the tree of scene nodes.
|
DragListener |
getScaleTool()
Returns the DragListener that is scales the viewport.
|
boolean |
getScaleToolEnabled()
Returns the boolean that determines whether left mouse drag on the viewer
background is hooked to scaling of the viewport.
|
int |
getSnapToGridResolution() |
DragListener |
getTranslateTool()
Returns the DragListener that is scales the viewport.
|
boolean |
getTranslateToolEnabled()
Returns the boolean that determines whether left mouse drag on the viewer
background is hooked to translation of the viewport.
|
Rectangle2D.Double |
getViewport()
Return the visible Rectangle
|
boolean |
isGridEnabled() |
boolean |
isKeepingAspectRatio() |
boolean |
isSnapToGridEnabled() |
protected void |
paintComponent(Graphics g)
Requires g to be of type
java.awt.Graphics2D . |
void |
removeDragListener(DragListener l)
Removes
l from the list of listeners that are notified of
drag events on the viewer background. |
void |
removeViewportChangeListener(ViewportChangeListener l)
Removes
l from the list of listeners that are notified
when the viewport changes. |
void |
setAntialias(boolean b)
Sets the boolean that determines whether antialiasing is turned on.
|
void |
setEncompassMarginX(int n)
Sets the integer that determines how many pixels on the left and
right window boundary are left as as a margin when a rectangle (or
the whole scene) is encompassed.
|
void |
setEncompassMarginY(int n)
Sets the integer that determines how many pixels on the top and
bottom window boundary are left as as a margin when a rectangle (or
the whole scene) is encompassed.
|
void |
setGridEnabled(boolean b) |
void |
setInitialViewPort(Rectangle2D.Double initialViewPort) |
void |
setKeepingAspectRatio(boolean b) |
void |
setMenuToolEnabled(boolean b)
Determines whether right click on the viewer background is hooked to
showing the context menu and setting lastPopupLocationX, lastPopupLocationY.
|
void |
setScaleToolEnabled(boolean b)
Determines whether left mouse drag on the viewer background is hooked to
scaling of the viewport.
|
void |
setSnapToGridEnabled(boolean snapToGridEnabled) |
void |
setSnapToGridResolution(int snapToGridResolution) |
void |
setTranslateToolEnabled(boolean b)
Determines whether left mouse drag on the viewer background is hooked to
translation of the viewport.
|
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, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
add, 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, remove, removeAll, 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
public Viewer2D()
public Viewer2D(Rectangle2D.Double r)
encompassPolicy
determines when the viewer will adjust its
viewport to encompass the whole scene. If set to MANUAL this
will only happen if encompass is explicitly called. If set to
ENCOMPASS_ON_FIRST_RESIZE
, it will happe in addition the
first time the viewer gets resized (which usually happens the first time
it appears on the screen). If set to ENCOMPASS_ON_RESIZE it will happen
on every resize.
public void addDragListener(DragListener l)
l
to the list of listeners that are notified of
drag events on the viewer background.public void addViewportChangeListener(ViewportChangeListener l)
l
to the list of listeners that are notified when the
viewport changes.public void appearanceChange(AppearanceChangeEvent e)
AppearanceChangeListener
appearanceChange
in interface AppearanceChangeListener
public void encompass(Rectangle2D.Double r)
public void encompass(Rectangle2D.Double r, double marginX, double marginY)
r
in user space just fit into the viewing
window (centered both horizontally and vertically), respecting the value
of the property encompassMargin
.public void exportPDF()
public boolean getAntialias()
public SceneComponent getBackdrop()
public Rectangle2D.Double getBounds2D()
public int getEncompassMarginX()
public int getEncompassMarginY()
public Action getExportPDFAction()
public CoordinateGrid getGrid()
public Rectangle2D.Double getInitialViewPort()
public double getLastPopupLocationX()
public double getLastPopupLocationY()
public JPopupMenu getMenu()
public DragListener getMenuTool()
public boolean getMenuToolEnabled()
public SceneComponent getRoot()
public DragListener getScaleTool()
public boolean getScaleToolEnabled()
public int getSnapToGridResolution()
public DragListener getTranslateTool()
public boolean getTranslateToolEnabled()
public Rectangle2D.Double getViewport()
public boolean isGridEnabled()
public boolean isKeepingAspectRatio()
public boolean isSnapToGridEnabled()
protected void paintComponent(Graphics g)
java.awt.Graphics2D
.paintComponent
in class JComponent
public void removeDragListener(DragListener l)
l
from the list of listeners that are notified of
drag events on the viewer background.public void removeViewportChangeListener(ViewportChangeListener l)
l
from the list of listeners that are notified
when the viewport changes.public void setAntialias(boolean b)
public void setEncompassMarginX(int n)
public void setEncompassMarginY(int n)
public void setGridEnabled(boolean b)
public void setInitialViewPort(Rectangle2D.Double initialViewPort)
public void setKeepingAspectRatio(boolean b)
public void setMenuToolEnabled(boolean b)
public void setScaleToolEnabled(boolean b)
public void setSnapToGridEnabled(boolean snapToGridEnabled)
public void setSnapToGridResolution(int snapToGridResolution)
public void setTranslateToolEnabled(boolean b)