Package eu.svjatoslav.sixth.e3d.gui
Class ViewPanel
java.lang.Object
java.awt.Component
java.awt.Canvas
eu.svjatoslav.sixth.e3d.gui.ViewPanel
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible
AWT Canvas that provides a 3D rendering surface with built-in camera navigation.
ViewPanel is the primary entry point for embedding the Sixth 3D engine into
a Java application. It manages the render loop, maintains a scene graph
(ShapeCollection), and handles user input for camera navigation.
Uses BufferStrategy for efficient page-flipping and tear-free rendering.
Quick start - creating a 3D view in a window:
// Option 1: Use ViewFrame (creates a maximized JFrame for you)
ViewFrame frame = new ViewFrame();
ViewPanel viewPanel = frame.getViewPanel();
// Option 2: Embed ViewPanel in your own window
JFrame frame = new JFrame("My 3D App");
ViewPanel viewPanel = new ViewPanel();
frame.add(viewPanel);
frame.setSize(800, 600);
frame.setVisible(true);
// Add shapes to the scene
ShapeCollection scene = viewPanel.getRootShapeCollection();
scene.addShape(new WireframeCube(
new Point3D(0, 0, 200), 50,
new LineAppearance(5, Color.GREEN)
));
// Position the camera
viewPanel.getCamera().setLocation(new Point3D(0, 0, -100));
// Listen for frame updates (e.g., for animations)
viewPanel.addFrameListener((panel, deltaMs) -> {
// Called before each frame. Return true to force repaint.
return false;
});
Architecture:
- A background render thread continuously generates frames at the target FPS
- The engine intelligently skips rendering when no visual changes are detected
FrameListeners are notified before each potential frame, enabling animations- Mouse/keyboard input is managed by
InputManager - Keyboard focus is managed by
KeyboardFocusStack
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.awt.Canvas
Canvas.AccessibleAWTCanvasNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsFields 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 TypeMethodDescriptionvoidaddFrameListener(FrameListener listener) Registers a listener that will be notified before each frame render.voidEnables progressive global illumination with the default of 2 dedicated low-priority CPU threads.enableGlobalIllumination(int threadCount) Enables progressive global illumination with the given number of dedicated low-priority CPU threads.Returns the camera representing the viewer's position and orientation.Returns the debug log buffer for this view panel.Returns the developer tools for this view panel.Returns the input manager handling mouse and keyboard events for this view.Returns the keyboard focus stack, which manages which component receives keyboard input.Returns the global lighting manager for the scene.doubleReturns the measured production rate: frames completed per second, averaged over the last ~500 ms window.intReturns the current number of render threads.Returns the current rendering context for the active frame.Returns the root shape collection (scene graph).doubleReturns the current inter-pupillary distance used for stereo rendering.intReturns the current target frames per second rate.booleanReturns whether side-by-side stereoscopic rendering is currently enabled.voidvoidremoveFrameListener(FrameListener frameListener) Removes a previously registered frame listener.voidCalling these methods tells 3D engine that current 3D view needs to be repainted on first opportunity.voidsetFrameRate(int frameRate) Set target frames per second rate for this view.voidsetNumRenderThreads(int count) Sets the number of render threads.voidsetStereoIPD(double ipd) Sets the inter-pupillary distance for stereo rendering.voidsetStereoModeEnabled(boolean enabled) Enables or disables side-by-side stereoscopic rendering.voidShows the developer tools panel, toggling it if already open.voidstop()Stops rendering of this view.voidMethods inherited from class java.awt.Canvas
createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategyMethods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
-
Field Details
-
backgroundColor
The background color of the view.
-
-
Constructor Details
-
ViewPanel
public ViewPanel()Creates a new view panel with default settings.
-
-
Method Details
-
getCamera
Returns the camera representing the viewer's position and orientation.- Returns:
- the camera
-
getKeyboardFocusStack
Returns the keyboard focus stack, which manages which component receives keyboard input.- Returns:
- the keyboard focus stack
-
getRootShapeCollection
Returns the root shape collection (scene graph). Add your 3D shapes here to make them visible in the view.viewPanel.getRootShapeCollection().addShape(myShape);- Returns:
- the root shape collection
-
getInputManager
Returns the input manager handling mouse and keyboard events for this view.- Returns:
- the input manager
-
addFrameListener
Registers a listener that will be notified before each frame render. Listeners can trigger repaints by returningtruefromFrameListener.onFrame(eu.svjatoslav.sixth.e3d.gui.ViewPanel, int).- Parameters:
listener- the listener to add- See Also:
-
getPreferredSize
- Overrides:
getPreferredSizein classComponent
-
getMinimumSize
- Overrides:
getMinimumSizein classComponent
-
getMaximumSize
- Overrides:
getMaximumSizein classComponent
-
getRenderingContext
Returns the current rendering context for the active frame.- Returns:
- the rendering context, or null if no frame is being rendered
-
getDeveloperTools
Returns the developer tools for this view panel.- Returns:
- the developer tools
-
getDebugLogBuffer
Returns the debug log buffer for this view panel.- Returns:
- the debug log buffer
-
getLightingManager
Returns the global lighting manager for the scene. Add light sources here to illuminate the world.- Returns:
- the lighting manager
-
enableGlobalIllumination
Enables progressive global illumination with the default of 2 dedicated low-priority CPU threads. GI is opt-in: without this call lighting behaves exactly as before. Shadows and bounced light fade in over the first seconds and keep adapting to scene changes.- Returns:
- the running GI system
-
enableGlobalIllumination
Enables progressive global illumination with the given number of dedicated low-priority CPU threads. Calling again returns the already-running system.- Parameters:
threadCount- worker threads for ray tracing- Returns:
- the running GI system
-
showDeveloperToolsPanel
public void showDeveloperToolsPanel()Shows the developer tools panel, toggling it if already open. Called when F12 is pressed. -
paint
-
update
-
addNotify
public void addNotify() -
repaintDuringNextViewUpdate
public void repaintDuringNextViewUpdate()Calling these methods tells 3D engine that current 3D view needs to be repainted on first opportunity. -
setFrameRate
public void setFrameRate(int frameRate) Set target frames per second rate for this view. Target FPS can be changed at runtime. Use 0 or negative value for unlimited FPS (max performance mode for benchmarking).- Parameters:
frameRate- target frames per second rate for this view.
-
getTargetFPS
public int getTargetFPS()Returns the current target frames per second rate.- Returns:
- target FPS; 0 or less means unlimited
-
getMeasuredFPS
public double getMeasuredFPS()Returns the measured production rate: frames completed per second, averaged over the last ~500 ms window. This is the benchmark number: how fast the pipeline produces frames, regardless of how quickly the display path presents them (the mailbox present thread may drop stale frames when the display is slower than production).- Returns:
- measured frames per second
-
getNumRenderThreads
public int getNumRenderThreads()Returns the current number of render threads.- Returns:
- the number of render threads
-
setNumRenderThreads
public void setNumRenderThreads(int count) Sets the number of render threads. Takes effect on the next frame. The executor service is recreated lazily when the render loop detects the change.- Parameters:
count- number of render threads (must be at least 1)
-
isStereoModeEnabled
public boolean isStereoModeEnabled()Returns whether side-by-side stereoscopic rendering is currently enabled.- Returns:
trueif stereo mode is active
-
setStereoModeEnabled
public void setStereoModeEnabled(boolean enabled) Enables or disables side-by-side stereoscopic rendering. When enabled, each frame renders two eye views side-by-side.- Parameters:
enabled-trueto enable stereo mode,falseto disable
-
getStereoIPD
public double getStereoIPD()Returns the current inter-pupillary distance used for stereo rendering.- Returns:
- IPD in world units (centimeters)
-
setStereoIPD
public void setStereoIPD(double ipd) Sets the inter-pupillary distance for stereo rendering. Human IPD ranges from ~5.5 to ~7.5 cm; for XR glasses the optical IPD may differ from the user's anatomical IPD.- Parameters:
ipd- the inter-pupillary distance in world units (centimeters)
-
stop
public void stop()Stops rendering of this view. -
removeFrameListener
Removes a previously registered frame listener.- Parameters:
frameListener- the listener to remove
-