Class FractalCanvas
- All Implemented Interfaces:
ComponentListener,ImageObserver,MenuContainer,Serializable,EventListener,Accessible
Each Runnable has its own local version of Jep, and its own copy
of the expression. These are set during construction of the runnable.
Jep localJep = new Jep(new LightWeightComponentSet(jep));
ImportationVisitor iv = new ImportationVisitor(localJep);
Node localExpression = iv.deepCopy(expression);
Complex localC = new Complex(0, 0); // A single mutable value
Complex localZ = new Complex(0, 0);
localJep.addVariable("c", localC);
localJep.addVariable("z", localZ);
These are copies are used in the main evaluation loop.
localC.set(x, y);
localZ.set(0, 0);
int count = 0;
while (count < iterations && localZ.abs2() < 4.0) {
localZ.set((Complex) localJep.evaluate(localExpression));
count++;
}
Scheduling of threads is managed by a ThreadPoolExecutor with 10 threads
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100));
When a new image is needed a set of Runnables is created and
sent to the executor.
try {
for (int i = 0; i < nDivisions; ++i) {
for (int j = 0; j < nDivisions; ++j) {
Runnable blockRunner = new CalcBlockRunnable(i * wid, j * high, wid, high);
Future<Integer> future = executor.submit(blockRunner, wid * high);
synchronized (futures) {
futures.add(future);
}
}
}
} catch (RejectedExecutionException ex) {
System.out.println("Rejected execution " + ex.toString());
} catch (JepException ex) {
System.out.println(ex);
}
Each of these has a Future value used to count the number of
completed runnables and cancel the tasks if needed. An ArrayList
of these futures is kept. If the screen needs to be redrawn before every task
is finished then they are killed by first calling
future.cancel(true) and then purging them with
executor.purge().
An additional thread is used to collect statistics about completed tasks. A
ConcurrentLinkedDeque<Integer> is used to send messages to
this queue, and a Lock and Condition is used to
wake this thread when there is a new message.
ConcurrentLinkedDeque<Integer> reportQueue = new ConcurrentLinkedDeque<Integer>(); Reporter reporter = new Reporter(); Thread reportThead = new Thread(reporter, "ReportThread"); reportThead.start(); // Lock for reporter private Lock lock = new ReentrantLock(); // Condition to signal when the reportQueue has data private Condition notEmpty = lock.newCondition();
Messages are sent to this queue using
lock.lock(); reportQueue.add(nItts); notEmpty.signal(); lock.unlock();
And the report thread waits for messages
lock.lock();
while(true) {
while(reportQueue.isEmpty()) {
notEmpty.awaitUninterruptibly();
}
Integer itts = reportQueue.pollFirst();
...
}
The image is created using a MemoryImageSource backed by a
shared pixel array.
int[] pixels = new int[dimensions.width * dimensions.height]; MemoryImageSource source = new MemoryImageSource(dimensions.width, dimensions.height, pixels, 0, dimensions.width); source.setAnimated(true); Image outImage = Toolkit.getDefaultToolkit().createImage(source);
The pixels are simply set by the runnable
pixels[index] = Color.getRGB() and when an entire block is
finished image consumers are informed using
source.newPixels(rect.x,rect.y,rect.width,rect.height ). The
paint method simply uses the Graphic.drawImage using
Component imageUpdate() callback method to paint
more of the image as it becomes available.
public void paint(Graphics g) {
g.drawImage(outImage, 0, 0, this);
}
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanelNested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidvoidvoidvoidvoidrender()booleansetColorExpression(String str) Parses a new expression for colorsbooleansetExpressionString(String newString) Parses a new expressionvoidsetIterations(int iterations_in) Sets number of iterationsvoidshiftSelection(com.singularsys.jepexamples.applets.FractalCanvas.RegionListener regionListener) Responds to mouse drag events translates the scene.voidZoom in the sceenvoidzoomOut()Zoom out the scene.Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUIMethods inherited from class javax.swing.JComponent
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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, 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, updateMethods inherited from class java.awt.Container
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, validateTreeMethods inherited from class java.awt.Component
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, 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, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
FractalCanvas
Constructor.
-
-
Method Details
-
setExpressionString
Parses a new expression- Returns:
- true on successful parse
-
setColorExpression
Parses a new expression for colors- Returns:
- true on successful parse
-
setIterations
public void setIterations(int iterations_in) Sets number of iterations- Parameters:
iterations_in-
-
paint
- Overrides:
paintin classJComponent
-
shiftSelection
public void shiftSelection(com.singularsys.jepexamples.applets.FractalCanvas.RegionListener regionListener) Responds to mouse drag events translates the scene. -
zoomIn
Zoom in the sceen- Parameters:
mid- midpoint of the new scene
-
zoomOut
public void zoomOut()Zoom out the scene. -
componentResized
- Specified by:
componentResizedin interfaceComponentListener
-
componentMoved
- Specified by:
componentMovedin interfaceComponentListener
-
componentShown
- Specified by:
componentShownin interfaceComponentListener
-
componentHidden
- Specified by:
componentHiddenin interfaceComponentListener
-
render
public void render()
-